c# - Same Rebus handler instance for multiple messages within a Unit of Work -


i wish process related messages in batch e.g. processing events customercreated , preferredcustomer same handler (same instance) within same scope/transaction using rebus service bus.

the same handler handling both messages/events:

class customerhandler : ihandlemessages<customercreated>, ihandlemessages<preferredcustomer> {     customer customer { get; set; }      public customerhandler() {         customer = new customer();     }      public void handle(customercreated message) {         customer.name = message.name;         console.writeline(customer);     }      public void handle(preferredcustomer message) {         customer.rebate = message.rebate;         console.writeline(customer);     } } 

when sending messages use batch operation (transport messages in nservicebus)

bus.advanced.batch.publish(   new customercreated() { name = "anders" },    new preferredcustomer() { rebate = 10 }); 

to control lifetime of handler, use windsor castle’s scoped lifestyle

_container.register(   component.for<ihandlemessages<customercreated>, ihandlemessages<preferredcustomer>>)     .implementedby<customerhandler>().lifestylescoped()); 

and custom unitofworkmanager instanciates scopedunitofwork

class customunitofworkmanager : iunitofworkmanager {     private readonly iwindsorcontainer _container;      public customunitofworkmanager(iwindsorcontainer container) {         _container = container;     }      public iunitofwork create() {         return new scopedunitofwork(_container);     } }  class scopedunitofwork : iunitofwork {     private readonly idisposable _scope;      public scopedunitofwork(iwindsorcontainer container) {         // begin transaction         _scope = container.beginscope();      }      public void dispose() {         _scope.dispose();     }      public void commit() {         // commit transaction         console.writeline("commiting");     }      public void abort() {         // rollback transaction                     console.writeline("aborting!!!");     } } 

finally configured rebus use customunitofworkmanager

var bus = configure.with(new windsorcontaineradapter(_container))   .transport(t => t.usemsmqandgetinputqueuenamefromappconfig())   .messageownership(d => d.fromrebusconfigurationsection())   .events(x => x.addunitofworkmanager(new customunitofworkmanager(_container)))   .createbus()   .start(); 

is correct approach?

my limited testing shows should work. able expand include transaction management against data store within scopedunitofwork too.

sounds have nailed :)

if correct commit/rollback behavior, i'd it's fine , dandy.

if you're interested in alternative, might want take @ pertransportmessage castle windsor scope accessor - can used this:

container.register(     component         .for<isomething>()         .implementedby<whatever>()         .lifestylescoped<pertransportmessage>() ); 

which should able achieve exact same behavior.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -