简体   繁体   English

将 Entity Framework 6 项目分层

[英]Separating Entity Framework 6 projects into layers

I am going through a tutorial on Entity Framework 6 and trying to implement this into a test project.我正在阅读有关 Entity Framework 6 的教程,并尝试将其实现到测试项目中。 This would be targeting ASP.Net webforms but i think the principle would be the same for MVC projects.这将针对 ASP.Net 网络表单,但我认为 MVC 项目的原理是相同的。

I have 3 projects DAL, BAL and Webforms project.我有 3 个项目 DAL、BAL 和 Webforms 项目。

I create a Class Library called DAL, Install EF 6. Add ADO.Net Entity Data Model, link to the database.我创建了一个名为 DAL 的 Class 库,安装 EF 6。添加 ADO.Net 实体数据 Model,链接到数据库。 This has 2 tables for ease Customer and Addresses.这有 2 个表格以方便客户和地址。 The connection string is stored in the app.config file.连接字符串存储在 app.config 文件中。

Table 1 (Customers)- Id, Firstname, Surname
Table 2 (Addresses)- Id, PrimaryAddress, CustomerId
  1. I see a.tt file and expanding it i see all the classes resembling my database tables.我看到一个.tt 文件并展开它,我看到所有类似于我的数据库表的类。 If i want to add another method ie如果我想添加另一种方法,即

    Public string GetCustomerFullName { return Firstname + " " + Surname; }

Where and how would i do this on the DAL project?我将在哪里以及如何在 DAL 项目中执行此操作? I was thinking of the Class created when expanding the.tt file (ie Customer class) but i have a sneaky feeling this may get overridden if i was to regenerate /refresh some part of the ADO.Net Entity Data Model file, eg if a table is added or something?我正在考虑扩展.tt文件(即客户类)时创建的Class,但我有一种偷偷摸摸的感觉,如果我要重新生成/刷新ADO.Net实体数据Model文件的某些部分,这可能会被覆盖,例如,如果表是添加还是什么?

  1. I create another Class Library called BAL (this is where i would like to call all of the CRUD operations).我创建了另一个名为 BAL 的 Class 库(这是我想调用所有 CRUD 操作的地方)。 Install EF6 from Nuget.从 Nuget 安装 EF6。 I add reference to the DAL project.我添加了对 DAL 项目的引用。 Currently the tutorial adds a new Dbcontext instance for every method ie Add, Update etc within the same project.目前,本教程为同一项目中的每个方法(即添加、更新等)添加了一个新的 Dbcontext 实例。

How do i reference the DbContext created in DAL project in this project?我如何在这个项目中引用在 DAL 项目中创建的 DbContext? I've read a mixture of threads that a new context could give odd behaviour where others feel reusing the same context is/not a good idea.我已经阅读了混合线程,新上下文可能会产生奇怪的行为,而其他人认为重用相同的上下文是/不是一个好主意。 Could someone show me how to reference the DbContext in the correct way.有人可以告诉我如何以正确的方式引用 DbContext。 I normally have an abstract class which is then added to my classes eg我通常有一个抽象的 class 然后添加到我的类中,例如

    public class CustomerService : DbContxtService, ICustomer
    {
     public void Add(Customer c)
    {
    // Add customer
    }
    }
  1. How could set the DAL project to retrieve the connection string from my Web forms project (so that i dont need to always change the Class Library and could have one area to change the connection string)如何设置 DAL 项目以从我的 Web forms 项目中检索连接字符串(这样我就不需要总是更改 Class 库并且可以有一个区域来更改连接字符串)

If i want to add another method.. where would I do it.如果我想添加另一种方法.. 我会在哪里做。

The entity definitions are partial classes .实体定义是部分类 You can add members to the class in a separate file that wouldn't be overwritten.您可以在不会被覆盖的单独文件中将成员添加到 class。

How do i reference the DbContext created in DAL project in this project?我如何在这个项目中引用在 DAL 项目中创建的 DbContext?

You set a reference to the project and add a constructor dependency for each service on your (single) DbContext.您设置对项目的引用并为(单个)DbContext 上的每个服务添加构造函数依赖项。

eg例如

public class CustomerService 
{
    private MyDbContext db;
    public CustomerService(MyDbContext db)
    {
       this.db = db;
    {
    public void CreateNewCustomer(Customer c)
    {
       //CRUD and business logic here
       ...
       db.SaveChanges();
    }
    public Customer GetCustomerWithHistory(int customerId)
    {
       db.Customer.Include("SalesOrders").FirstOrDefault( c => c.Id = customerId);
    }

    public void CreateCustomerServiceTicket(Customer c, Incident i)
    {
    }
}

Note this should be a business service, not a repository.请注意,这应该是业务服务,而不是存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM