design - Entities and services -


lets take scenario order dispatched , optionally mail can sent customer depending on attribute in order class. functionality send email in service "emailservice". question should order.dispatch method call emailservice send email depending on attribute in order class or should application layer after calling order.dispatch method call emailservice? correct way in ddd?

thank you. nn

domain events allow decouple concerns. order dispatched, raise domain event informing interested subscribers. allows make decision send email somwewhere else, order aggregate can remain pure. helps capture language better; when order dispatched, send email.

public class order  {     public order(string id)      {         guard.forempty(id, "id");          id = id;     }      public string id { get; private set; }      public void dispatch()      {         domainevents.raise(new orderdispatchedevent());     } }  public class mailservice : ihandle<orderdispatchedevents> {     private readonly imailsender _mailsender:      public mailservice(imailsender mailsender)      {            _mailsender = mailsender;     }      public void handle(orderdispatchedevent @event)      {         _mailsender.send(...);     } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -