Wednesday, August 11, 2010

Men's Full Elastic Waist Jeans

the Event Aggregator - What is this?

Today I would like to describe another pattern / mechanism that I will be using, and it aims to significantly simplify the code and eliminate unnecessary relationships between objects.


Event Aggregator
As mentioned earlier, this pattern / mechanism is intended to eliminate unnecessary relationships between objects. It provides a single source for multiple objects. In its simplest form it looks like the class so that the record which relate to a specific event to our event aggregator. These classes are called handles (handlers). However, when we send an event to the event aggregator, it selects all the fixtures which relate to an event, sends them to an event, and they no longer deal with the perception of these events and activities related shares. In my forum I will use a fairly simple solution because it does not need anything complicated. An event is just a simple object which must implement the interface IEvent. eg
Public class
UserCreatedEvent: IEvent {}
EventAggregator: IEventAggregator
{private readonly
SynchronizationContext _context; private readonly IListenerService _listenerService;
public EventAggregator (context SynchronizationContext , IListenerService listenerService)
{
 Check.Argument.IsNotNull (context, "context"); 
Check.Argument.IsNotNull (listenerService, "listenerService");

_context = context;
 _listenerService = listenerService; 
}

public void SendMessage<T>(T message) where T : IEvent
{
var listeners = _listenerService.GetListeners<T>();

SendAction(() => listeners.ForEach(x => SendMessageToListeners<T>(x, message)));
}

private void SendMessageToListeners<T>(IListenTo<T> x, T message) where T : IEvent
{
x.Handle(message);
}

private void SendAction(Action action)
{
_context.Send (state => action (), null);}

}

heart of the whole event aggregator function is SendMessageToListeners. This function is responsible for sending events to the recipients. All brackets (handlers) are recorded IoC container FOR IN. However ListenerService just takes all those that apply to this event. Then, using the event aggregator lambda been specifically send events to a specific fixture. Everything is of course wątkowo-safe (thread-safe). In the event that was to be an asynchronous call. It sounds terrible in Polish: P Event handler that is nothing but a class that implements intertejs IListenTo that looks like this: public interface

IListenTo \u0026lt;T> WHERE T: IEvent

{void Handle (T message);}


This is a normal class with a method that has a Handle on the event argument.

And now a practical example. Say to create a new user. With the creation of entails, giving him the basic rights, sending an email confirming his / her e-mail address. In the normal case, simply a method of forming wywołalibyśmy methods from other classes associated with these activities. That is: public void
CreateUser () {
 / / here share associated with the addition of 
's ...

_rolesService.AddRole ();
_emailService.SendEmail ();}


unnecessary connections are formed with various objects. In the event of any modification services related roles, or sending e-mails it is possible that it will be a need for change in many places in the code. In small systems, it is not a problem but if grown in a difficult work, upgrading the application. It solved the same task with our Event Handler. We CreatedUserEvent event.
 / / here we have a separate class is not related at all with the method CreateUser 
class VerificationEmailHandler: IListenTo \u0026lt;CreatedUserEvent> {};
class BasicRoleHandler: IListenTo \u0026lt;CreatedUserEvent> {};
//----------------------------- ---------------------------
CreateUser public void () {

/ / here share for adding user
...
CreatedUserEvent CreatedUserEvent event = new ();
_eventAggregator.SendMessage (testEvent);}
 

Immediately I can see the removed redundant links between the objects. Method CreatedUser have no idea about the other services (servicow) non-specifically with it. In addition, the code can easily expand with new fixtures, where appropriate, as simply register a new handle, and it will be executed. You do not need any code changes.

Apart from the Event Handler, of course, I added a test associated with it. Thus created a new test project associated with mForum.Core. That's all for today:)


0 comments:

Post a Comment