Using Autofac with Domain Events -


i'm trying introduce domain events project. concept described in udi dahan's post - http://www.udidahan.com/2009/06/14/domain-events-salvation/

here's domain event code

public interface idomainevent { }  public interface ihandledomainevents<t> t : idomainevent {      void handle(t args);  }  public interface ieventdispatcher {     void dispatch<tevent>(tevent eventtodispatch) tevent : idomainevent; }  public static class domainevents {     public static ieventdispatcher dispatcher { get; set; }      public static void raise<tevent>(tevent eventtoraise) tevent : idomainevent     {         dispatcher.dispatch(eventtoraise);     } } 

the important part ieventdispatcher implementation decouples domain event whatever needs happen when event raised. trick wire coupling through container. here's attempt

code registering domain event handlers....

        var asm = assembly.getexecutingassembly();         var handlertype = typeof(ihandledomainevents<>);          builder.registerassemblytypes(asm)             .where(t => handlertype.isassignablefrom(t)                         && t.isclass                         && !t.isabstract)             .asclosedtypesof(handlertype)             .instanceperlifetimescope();  

and resolving event handlers in dispatcher. problem no handlers resolved.

public class eventdispatcher : ieventdispatcher {     private readonly icontainer _container;      public eventdispatcher(icontainer container)     {         _container = container;     }      public void dispatch<tevent>(tevent eventtodispatch) tevent : idomainevent     {         var handlers = _container.resolve<ienumerable<ihandledomainevents<tevent>>>().tolist();         handlers.foreach(handler => handler.handle(eventtodispatch));     } } 

so i'm not registering event handlers correctly or not resolving them. how check registration working?

example code of handler

public class sendwebquestiontocso : ihandledomainevents<jobnotecreated> {     private readonly iconfig _config;      public sendwebquestiontocso(iconfig config)     {         _config = config;     }       public void handle(jobnotecreated args)     {         var jobnote = args.jobnote;         using(var message = new mailmessage())         {             var client = new smtpclient {host = _config.smtphostip};             message.from = new mailaddress(_config.whensendingemailfromwebprocessusethisaddress);             ...... etc         }     } } 

update after trial , error eventdispatcher working ok! if manually register handler , fire domain event works. assembly scanning/registraion problem. manual registration code...

builder.registertype<sendwebquestiontocso >().as<ihandledomainevents<jobnotecreated>>(); 

so how scan assemblies ihandledomainevents<> given this

public class sendwebquestiontocso : ihandledomainevents<jobnotecreated> 

as peter pointed out, registration problem where() clause.

when scanning assemblies autofac filters components automatically based on services specify, equally correct use:

builder.registerassemblytypes(asm)     .asclosedtypesof(handlertype)     .instanceperlifetimescope(); 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -