vendredi 26 février 2016

CommandHandler and EventHandler order

I am new about CQRS and want to learn work order of the pattern. My command handler and command are like this:

public interface ICommandHandler<in TCommand> where TCommand : ICommand
{
    void Handle(TCommand command);
}

When I cerate a Work a command handler is working.

public class CreateWorkCommandhandler : ICommandHandler<CreateWork>
{
    public void Handle(CreateWork command)
    {
        Save to database...            
    }
}

And I do not call command handlers in controller classes. I use a CommandExecuter to call commands by type.

public class CommandExecuter
{

    public static void ExecuteCommand<TCommand>(TCommand command) where TCommand : ICommand
    {
        var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());

        var handlers = serviceLocator.GetAll(handlerType);

        foreach (var handler in handlers)
            ((dynamic)handler).Handle((dynamic)command);
    }
}

I want to send email and send sms and another things after work created. But I can not do these steps in Handle(CreateWork command) because of separated architecture.

I think these steps are events, is this true? So I need event and event handler types.

public interface IEventHandler<in TEvent> where TEvent : IEvent
{
    void Handle(TEvent event);
}

Where can I populate the events? In CommandExecuter or CommandHandler I need multiple events for an event. for example:

public class WorkCreated:IEvent{}

Send sms, send email.

Aucun commentaire:

Enregistrer un commentaire