locked
Seperate Project for EF RRS feed

  • Question

  • I am following a example on creating a ASP web app using the entity framework, but I have a database already created I would like to access.

    I would like to use the entity framework classes in a couple of projects.  Is it considered a good practice to put the entity framework. code in a separate project?

    Do you put all the controller code in the entity framework project or in the web portion of the project?

    When creating the project for the entity framework would I still use a web project template or would I use a DLL template?

    Thanks in advance.

    Thursday, January 8, 2015 1:33 AM

Answers

  • Hello Kazinski

    >>Is it considered a good practice to put the entity framework. code in a separate project?

    Personally, I would like do that puts data access code to a separate project which would make us easy to manage it.

    >>Do you put all the controller code in the entity framework project or in the web portion of the project?

    I do not quite understand what you mean about the controller code, if you mean business code, I would put them into a separate layer which we could call it business layer. It is not recommended to put business code and data access part code together.

    >>When creating the project for the entity framework would I still use a web project template or would I use a DLL template?

    If you are creating a MVC project, this project has already helped us constructed a separated layer. If it is just a common web application, you could create a class library project for your Entity Framework, and note do not forget to copy the connection string in your class library project to your web application or it would throw an exception.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Friday, January 16, 2015 9:45 AM
    Thursday, January 8, 2015 5:23 AM
  • You can use the Repository pattern.

    http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and-entity-framework.htm

    Or you can use the DAO pattern.

    http://cgeers.com/2009/03/14/data-access-objects-with-the-entity-framework/

    Both patterns can be used in a  classlib project called DAL (Data Access Layer), which the Web project has reference to the DAL. The controller in a MVC solution  resides in the Web project, the Web project has project reference to the DAL. The controllers call upon objects/classes in the DAL to do CRUD (create, read, update and delete) operations with the database.

    The key here with the Repository or DAO pattern is the usage of an Interface in the Repository or DAO classes/objects in the DAL, which is a contract between two objects that publicly exposes properties and/or methods implemented by the class/object that implements the interface to the object that wants to use the object that implements the interface. 

    http://www.windowsdevcenter.com/pub/a/dotnet/2003/06/30/interfaces.html

    • Marked as answer by Fred Bao Friday, January 16, 2015 9:45 AM
    Friday, January 9, 2015 3:59 PM

All replies

  • Hello Kazinski

    >>Is it considered a good practice to put the entity framework. code in a separate project?

    Personally, I would like do that puts data access code to a separate project which would make us easy to manage it.

    >>Do you put all the controller code in the entity framework project or in the web portion of the project?

    I do not quite understand what you mean about the controller code, if you mean business code, I would put them into a separate layer which we could call it business layer. It is not recommended to put business code and data access part code together.

    >>When creating the project for the entity framework would I still use a web project template or would I use a DLL template?

    If you are creating a MVC project, this project has already helped us constructed a separated layer. If it is just a common web application, you could create a class library project for your Entity Framework, and note do not forget to copy the connection string in your class library project to your web application or it would throw an exception.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Friday, January 16, 2015 9:45 AM
    Thursday, January 8, 2015 5:23 AM
  • Thanks that is the type of information I was looking for.

    Thursday, January 8, 2015 1:37 PM
  • You should understand seperation of concerns.

    http://en.wikipedia.org/wiki/Separation_of_concerns

    Even in a MVC solution, one can implement n-tier and the seperation of concerns without having data access slammed up at the controller of a MVC solution.

    http://en.wikipedia.org/wiki/Multitier_architecture

    Data access is always in the DAL. One can functional test the DAL with a test harness and test classlib project that tests the DAL without ever having to go to the UI and the controller to do testing

    Thursday, January 8, 2015 3:57 PM
  • Thanks for the information.  I understand in principle what I need to do, it is just the mechanics of getting it done I am struggling with.

    I set up 2 projects in VS2013.  The first project was created using the MVC template.  I created a second project for the data layer also using the MVC template.  I copied my database table classes into the project.  I set the application type to be a class library.

    Is this the correct way to set up the project(s)?

    When I add a controller, do I put the controller in the first or second project?

    I tried to import the second project into the first project but the import fails.

    Any links on how to would be appreciated.

    Friday, January 9, 2015 3:17 PM
  • You can use the Repository pattern.

    http://www.codeguru.com/csharp/.net/net_asp/mvc/using-the-repository-pattern-with-asp.net-mvc-and-entity-framework.htm

    Or you can use the DAO pattern.

    http://cgeers.com/2009/03/14/data-access-objects-with-the-entity-framework/

    Both patterns can be used in a  classlib project called DAL (Data Access Layer), which the Web project has reference to the DAL. The controller in a MVC solution  resides in the Web project, the Web project has project reference to the DAL. The controllers call upon objects/classes in the DAL to do CRUD (create, read, update and delete) operations with the database.

    The key here with the Repository or DAO pattern is the usage of an Interface in the Repository or DAO classes/objects in the DAL, which is a contract between two objects that publicly exposes properties and/or methods implemented by the class/object that implements the interface to the object that wants to use the object that implements the interface. 

    http://www.windowsdevcenter.com/pub/a/dotnet/2003/06/30/interfaces.html

    • Marked as answer by Fred Bao Friday, January 16, 2015 9:45 AM
    Friday, January 9, 2015 3:59 PM