October 12, 2010

Some time ago, I wrote about my project AjAgents, describing it, implementing genetic algorithms and other mini demos. The project is base in the send of messages to “agents” (I could named them actors, or something else, never mind). The agents received the messages and process them one by one. Each instance of an agent don’t need to manage concurrency: the messages are queued in an internal queue by each agent (I could reimplement this feature using a queue for many agents, if needed).

But my previous implementations relied on reflection, or CCR library by Microsoft (see my previous posts). Now, I take another way: an agent is a wrapper around a normal object. The source code can be download from my AjCodeKatas Google Code project, under trunk/AjAgents folder.

The key interface is:

publicinterface IAgent<T>
{
void Post(Action<T> action);
}

The new idea: to use a generic class. T is the type of our “classical” type (no agent), and Post is the method to invoke an action over the inner object: an action that is a “routine” receiving a T instance as parameter. So, if the inner object has a method

Note that Post method enqueues the action in an internal queue. The process of that queue is in the method .Execute (not shown). The queue is a blocking one (I know, there is one in .NET 4.0, but my code runs under 3.x, and under 2.x versions, I guess):