Last post Jul 06, 2014 05:59 PM by rickevry
Jul 05, 2014 04:01 PM|jlundstocholm|LINK
I have an existing system (a couple of Windows Services, an WCF endpoint etc) that all share a common data access layer (a VS class project, using EF6, code first and EF6Migrations).
Now we need to add an MVC project to allow users to configure our system. So the MVC app will access existing classes and entities already defined in our system (in our assembly MyComplexSystem.Data.dll).
The thing I can't figure out is how to - best - hook MVC up to our existing system. By convention MVC will have model classes in the \Models-folder, but in our system the model classes exist in an entirely different project.
How do I do this - without breaking one project or the other when they are connected?
Jul 05, 2014 05:39 PM|Mikesdotnetting|LINK
By convention MVC will have model classes in the \Models-folder
It's not really a convention. Framework conventions in the truest sense are practices that the framework relies on, such as the location of Views or controllers being named in a particular way. No part of MVC relies on code files being placed in a folder
called Model. Actually, stuffing all that code into one folder can result in a bit of a mess. One of the first things I do is delete the "Models" folder. I usually create a separate class library project for the business objects.
Just add a reference in the MVC solution to the project that your model classes reside in.
Jul 06, 2014 11:50 AM|Yorrick vd Voort|LINK
The models used in a MVC project are ViewModels. These models reflect the data that is shown in a View. When, like in your case, you have business models that come from a DAL or BLL you will have to map those to the ViewModels in the MVC project.
I.e. You have a business object called "Person" and it consists of the following properties:
On the other hand you have a View for displaying only the First and Lastname of a person. You will need to create a ViewModel for this View containing the following properties:
Now in your controller you will do a call to your BLL to get the person of who you want to show the first and lastname. This will return the business model with all properties. In the same action you instantiate a new ViewModel and map the necessary properties
ViewModel.Id = BusinessModel.Id
ViewModel.Firstname = BusinessModel.Firstname
ViewModel.Lastname = BusinessModel.Lastname
The ViewModel is used to render the View with the least amount of data necessary. This way you won’t have to change any of your existing layers and you’ll have full reusability of them.
If you find manually mapping a tedious job you could opt for something like automapper:
https://github.com/AutoMapper/AutoMapper. I prefer to do it manually because that way I know exactly what is happening and where.
Hope this helps.
Jul 06, 2014 05:59 PM|rickevry|LINK
It is not a perfect design but you can use your business classes as view models.
Go into web.config under system.web/pages/namespaces and add:
I do not think this will get you into trouble if you are building an administration interface.