Re: Domain Model and Service Layer
- From: Robert Martin <unclebob@xxxxxxxxxxxxxxxx>
- Date: Tue, 30 Jan 2007 17:06:41 -0600
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 |
.
- Follow-Ups:
- Re: Domain Model and Service Layer
- From: qu0ll
- Re: Domain Model and Service Layer
- References:
- Domain Model and Service Layer
- From: mikeon
- Domain Model and Service Layer
- Prev by Date: Re: Wrapping SQL (Was: Critique of Robert C. Martin's...)
- Next by Date: Re: Critique of Robert C. Martin's "Agile Principles, Patterns, and Practices"
- Previous by thread: Re: Domain Model and Service Layer
- Next by thread: Re: Domain Model and Service Layer
- Index(es):
Relevant Pages
|