c# - Where do long running, stateful 'services' fit in DDD? -
in more industry or automation related applications (that heavily rely on external components have manage), you'll end case domain contains models more abstractions real problem, representations of , pointers physically exist outside of domain.
for example, take domain entity represents network device:
public class networkdevice { public ipaddress ipaddress { get; set; } }
instead of storing or validating such entities or taking actions upon entity changes, application may need manage external components based upon representations inside domain. now, ddd suitable such cases? managers domain services?
eric evans describes in famous blue book domain service needs stateless model implementing methods taken ubiquitos language fullfil request entity, or repository cannot handle itself. if service needs stateful?
a simple example: application needs monitor configured ip devices within network in order notify other application inside domain state events. if ip device gets registered inside application (stored inside database, example), "ping-service" gets notified , starts monitor device.
public class pingmonitor : idisposable, ihandle<deviceregisteredevent>, ihandle<deviceremovedevent> { public list<networkdevice> _devices = new list<networkdevice>(); public void handle(deviceregisteredevent @event) { _devices.add(@event.device); } public void handle(deviceremovedevent @event) { _devices.remove(@event.device); } public void pingworker() { foreach(var device in _devices) { var status = ping(device.ipaddress); if(status != statusbefore) domainevents.raise<devicestateevent>(new devicestateevent(device, status)); } } }
other components handle status events , e.g. stop talking device via other protocols if device goes offline.
now, components? @ first thought domain services because serve requirement of domain. however, stateful not represent ubiquitos language (the task of ping-service ping domain entity , report it's status, ping-service not implement method clients allow ping device).
are application services? such components fit inside ddd pattern?
i once implemented similar function, hope helps :)
our organization owns online-payment handling application. once customer finishes payment, online-payment provider sends use notification indicating success or failure. somtimes network failures occur, notification may never reach our application. hence, here comes disgruntled customers. auto-checking mechanism needed.
an application runner responsible keeping check running:
public class checkingbatch { private transactionchecker transactionchecker; public void run() { list<transaction> transactions = transactionstobechecked(); (transaction transaction : transactions) { //publish events if transaction needs check docheck(transaction, now); } } } private list<transaction> transactionstobechecked() { return transactionrepository.findby(transactionchecker .atobecheckedspec()); } }
annother application service listens event , actual check:
public class checktransactionserviceimpl implements checktransactionservice { private transactionchecker transactionchecker; @transactional public void check(final transactionno transactionno) { transaction transaction = transactionrepository.find(transactionno); checkresult result = transactionchecker.check(transaction); //handle check result } }
the transactioncheck online-payment-solution agnostic domain service:
public interface transactionchecker { /** * * | data between online-payment provider , ours | txn status | result |<br> * | consistent | closed | valid |<br> * | inconsistent | closed | invalid |<br> * others omitted */ checkresult check(transaction transaction); /** * returns txn specification filter checked ones. */ tobecheckedspecification atobecheckedspec(); }
as may see, both application service , domain service stateless.
imho, ping domain service(correlate txnchecker), monitor kind of application service(correlate checkingbatch).
Comments
Post a Comment