mardi 24 février 2015

MVC Mapping between View Model and Request-Response messaging pattern

I have the following MVC design pattern issue and confused which way to go.


In the UI layer, a View Model is used in a Controller action method. Cool.


The Service layer uses the Request-Response message pattern, so the service class method has a Request object as a parameter (in), and the method returns a Response object (out). This method calls a Repository method with a Domain object parameter. In other words, to call the service method, you need to populate the Request object with your data, and the method returns results in the Response object, i.e. request-response messaging.


To pass your data in your View Model to the Domain object in the Service method, you have two options AFAIK:



  1. Ensure the Request object contains the same properties as the View Model properties, then you can map (manually or automatically) values from the View Model to the Request object.

  2. The Request object contains a property of the View Model, i.e. instance of the View Model now lives in the Request object (different from above option - only one property). Now you can simply assign the View Model to the property in the Request object.


I see flaws in either approach...


In option 1, if the View Model has many properties, and you are using a mapper (e.g. AutoMapper), in the Controller method you need to automap the properties from the View Model to the Request object. Then in the Service layer, in the service method you need to automap the properties in the Request object to the Domain object. Two levels of mapping - very wrong!


In option 2, the Request object contains a property containing the View Model. You can then automap the Request.vm (property) to the Domain object easily and efficiently, but this for some reason look like poor design to me! I'm concerned about this design.


Which is the best approach? Or is there another better approach to mapping between VM and R-R pattern?


Aucun commentaire:

Enregistrer un commentaire