MVC, Entitiy Framework and Repository

Posted on 2011-04-30
Last Modified: 2013-12-16
Hi experts

I'm building my first MVC app, now I have some questions about how to separate the different parts of the app.

So far I have three projects
1. the mvc app
2. the "Data-Project" with an Entity Framework Model and ObjectContext
3. the "Entity-Project" consisting of POCOs

So, to get my app to work I'll have to reference both the Data-Project and the Entity-Project, right?
Or am I getting something wrong?

I am using VS2010 and .NET 4.0
Question by:Arikael
    LVL 40

    Assisted Solution

    If you organize the projects like you mention, then yes, you have to reference both projects from the MVC app.

    More typically people don't separate their entities (POCOs) from the data access layer, those are usually one combined project. Only for large apps that may have several backends / frontends would it be worth the trouble to break the entities out from the data layer. But either way, not a major issue.

    LVL 5

    Accepted Solution

    I would like to add something more.

    1. Data Layer - Access to database.
    2. Object/Entity Layer - Having entities those will be used in the business logic.
    3. Business Layer - Have all the business logic.
    - Has reference of 1 & 2
    4. ASP.NET MVC layer - Presentation logic
    - Has reference of 2 & 3.
    NOTE: Never use the Entities, declared in layer 2, to create strongly typed views.
    5. Service Layer - Used for exposing your business logic/features to third parties.
    - Has reference of 2 & 3.

    Hope this will help you.
    LVL 3

    Author Comment

    thanks for your answers

    If I would do it like you suggested, wouldn't the data access layer and the entities be very tight coupled?
    LVL 3

    Author Comment

    "NOTE: Never use the Entities, declared in layer 2, to create strongly typed views."

    Why shouldn't I do that?

    thanks for your reply :)
    LVL 5

    Expert Comment

    by:Vipul Patel
    Because sometime it requires to manipulate the fields.

    Let me give you simple example;
    In a table, you may save First Name and Last Name separately, but requirement says it would be Firat Name and first character of Last Name.

    In the Entity it might be look like;
    public class Person{
         public string FirstName {get;set;}
         public string LastName{get;set;}

    Public class PersonViewModel{
           Person p;
           public void Retrieve(int id)
               //BL code that returns Person object
          public PersonName {
                        return string.Format("{0} {1}",p.FirstName,p.LastName[0]);

    NOTE: This is the very simple example.
    LVL 5

    Expert Comment

    by:Vipul Patel
    The ultimate goal is that code should accept changes very easily and rapidly.

    You can use your database entities in the views. But I still recommend  to make it separate from entity layer.
    LVL 3

    Author Comment

    ok, so basically I would have to create a Business-Layer-Class for every entity and work with this one?

    validation also comes into the business-layer (leave the entities untouched), right?

    what is the difference between the business-layer and the service layer, isn't that kinda the same?

    LVL 3

    Author Comment

    I decided to close the question because the initial question has been answered.

    I created a new question which addresses some unanswered/unclear topics.

    thanks for your help

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now