Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I have an ASP.NET MVC web site working in this way:

(In the controller)

var user = _applicationService.GetUserById(1);
user.ChangeEmail("[email protected]");
user.ChangeAddress("my new street", "21");

(In a HttpModule)

void EndRequest(){
    if (HttpContext.Error is null){
        _sessionFactory.GetCurrentSession.Transaction.Commit();
     }

This way is possible because the session factory is built in the web site application start. Is posible to call multiple domain methods and call commit once in the end of the request. But in a distributed application isn't posible to use domain objects directly, instead a WCF service must receive a DTO. Now my question is: how you design the methods ChangeEmail and ChangeAddress for a distributed scenario? Each one must be a distinct operation in the Web service? If so, then when a user changes his email AND his address must be necesary to call two times to the service. Or instead I must model a web service like this:

var userDTO = _wcfService.GetUserDTOById(1);
userDTO.Email = "[email protected]";
userDTO.Street = "My new street";
userDTO.StreetNumber = "21";
_wcfService.PersistChanges(userDTO)
share|improve this question
    
The second option you have mentioned is good. You can create a method to save an entity as a whole instead of putting them separately. So you can create a method "PersistChanges" in webservice to receive the object and save. –  Thanigainathan Feb 7 '14 at 18:24
    
Now I realized what is my dilemma: DDD says you must have domain methods pertaining to a single use case. From this point of view,ChangeEmail and ChangeAddress must be distinct operations because they belongs to different use cases. I.e., the user probably doesn't change his email for the same reasons that he changes his address. –  Apocatastasis Feb 7 '14 at 18:50
    
Changing a username or address cannot be considered as use case. Instead you can consider a business scenario as use case. For example "if a user is from a particular state then he must have a certain tax percentage" - this can be considered as a use case. –  Thanigainathan Feb 7 '14 at 19:29
    
OK, let's say you have a method in your domain object named MakeUserPreferred() wich changes the state of the user and thus let us know that certain tax percentage must be applied. You would expose a MakeUserPreferred method in your web service? Or instead you set the DTO property ´UserDTO.IsPreferred = true´ and then call the service method ´_service.PersistChanges(userDTO)´?. –  Apocatastasis Feb 7 '14 at 20:14
    
Can not you build a domain User object from the retrieved DTO? In this way, you could still call ChangeEmail and/or ChangeAddress methods, and then commit the changes. Both building the domain object from a DTO and committing changes to persistent storage should be responsibilities of a UserRepository. –  rucamzu Feb 12 '14 at 11:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.