c# - Nservice bus sagas implemetation -
i have nservice bus project which call connector. connector receives variouis kinds of messages example clientchangemessage, clientcontactchangemessage. connector has not implemented saga have handler each message clientchangemessage have clientchangemessagehandler gets fired when connector receives clientchangemessage , clientcontactchangemessagehandler when receive clientcontactchangemessage.
now while looking @ sagas implementation found myself writing following code (if client contact message comes before clientchange message i.e client not exist in database):
public class clientcontactchangemessagehandler : clientmessagehandler, ihandlemessages<clientcontactchangemessage>, iamstartedbymessages<clientcontactchangemessage>, ihandlemessages<clientchangemessage> { [setterproperty] public iclientcontactchangedb clientcontactchangedb{get;set;} [setterproperty] public ibusreftranslator busreftranslator{get;set;} static clientcontactchangemessagehandler() { logger = logmanager.getlogger(typeof (clientcontactchangemessagehandler)); } static ilog logger; public void handle(clientcontactchangemessage message) { //some handling logic } public void handle(clientchangemessage message) { throw new notimplementedexception(); } public override void configurehowtofindsaga() { configuremapping<clientcontactchangemessage>(s => s.id, m => m.id); configuremapping<clientchangemessage>(s => s.id, m => m.id); // notice have no mappings orderauthorizationresponsemessage message. not needed since hr // endpoint bus.reply , nservicebus automatically correlate reply // originating saga } } public class clientmessagehandler : basemessagehandler { } public class basemessagehandler : saga<mysagadata> { } public class mysagadata : icontainsagadata { public guid id { get; set; } public string originator { get; set; } public string originalmessageid { get; set; } }
as can seen example have implement handle clientchangemessage , have defined handler clientchangemessage ,do have handle again on here because if further on in time clientchangemessage come expect caught , processed clientchangemessagehandler nad not one.
i store message if , if don't find local reference client in database . looking @ examples saga on web dont't see particular place or condition handled. hoping storing message inside clientcontactchange handle method.
any appreciated,
thanks
update:
it seem did not understand how implement nservice bus saga. mistake made here according me considered client contact change single entity i.e independent of client change message. therefore think ma wrong in implementing saga client contact change . here how had change code:
public class clientsaga : saga<clientsagastate>, iamstartedbymessages<clientchangemessage>, iamstartedbymessages<clientcontactchangemessage>, ihandletimeout<clientsagastate> { [setterproperty] public iclientcontactchangedb clientcontactchangedb{get;set;} [setterproperty] public ibusreftranslator busreftranslator{get;set;} public void handle(clientcontactchangemessage message) { //some handling logic //check if client not in database store state this.clientcontactchange=message; //if client in data base markascomplete(); } public void handle(clientchangemessage message) { //update or create client depending on situation //check dependencies if(this.clientcontactchange !=null) { //handle contact change } } public override void configurehowtofindsaga() { configuremapping<clientcontactchangemessage>(s => s.clientref, m => m.clientref); configuremapping<clientchangemessage>(s => s.clienttnref, m => m.id); // notice have no mappings orderauthorizationresponsemessage message. not needed since hr // endpoint bus.reply , nservicebus automatically correlate reply // originating saga } } public class clientsagastate: icontainsagadata { //i dont need these 3 fields public guid id { get; set; } public string originator { get; set; } public string originalmessageid { get; set; } // fields needed public guid clientref {gee; set;} public clientchangemessage clientchange {get;set;} public clientcontactchange clientcontactchange {get;set;} }
since both handlers handle same message type, both called. if specify order in called using ispecifymessagehandlerordering. furthermore, can short circuit chain based on condition may solve secondary issue.
if not work, may want consider versioning message support both scenarios in graceful way.
Comments
Post a Comment