Search code examples
asp.net-mvcdesign-patternsautomapperasp.net-mvc-viewmodel

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?


Solution

  • Request/Response is a messaging pattern, but it appears you are not using messages.. instead you are using objects. This is the real crux of your problem. You're using the pattern incorrectly, and more importantly, it seems like you're using the wrong pattern for the job. Why do you need messaging in this instance? It's just extra overhead for a simple multi-layer application.

    If you really want to use messaging, you should probably serialize the data to json or xml, pass that to your service, then deserialize the data into whatever object you are using in that layer. In this way, you don't need any dependency on the data types of the other layer, because the (de)serialization process does not necessarily require such dependencies.

    Personally, I would avoid the whole messaging aspect and have a mapping layer that maps between your view model and your domain object, then call your service layer with the domain object.