3

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

(In the controller)

var user = _applicationService.GetUserById(1);
user.ChangeEmail("mynemail@somewhere.com");
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. It is possible to call multiple domain methods and call commit once in the end of the request.
But in a distributed application, it isn't possible to use domain objects directly, instead a WCF service must receive a DTO.
Now my question is: how do 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 necessary to call two times to the service. Or instead I must model a web service like this:

var userDTO = _wcfService.GetUserDTOById(1);
userDTO.Email = "mynewemail@somewhere.com";
userDTO.Street = "My new street";
userDTO.StreetNumber = "21";
_wcfService.PersistChanges(userDTO);
shA.t
  • 103
  • 7
Apocatastasis
  • 215
  • 1
  • 5
  • 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 07 '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 07 '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 07 '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 07 '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
  • You can't do that. The change tracking is performed by the session factory, wich in this case lives in the Web service, not in the consuming Web site. The UserRepository is encapsulated in that Web service. – Apocatastasis Feb 12 '14 at 23:27

0 Answers0