Create n-tier with MVC 4 and EF 5

I am working on creating an n-tiered application where I will have two separate projects

1) project EF (where it will have all my edmx...) 2) project MVC 4 (internet application.) 

In my EF, I have my .edmx file, and it generates a couple of classes with all the details, as shown below (as an example) ...

 //------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ public partial class Requester { public int Id { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } <//more...........> } 

all is good so far!

Back to MVC project

Now I will create a new controller in my MVC project, and when I try to create Scaffolding and specify the name Controller and Controller expects Model , so the real question is:

What do I need Model here? should i have the same class that created EF? or should I create another Model in my Model (MVC) and link it? if so, then I do not create a duplicate property, if I continue and create the same model in the MVC Model Folder project?

What am I trying to do ?: Well, my goal of this exercise is to completely separate my data access level (DAL) from the MVC project.

any thoughts?

+4
source share
2 answers

I suggest creating a view model so that you can decorate properties with the help of materials associated with it (i.e. UIHint). In addition, this presentation model will be a reduced version of the class (for example, it may contain only the identifier of the associated object, and not the entire object), which simplifies the use of action as parameters.

Also, you are talking about objects here, try not to think about "data."

+1
source

MVC really needs to be renamed VMVC - ViewModel View Controller.

Models in MVC have nothing to do with EF, Persistence, or your domain. They represent a composition of several data sources / settings / things that are / are needed in the presentation.

So, create new models for your views.

Edit:

All examples / tutorials that use the first EF Code models as view models are scary tutorials / examples. They teach you bad practice, because in the real world you will never and never will use them directly in your gaze.

ViewModel is the composition or aggregation of the data that is included in your view. For instance:

If you have a product information page, you can get information about the Product from the database, the availability of the product from the web service, your shopping cart from some cache.

They will be arranged in a ViewModel that displays the view you are viewing. And displayed.

ViewModels should not be shared between views, because if you change the ViewModel, you will change the value of the views that share the View Model view.

+1
source

Source: https://habr.com/ru/post/1499513/


All Articles