I have this order:
class Order
{
long Id;
string Concept;
string Comments;
//another properties
UpdateConcept(string paramConcept) {...}
UpdateComments(string paramComments) {...}
}
In the domain entity, I have to have one method for each property that I can update.
But my doubt is in the application layer and UI layer. Suponse that in the UI I have a form that shows the information of the order, and the user see that the concept and the comments have to be update to correct some error.
Do I have to update first the concept and then the comments in two separate actions or I could have a generic update that update all the data in a one process?
Because in my case, I have a UI (WPF application for example) that use my application layer in which I have the OrderService. This is the order service if I would have a generic update method:
//Application layer
public OrderService
{
public OrderService(IUnitOfWork paramUoW)
{
_UoW = paramUoW;
}
IUnitOfWork _UoW;
Update(string paramConcept, string paramComments)
{
Order myOrder = _UoW.OrderRepository.Get(1);
myOrder.UpdateConcept(paramConcept);
myOrder.UpdateComments(paramComments);
_UoW.Commit();
}
}
In this case, I only have to call the database one to save the data. So the user could have edit both fields in the form and when he considerate all is correct, to click the button accept to save the data. This accept button would call the update method of the service in the application layer.
If I would have one method for each update of each field, I would in the service something like that:
//Application layer
public OrderService
{
public OrderService(IUnitOfWork paramUoW)
{
_UoW = paramUoW;
}
IUnitOfWork _UoW;
UpdateComments(string paramComments)
{
Order myOrder = _UoW.OrderRepository.Get(1);
myOrder.UpdateComments(paramComments);
_UoW.Commit();
}
UpdateConcept(string paramConcept)
{
Order myOrder = _UoW.OrderRepository.Get(1);
myOrder.UpdateComments(paramComments);
_UoW.Commit();
}
}
So I guess the option it is to call the method UpdateComments when the user leave the comments field and call the the method to update the concept when it will leave the field of concept. But it is duplicate the calls to the database.
How could handle this case, when it is needed to update various properties of the entity?
Perhaps I could separate in the service of the application layer the 3 steps of the update, load the order, update the data and commit the data, something like that:
//Application layer
public OrderService
{
public OrderService(IUnitOfWork paramUoW)
{
_UoW = paramUoW;
}
IUnitOfWork _UoW;
GetOrder(long paramId)
{
_UoW.OrderRepository.Get(paraId);
}
UpdateComments(string paramComments)
{
Order myOrder = _UoW.OrderRepository.GetLocal(1);
myOrder.UpdateComments(paramComments);
}
UpdateConcept(string paramConcept)
{
Order myOrder = _UoW.OrderRepository.GetLocal(1);
myOrder.UpdateComments(paramComments);
}
void Commit()
{
_UoW.Commit();
}
}
In this case, I would separate the load of the entity to update, and the methods to update only update the entity without commit, and when the user is ready to confirm the update, it could click the accept button, that calls the method commit of the service.
But in sumary, I would like to know how to handle the case that various properties of the entity need to be updated.
Thanks.
Update(string paramConcept, string paramComments)
) is the correct way to do it, just give the method a less generic name. It's the approach with the least amount of coupling. The interface of your service shouldn't just duplicate the interface of the Order (shouldn't have all the same methods), generally speaking. That almost defeats the point of having a service. The methods on the service should reflects the tasks or usage patterns of your app's users, not small steps within those. So, something likeUpdateOrderNotes
orAddOrderItems
.