I am reading about DDD and in the examples it is common to see how the domain gets the data from database, do the business logic and then persist the data. For example to update a field:
class MyDomainClass()
{
//Dependency injection of the repository
public MyDomainClass(IRepository paramRepository)
{
_repository = paramRespository;
}
public static DoWork(Type1 paramData1, Type2 paramData2)
{
var myDomainInstance = _repository.GetEntity(entityID);
//do work
_repository.Persist();
}
}
But my doubt is, if the domain model only has to do the business logic, why does it has to load the data from the database and persists the updates in the database? wouldn't it be this responsability in a upper layer that load the data, call the methods of the domain classes and then persist the data? Perhaps this upper layer could be a service layer or application layer.
Somthing like that:
class MyDomainServiceDoWork
{
public MyDomainServiceDoWork(IRepository paramRepository)
{
_repository = paramRepository;
}
public void DoWork(Type1 paramData1, Type2 paramData2)
{
var myDomainEntity = _repository.GetEntity(entityID);
myDmainEntity.DoWork(paramData1, paramData2);
_repository.Persist();
}
}
class MyDomainClass()
{
//No dependy injection in this case.
public MyDomainClass()
{
}
//Now the method doesn't need to be static
public DoWork(Type1 paramData1, Type2 paramData2)
{
//No get the instance from database.
//do work
//No persist data here, it is responsible of the service
}
}
Also someone comments that the domain should to be implemented thinking that the classes are in memory. According to that, then persist the data seems not have sense, because it works only in memory.
Another point that is related is that sometimes I have read that if the domain classes can be used in the UI, it is an option. I like this idea, because I can create instances locally and use the business logic of them and no persist the data. I think it is interesting because I can validate the data, and if it is correct, then call the service or application layer methods that I know that it will pesist the data. here de UI has to know that the services persist data and the domain model just do business logic.
One example in the UI could be this:
var myDomainInstance = new MyDomainClass();
//this no persist data, just I use it to have a local instance for validating and to use locally if the action is OK.
var myAction result = myDomainInstance.DoAction(userData1, userData2);
if(myActionResult != null)
{
//notify user which data is incorrect and the problem.
}
else
{
//This persist the data
MyService.DoAction(userData1, userData2);
}
//Here I can use myDomain instace as local variable to work with it. If I could persist the data in database, then I get the same state in myDomainInstance than in database.
Well, in the example I don't want to detail the way to handle the errors, there are many options for that. My objetive it is to show the idea to use in the UI the domain model or the service according to the needs.
In sumary, I would like to know why it is so common to get the data from database and persists the changes in the domain classes. I guess it is more flexible to have a domain to only do the work of the domain and let to another the responsability to persist the data. So a consumer (the UI or user) can decide when persist the data, using the service or only want to do a work in the instance of a domain that it has locally.
Thanks.