Tuesday, May 19, 2009

MassTransit Messages

[Intro]

Messages are the backbone of MassTransit, without them there would not really be a need for the solution. Messages IMO should be a Verb. "Customer" is not a suitable message name as it has no intent, "NewCustomerCreated" is therefore a more suitable name. As far as MassTransit goes a message just needs to be a class that is marked as [Serializable]. For most scenarios is have encountered I actually want to track a specific message, i.e. I want to know its identity (which we will cover soon), so I have my message implement the interface "MassTransit.CorrelatedBy<T>" which gives the message a Correlation Id so I can track it. It is probably a good time to mention that messages are Immutable dumb DTO's. I have worked on several systems now that try to ignore this and every time it has ended in trouble. The message is a trigger, it should never be the entity you manipulating.

An Example from the MassTransit Pub/Sub Sample is below:

[Serializable]
public class RequestPasswordUpdate :
CorrelatedBy<Guid>
{
private readonly string _newPassword;
private readonly Guid _correlationId;
public RequestPasswordUpdate(string newPassword)
{
_correlationId = Guid.NewGuid();
_newPassword = newPassword;
}
public string NewPassword
{
get { return _newPassword; }
}
public Guid CorrelationId
{
get { return _correlationId; }
}
}


Using the correlation Id means that  later on when I want to listen for associated messages I can. This will be covered in [Consumers/Publishers]

2 comments:

Lee Campbell said...

"New Customer Created" is that to differentiate from "Old Customer Created"?

RhysC said...

no one likes a smart ass. Any way it could be super exclusive place that has NewCustomerDenied... i dont know... its a post on Masstranist not a competitor to Code Complete 2... jack ass ;)