I am putting together a new system and I would like to have a rich domain for it. However, I am stuck on a little detail I want to know more about.
Suppose I would have a class Customer, where do you actually persist a customer entity or aggregate root?
should it be the customer class that has the knowledge of persistence?
var myCustomer = CustomerFactory.CreateCustomer(id);
myCustomer.Name = ...
myCustomer.LastName = ...
myCustomer.Save()
in this case I would need to pass my repository of Customers into the customer class (either via the factory or some injection). Notice that I could not find a good example of this in the DDD book by Eric Evens.
Another way is keeping the customer free of knowing about persistence
var myCustomer = CustomerFactory.CreateCustomer(id);
myCustomer.Name = ...
myCustomer.LastName = ...
CustomerRepository repository = new CustomerRepository();
repository.Save(myCustomer);
Also I suppose I can have a service class that does it all together like this:
AddCustomerService service = new AddCustomerService(CustomerRepository repository)
service.AddCustomer(myCustomer);
just a little note: if I don't actually save in the customer class, I find that my customer class is nothing but properties, maybe with a little validation but nothing more. No real behavior is there. Behavior is moved either to service class or repositories used by the client...
I have a DDD project. There are 4 layers in this order (because I am not using WCF else there would be more):
1. UI
1.1 MVC4.csproj
... (Repository Interfaces)
... (Repository Implementations)
2. Service Layer
2.1 Service.cs
...DTOs
3. Business Layer
3.1 DomainObjects.csproj
4. Data Access Layer
4.1 DAL.csproj (Entity framework 5)
... (Repository Interfaces)
... (Repository Implementations
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments