Re: Domain Model and Service Layer



On 2007-01-30 08:09:33 -0600, mikeon@xxxxxxxxx said:

Hello!

I'm working on a domain model (accorting to DDD) where I represent a user as an entity.
Creation of a user is handled by a factory i.e.:
UserFactory uf = GetFactory();
User user = uf.CreateUser("name", "login", "email");

What I want to achieve is to be able to send new users an email, when their account is created.
Where should I put that kind of logic? I understand that sending an email i.e.: interaction with an external service should be done via some kind of a service layer object. Where should I use this object?
Possible solutions include the application layer where the create user call originates from. There I could easily add a call to a service right after a user is created.
I could put a dependency in the factory class so that it would call the service.
I could put the dependency in the repository so it calls the service when object is saved.

Consider partitioning your application into entities, controllers, and interfaces. The entities are like your User class. They hold the behaviors that are local to the entities and require no external dependencies. The UserFactory interface is part of the entity layer.

There may be several different interface layers. One might hold the code that builds web pages. Another might be the code that manages the database. The implementation of the UserFactory might go in the database interface layer.

The controller layer has classes that respond to user gestures. They make calls to the entities and interfaces and coordinate the interaction of the user (not the User class) with all the other entities and interfaces.

One of the controller classes would be responsible for responding to the user gesture that causes a User to be created. That class would call the UserFactory, and then would invoke the mail server.

The mail server would be in the mail interface layer.

Some simple dependencies rules might be helpful.
1. The entity layer has no external dependencies. It knows about itself and nobody else.

2. The interface layers know about the entities, but not about the controllers.

3. The controllers know about the entities and interfaces. (though it's better to isolate them using interfaces.)

--
Robert C. Martin (Uncle Bob)  | email: unclebob@xxxxxxxxxxxxxxxx
Object Mentor Inc.            | blog:  www.butunclebob.com
The Agile Transition Experts  | web:   www.objectmentor.com
800-338-6716                  |



.



Relevant Pages

  • Re: Layers, Levels, and DIP
    ... >> or interfaces. ... >> making the layer structure equivalent to a procedural program. ... their diagrams often depict dependencies without ... interfaces that the infrastructure layer should implement. ...
    (comp.object)
  • Re: What would be a truly relational operating system ?
    ... lower physical layer (I mean on current disks, RAM, and CPU ... One perhaps tangential problem is that mainstream OS designers aren't really into making coherent interfaces, let alone applying information theory, even though some of them jabber about that. ... I had to cope with an IBM mainframe OS called MVS. ...
    (comp.databases.theory)
  • Re: Exposing Business Layer Objects to Presentation Layer
    ... >> those interfaces do the mapping of the data to their own paradigm. ... layer OSI reference model for communication. ... C++, Eiffel, Ada, Modula, etc, that means a SAP should be an abstract ... >> case the implementation of the Xout Facade interface implementation ...
    (comp.object)
  • Re: Exposing Business Layer Objects to Presentation Layer
    ... >> those interfaces do the mapping of the data to their own paradigm. ... layer OSI reference model for communication. ... C++, Eiffel, Ada, Modula, etc, that means a SAP should be an abstract ... >> case the implementation of the Xout Facade interface implementation ...
    (comp.lang.java.programmer)
  • Re: How to solve "circular dependencies"?
    ... > Since dependencies always exist among modules. ... Each subject matter should ... the service subsystem subject matter must resolve in the overall problem ... While building systems around interfaces is laudable, ...
    (comp.object)