2

First of all , I use EF into Dal layer (separeted project from MVC, same solution). The generated models from EF's EDMX file are the actual models from Model layer?? If so, how do I access these models to work in MVC's View layer? I think it's wrong accessing data layer directly from view to work with these models, and If I make a Model layer with "my models" and translate Dal's models into mine models... it'll be duplicated code.

Probably I'm getting something wrong, but most of egs. are with code first approach, and I can't figure out this.

Wagner Leonardi
  • 4,226
  • 2
  • 35
  • 41
  • possible duplicate of [ORM Entities vs. Domain Entities under Entity Framework 6.0](http://stackoverflow.com/questions/18109547/orm-entities-vs-domain-entities-under-entity-framework-6-0) – Gert Arnold Sep 13 '13 at 18:15

4 Answers4

3

You are right in your idea of not accessing access the Models in your DAL directly from your presentation layer.

To avoid duplicating code when translating your DAL objects into the Models used by your views, you could use something like AutoMapper, which is supposed to do the heavylifting for you in exactly that scenario.

Tallmaris
  • 7,605
  • 3
  • 28
  • 58
  • 1
    Is this the best solution? I though EF would have a better solution for this (like I doing POCO by myself in a separated layer, and by somehow EF track it. Actually I watched a video that a guy do it 'hacking' EF generated files, but it's very very painful). For small development that is all in the same project EF is awesome, but doesn't sounds productive to me using this in a big dev. with separation of concerns approach.. because sounds painful as well doing this 'fix' with automapper. Do you agree with me , or am I using EF wrong? – Wagner Leonardi Sep 13 '13 at 15:58
  • 1
    EF has the solution you mention by using the Code First approach, that is you create your POCO Models and they get persisted in the DB transparently by EF. Since you are using EDMX I assume you are generating classes based on an existing DB... In that case you have to use something to map or translate those classes into Models usable by the View (it they are not already usable or if you want to keep your DAL separated). The way of doing this is by creating a mapping layer, and Automapper makes this extremely simple (little to no code to write on your part). – Tallmaris Sep 14 '13 at 12:08
  • 1
    IMO, handling DB in code (code first approach) is unacceptable! That's why I prefer database first, but seriously, I'll choose your reply as solution because you explained that 'right solution' is to create models for Model layer by myself, and it isn't dumb duplicated code. Afterwards, [Abbas Amiri complemented it very well](http://stackoverflow.com/questions/18788705/dal-with-entity-framework-and-model-layers-into-mvc/18793774#18793774). Thank you guys both! – Wagner Leonardi Sep 14 '13 at 18:21
  • 1
    Well, about the code first approach there is a lot to be said, I find it quick and more maintainable and makes it easier to keep your db in sync with the code, through migrations and versioning... All things you can do with a db first approach of course, it's just that I find it easier and relieves a lot of the burdens of db maintenance and syncing. :) – Tallmaris Sep 15 '13 at 09:20
2

I think it's wrong accessing data layer directly from view to work with these models...

That's right, the appropriate method is using View Model

When you have dozens of distinct values to pass to a view, the same flexibility that allows you to quickly add a new entry, or rename an existing one, becomes your worst enemy .You are left on your own to track item names and values; you get no help from Microsoft IntelliSense and compilers . The only proven way to deal with complexity in software is through appropriate design. So defining an object model for each view helps you track what that view really needs. I suggest you define a view-model class for each view you add to the application.

-- "Programming Microsoft ASP.NET MVC" by Dino Esposito

A ViewModel provides all information that your view requires to make itself. To transfer data from ViewModel and a business entity you can use AutoMapper.

Don't worry about duplication, those are two different concept and should be separated from each other; it makes your application easy to maintain.

Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23
1

I may be mistaking but to me, using generation from EDMX provides you with the DbContext which could be considered as the DAL and entities which could be considered as the Model.

So you might directly manipulate entity instances as your business object. Manipulation of the base through the DbContext should appear in the BLL layer. Additionally, you might implement DTOs where needed.

This, of course, assumes you want to use entity framework code generation. Other options like using POCOs might be more relevant considering your overall architecture.

Marshall777
  • 1,196
  • 5
  • 15
  • If I understood correctly, Should I use EF into a hybrid layer (that'll be the Model/Dal layer)? If so, I need to work with this layer inside MVC's layers, and just not sounds good to me Controller and View having authority to access data objects. – Wagner Leonardi Sep 13 '13 at 14:50
  • Used that way, EF is indeed an hybrid layer combining DAL and model. I don't know much about MVC but to my understanding, the "model" part of MVC covers all the data part of the system in general. It includes representation and operations (like retrieval or modification). Therefore I would see it as the model and BLL layers of a multilayer architecture. – Marshall777 Sep 13 '13 at 15:14
  • This is all true, but in a big project, it's insane doing all stuff just into Model "namespace". Even in smaller projects that uses just **Controller**, **Model** and **View** layers, a lot of people put **Model** into a different project from **View/Controller**. – Wagner Leonardi Sep 13 '13 at 16:10
0

I use a view model in my local project and the models in the other project. Then put references to the models im gonna use on the page in my view model. Then reference the view model on my page. Let me know if that sounds like something you want to do and I can edit in some code.

Ryan Schlueter
  • 2,223
  • 2
  • 13
  • 19
  • I do the same, but the deal is: If I put Entity Framework (database first) in a Dal layer ,EF create the models in Dal. How do I work with these models in Dal? I can't access these models directly in View model. – Wagner Leonardi Sep 13 '13 at 14:36