简体   繁体   English

使用实体框架时如何创建 BLL(业务逻辑层)?

[英]How to create BLL (Business logic Layer) when using Entity Framework?

I'm learning Entity Framework, I was a bit confused between BLL and DAL, according to my search, I found that Entity Framework is DAL.我正在学习Entity Framework,我对BLL和DAL有点困惑,根据我的搜索,我发现Entity Framework是DAL。

There are two ways to create BLL and DAL below:下面有两种创建 BLL 和 DAL 的方法:

First approach : write a separate DAO for each object (including add, remove, findAll, ...).第一种方法:为每个对象编写一个单独的 DAO(包括 add、remove、findAll、...)。 In the BLL will call the DAO to get the data or modify the necessary data.在 BLL 中会调用 DAO 来获取数据或修改必要的数据。

I have StudentManagement which inherits from DbContext and placed in the DAL.我有StudentManagement ,它继承自DbContext并放置在 DAL 中。

public partial class StudentManagement : DbContext
{
       public StudentManagement()
            : base("name=StudentManagement")
       {
       }

       public virtual DbSet<LOP> LOP { get; set; }
       public virtual DbSet<STUDENT> STUDENT { get; set; }

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Entity<LOP>()
                       .HasMany(e => e.STUDENT)
                       .WithOptional(e => e.LOP)
                       .HasForeignKey(e => e.CLASS_ID);
       }
}

StudentDAO : query and modifying data if necessary. StudentDAO :必要时查询和修改数据。

class StudentDAO
{
    public StudentManagement context { get; set; }

    public StudentDAO()
    {
        context = new StudentManagement();
    }

    public IQueryable<STUDENT> findAll()
    {
        return context.STUDENT;
    }

    public void add(STUDENT student)
    {
        context.STUDENT.Add(student);
        context.SaveChanges();
    }

    public void remove(int id)
    {
        STUDENT student = context.STUDENT.Find(id);

        if (student != null)
        {
            context.STUDENT.Remove(student);
            context.SaveChanges();
        }    
    }
}

Student_BLL : call the StudentDAO to handle business and then return data to view. Student_BLL StudentDAO调用Student_BLL处理业务,然后返回数据查看。

class StudentBLL
{
    public List<STUDENT> getStudentInClass(int ClassID)
    {
        return new StudentDAO().findAll().Where(student => student.CLASS_ID == ClassID).ToList();
    }

    public List<STUDENT> findAll()
    {
        return new StudentDAO().findAll().ToList();
    }

    public STUDENT find(int id)
    {
        return new StudentDAO().findAll().FirstOrDefault(student => student.ID == id);
    }

    public void add(STUDENT student)
    {
        new StudentDAO().add(student);
    }

    public void remove(int id)
    {
        new StudentDAO().remove(id);
    }
}

Another approach : I don't have to create DAO for each object but use context in BLL and query directly using LINQ.另一种方法:我不必为每个对象创建 DAO,而是在 BLL 中使用上下文并使用 LINQ 直接查询。

class LopSH_BLL
{
    public StudentManagement context { get; set; }

    public LopSH_BLL()
    {
        context = new StudentManagement();
    }

    public List<LOP> findAll()
    {
        return context.LOP.ToList();
    }

    public LOP find(int id)
    {
        return context.LOP.Find(id);
    }

    public void add(LOP lop)
    {
        context.LOP.Add(lop);
        context.SaveChanges();
    }

    public void remove(int id)
    {
        LOP lop = context.LOP.Find(id);
        context.LOP.Remove(lop);
        context.SaveChanges();
    }
}

Which is better and does it follow the rules of 3 layers?哪个更好,它是否遵循 3 层的规则?

Although there is nothing wrong with the way you are accessing data, there are better approaches available as best practices.尽管您访问数据的方式没有任何问题,但有更好的方法可用作最佳实践。 However, you should always consider the type of project before planning any specific software architecture.但是,在规划任何特定的软件架构之前,您应该始终考虑项目的类型。

Ask yourself a few questions:问自己几个问题:

  • Is this project going to grow over time, or it's just a simple project to apply some simple logic?这个项目会随着时间的推移而增长,还是只是一个应用一些简单逻辑的简单项目?
  • How many developers are going to work on the project?有多少开发人员将在该项目上工作?

I believe these two simple questions can guide you much more accessible to deciding the architecture of your project.我相信这两个简单的问题可以指导您更容易地决定项目的架构。

Now regarding your question:现在关于你的问题:

Which is better, and does it follow the rules of 3 layers?哪个更好,是否遵循 3 层的规则?

Nothing wrong with the way you are accessing data, but:您访问数据的方式没有问题,但是:

  1. Program to the interfaces.编程到接口。

Using interfaces is a crucial factor in making your code easily testable and removing unnecessary couplings between your classes.使用接口是使代码易于testable并消除类之间不必要的couplings的关键因素。 Check this post : What does it mean to "program to an interface"?检查这篇文章: “编程到接口”是什么意思?

  1. Dependency Inversion & Dependency Injection依赖倒置和依赖注入

Understanding the meaning of these two and knowing the differences can help you so much down the road.了解这两者的含义并了解它们之间的差异可以为您提供很多帮助。

Check this post: Difference between dependency injection and dependency inversion检查这篇文章: 依赖注入和依赖倒置之间的区别

  1. Repository Pattern存储库模式

The Repository Design Pattern in C# (or any OOP-supported language) mediates between the domain and the data mapping layers using a collection-like interface for accessing the domain objects. C#(或任何 OOP 支持的语言)中的存储库设计模式使用类似集合的接口在domaindata mapping layers之间进行调解,以访问域对象。 In other words, we can say that a repository design pattern acts as a "middle layer" between the rest of the application and the data access logic.换句话说,我们可以说存储库设计模式充当应用程序其余部分和数据访问逻辑之间的“中间层”。

I believe this is an excellent example to check and learn: The Repository Pattern Example in C#我相信这是一个很好的检查和学习示例: The Repository Pattern Example in C#

Last but not least, there are some well-proven architecture patterns in general which are good to know if you are serious in this journey:最后但并非最不重要的一点是,一般来说,有一些经过充分验证的架构模式很高兴知道你是否认真对待这段旅程:

  • Domain Driven Design (DDD)领域驱动设计 (DDD)
  • Microservices Architecture Pattern微服务架构模式

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

相关问题 当使用实体框架作为数据访问层时,如何实现业务逻辑层? - How do you implement a business logic layer when using entity framework as data access layer? 使用实体框架时,业务逻辑层中的数据对象 - Data Objects in Business Logic Layer when using Entity Framework 使用Entity Framework时实现业务逻辑 - implementing business logic when using Entity Framework 在业务逻辑层中使用Entity Framework生成的类 - Using Entity Framework generated classes in Business Logic Layer 如何使用实体框架实现业务逻辑? - How to implement business logic using Entity Framework? 如何从业务层使用实体框架更新实体? - How to update an entity using Entity Framework from Business Layer? 使用实体框架时关于将业务逻辑放在哪里的困惑 - Confusion about where to put business logic when using Entity framework WCF和DI和业务逻辑层,如何公开BLL的所有方法,最佳实践 - WCF and DI and Business logic layer, How to expose all methods of BLL, Best practice 在单独的数据访问和业务逻辑层中,我可以在业务层中使用Entity框架类吗? - In separate data access & business logic layer, can I use Entity framework classes in business layer? 在使用Linq To SQL时,我是否应该在BLL类中使用数据访问和业务逻辑 - Should I be using Data Access and Business Logic inside my BLL class when using Linq To SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM