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

发布于 2025-01-31 03:25:37 字数 2912 浏览 4 评论 0原文

我正在学习实体框架,根据我的搜索,我在BLL和DAL之间有点混淆,我发现实体框架是DAL。

在下面创建BLL和DAL的方法有两种:

第一种方法:为每个对象编写一个单独的DAO(包括添加,删除,Findall,...)。在BLL中,将调用DAO获取数据或修改必要的数据。

我有student ManagementdbContext继承并放置在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:必要时查询和修改数据。

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:致电StudentDao处理业务,然后将数据返回到查看。

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);
    }
}

另一种方法:我不必为每个对象创建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();
    }
}

哪个更好,是否遵循3层规则?

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.

There are two ways to create BLL and DAL below:

First approach: write a separate DAO for each object (including add, remove, findAll, ...). In the BLL will call the DAO to get the data or modify the necessary data.

I have StudentManagement which inherits from DbContext and placed in the 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.

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦归所梦 2025-02-07 03:25:38

尽管您访问数据的方式没有错,但是可以作为最佳实践提供更好的方法。但是,在计划任何特定的软件体系结构之前,您应始终考虑项目的类型。

问自己几个问题:

  • 这个项目会随着时间的推移而增长,还是应用一些简单逻辑的简单项目?
  • 该项目有多少开发人员将在该项目上工作?

我相信这两个简单的问题可以指导您更容易确定项目的架构。

现在关于您的问题:

哪个更好,是否遵循3层的规则?

您访问数据的方式没有错,但是:

  1. 程序到接口。

使用接口是使您的代码轻松可测试并在类之间删除不必要的耦合的关键因素。
查看这篇文章:对“对接口进行程序”的含义是什么?

  1. 依赖关系反转&amp;依赖注入

理解这两者的含义并知道差异可以帮助您大大帮助您。

查看这篇文章:依赖关系注入和依赖性反转之间

差异C#中的存储库设计模式(或任何OOP支持的语言)在数据映射层之间使用类似集合的接口来访问域对象。换句话说,我们可以说存储库设计模式充当应用程序其余部分和数据访问逻辑之间的“中间层”。

我相信这是检查和学习的一个很好的例子: c#中的存储库模式示例

最后但并非最不重要的一点,通常有一些良好的架构模式,很高兴知道您是否在此旅程中很认真:

  • 域驱动设计(DDDDD) )
  • 微服务体系结构模式

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?

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.
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. 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#

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)
  • Microservices Architecture Pattern
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文