none
How do you make a layered solution's UI project resolve Entity Framework Entities without referencing the data access layer RRS feed

  • Question

  • I am working on a WinForms application which I have structured with 3 layers where each layer is a separate project as shown below:



    Within the SampleNtierDAL project is a DalServices class which is defined as follows:

    namespace SampleNtierDAL
    {
        public class DalServices
        {
            public static List<Employee> GetEmployees()
            {
                List<Employee> employeeList = null;
                using (SampleNtierEntities aSampleNtierEntitiesDbContext = new SampleNtierEntities())
                {
                    employeeList = (from emp in aSampleNtierEntitiesDbContext.Employees select emp).ToList();
                }
                return employeeList;
            }
        }
    }

    Within the SampleNtierBLL project is a BllServices class defined as follows:

    namespace SampleNtierBLL
    {
        public class BllSerices
        {
            public static List<Employee> GetEmployees()
            {
                return DalServices.GetEmployees();
            }
        }
    }

    The SampleNtierUI project has a WinForm button event which is supposed to ask the BllServices to get an employee list as follows:

    namespace SampleNtierUI
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void btnGetListOfEmployees_Click(object sender, EventArgs e)
            {
                List<Employee> anEmployeeList = BllSerices.GetEmployees();
            }
        }
    }

    The problem here is that the WinForm is not able to see the entity model called "Employee". The entity "Employee" was created using the Visual Studio 2015 template called "Ado.net Entity Data Model" which generated the Employee class as part of the "Data Base First" EF approach.  The SampleNtierBLL project class BllServices sees the class because it references the SampleNtierDAL and has a use statement at the top to resolve the Employee class. I thought about adding similar references in the WinForm to resolve the Employee class but I have seen in other articles that you are not supposed to make references to the Data Access Layer but such articles don't show how to address this problem I have.

    I tried regenerating the Employee entity using the same VS Template as before hoping to find an option to output the entities in a separate file which would solve this problem but I saw no such option.

    Can someone please show me how to get the Entity model (Employee class) recognized by the UI (WinForm) without making references to the data access layer where the Entity Framework generated the Entity called Employee? Thanks in advance.
    Thursday, November 5, 2015 12:07 AM

Answers

  • I found an answer to this problem at this link:

    http://stackoverflow.com/questions/33527484/how-do-you-make-a-layered-solutions-ui-project-resolve-ef-entities-without-refe/33529691?noredirect=1#comment54930166_33529691

    • Marked as answer by zonkerman Saturday, November 7, 2015 2:31 AM
    Saturday, November 7, 2015 2:31 AM

All replies

  • Hi zonkerman,

    According to your description, I suggest you create a new layer/project called Entity Layer. Move edmx file to this project. Because entity classes are used in the rest of three projects, we need to make it independent. And it will be referenced by the rest of three pojects.

    Best Regards,
    Jerry


    • Edited by Jerry Wa Tuesday, November 17, 2015 6:40 AM
    Thursday, November 5, 2015 2:12 AM
  • I tried drag dropping the .edmx object in the solution explorer from the DAL project to a new project I created called the ModelEmployee project. The problem with that was it brought over all the edmx objects such as context, designer, diagram plus the entities. I only want to expose the entities and none of the database related objects like context; those can stay in the DAL layer.

    What would really be super is if Visual Studio had an option while creating EF for Database First to allow the output of the entities only in a separate project and have the rest of edmx operations land in the project where you said "add new item". 

    If Visual Studio did this then I could just take the entity only project and reference it so the entities would be resolved from any business logic layer that uses entities like employees.  The DAL with the imported EF operations should also use the newly created project with entities only so there would not be duplication of entities.  Even the UI could use the separate entities project.

    At this point I'm beginning to think this can't be done using the wizard templates for EF but certainly can be done if I make the EF projects and classes manually without the wizards.  Seems like I just need a project that creates and uses a DBContext and have all the services in that project for database operations.  If done by hand then I can manually create a separate project and create the entities in there and have the DAL Layer reference that project to resolve entities like Employee.  Actually any layer in the solution could use the entities only project if it had to resolve an entity to get a task done.

    I'm hoping for an easier solution before I get a manual alternative implemented.

    Thursday, November 5, 2015 3:46 AM
  • Hi zonkerman,

    Due to your database has been created, I suggest you using Code First development targeting an existing database. Link below is for your reference. It can generate POCO classes for you. Then you could separate entity models from database operations classes.
    https://msdn.microsoft.com/en-us/library/jj200620.aspx

    Best Regards,
    Jerry


    • Edited by Jerry Wa Tuesday, November 17, 2015 6:42 AM
    Friday, November 6, 2015 7:49 AM
  • I found an answer to this problem at this link:

    http://stackoverflow.com/questions/33527484/how-do-you-make-a-layered-solutions-ui-project-resolve-ef-entities-without-refe/33529691?noredirect=1#comment54930166_33529691

    • Marked as answer by zonkerman Saturday, November 7, 2015 2:31 AM
    Saturday, November 7, 2015 2:31 AM