Internal Messaging for Castle IoC Components
June 1st, 2007 by TrilobyteTags:Castle Project, Inversion of Control, MicroKernel, Windsor Container
At the moment I am working on a framework which provides loose coupled internal messaging support for components that live in the Castle Windsor IoC container. This messaging framework stimulates the decoupling of components by providing loose coupled messages.
The messages can be sent from any component that is declared to be a message sender to any component that is declared to be a message receiver. The components, for the senders, do not know to which components receive the message or, for the receivers, where the message originated from.
The messages contain Meta information describing the payload of the message. Currently I am working on the messages themselves. Here is an example of how you can extract information from a message without knowing its explicit content.
Firs we will create a message and its payload:
// creating the message and it's payload
Person person1 = new Person("Bert");
IContentMetaInfo metaInfo1 = new ContentMetaInfoBase(typeof(Person));
IContent content1 = new ContentBase(person1, metaInfo1);
Person person2 = new Person("Trilobyte");
IContentMetaInfo metaInfo2 = new ContentMetaInfoBase(typeof(Person));
IContent content2 = new ContentBase(person2, metaInfo2);
MessageBase msg = new MessageBase(new IContent[] {content1,content2});
Image the message is send to somewhere and then take a look at the receive code below:
// get the payload from the message
foreach (IContent personContent in msg.FindContentByMetaInfo(delegate (IContentMetaInfo inf) {return inf.ContentType == typeof(Person);}))
{
// process the person content
Person p = (Person)personContent.Content;
Console.WriteLine(p.Name);
}
As you can see, I only extracted the data I discovered, for all I know the message contains tons of other information, but I do not care about that.
Although my current implementation is not completed yet and there is a lot of stuff still to do, I really wanted to share this sample with you, because I think this method is a good way to decrease coupling between components in the system.
I will keep working on the framework as much as I can, so expect more information about it soon. In the mean time reactions about ideas, questions or comments are very welcome!