locked
How to use Repository Pattern in MVC with View specific Models? RRS feed

  • Question

  • User1639864676 posted

    I'm trying to implement the repository and unit of work patterns in an existing MVC project. I already have classes which I use to create different models specific to particular Views. These classes communitcate to the database using Entity Framework, and a using() block to call Dispose.

    I've seen many many examples of using Repo/UoW patterns directly in Controllers.  But I have custom View specific Models (which I manage with classes like SalesOrderViewModelManager) in many cases which help select and populate models for the Views. I don't know how to manage view specific models like this or if this is a normal thing to do, as I'm kind of new to alot these frameworks and patterns.

    What I am trying to figure out is where now and to implement the Repo/UoW patterns. A ViewModelManager will have a method like:

    public SalesAgreementVM GetAgreement(int CustomerId, int OrderId)

    This and all other methods communitcate with the database via Entity Framework with a using() block in the method itself.

    I don't implement all CRUD, in fact, this app pretty much only reads from the DB.  But now that must change, as it needs to update the DB entities now.

    When I recieve a POST from a user wanting to edit the data, I'm using the controller to do model binding back into the View specific Model that I used to create the view the client browser uses to edit the data.  But this model does not match any specific repo, as it was designed for the View.

    My instincts are telling me I need some sort of layer between my View specific models and my Repos to implement CRUD operations. But I just don't know much about this.  I learn fast, but I'm still very junior.

    Monday, June 19, 2017 4:21 PM

Answers

  • User1068175894 posted

    look into automapper

    this library will help you convert your view models to your entities and back, the main responsibility of the controllers would be to receive the vm from the clients, call automapper to convert them to entities and pass the entities to your underlying services; and back, get the entities from your services, convert them to view models and pass them to the views.

    your vms should never leak your mvc application to the other layers underneath

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 19, 2017 5:05 PM
  • User753101303 posted

    Hi,

    It's not one way. It just map an input object to an output object (you'll just map B to A in addition to mapping A to B). Try https://github.com/AutoMapper/AutoMapper/wiki/Reverse-Mapping-and-Unflattening for details.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 20, 2017 4:51 PM

All replies

  • User1068175894 posted

    look into automapper

    this library will help you convert your view models to your entities and back, the main responsibility of the controllers would be to receive the vm from the clients, call automapper to convert them to entities and pass the entities to your underlying services; and back, get the entities from your services, convert them to view models and pass them to the views.

    your vms should never leak your mvc application to the other layers underneath

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 19, 2017 5:05 PM
  • User1639864676 posted

    From what I've been able to research, AutoMapper is sort of a one way street.  It will take my Domain objects and map them to my UI ViewModel objects, but it won't do the reverse. 

    If this is true, automapper will pretty much just replace my ViewModelManager objects I've been creating to code the mappings myself. Which is super cool, but NOT exactly an answer to my question:

    Once I get the data back from a POST and the controller does model binding back into the ViewModel what then. The ViewModel does not match any specific repo, as it was designed for the View. I need to get the data back into the repo to be persisted. Do I have to write that all manually for each and every ViewModel I create??

    Tuesday, June 20, 2017 4:39 PM
  • User753101303 posted

    Hi,

    It's not one way. It just map an input object to an output object (you'll just map B to A in addition to mapping A to B). Try https://github.com/AutoMapper/AutoMapper/wiki/Reverse-Mapping-and-Unflattening for details.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 20, 2017 4:51 PM
  • User1639864676 posted

    Your right. I was looking at old material.  I watched the video they had on their main page in which Jimmy Bogard is talking about what must be like the first version of the library.  It claims they don't do reverse mapping, but I guess the project has grown quite a bit since that video.  I'll try it out.

    Thursday, June 22, 2017 1:20 PM