[英]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:问自己几个问题:
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:您访问数据的方式没有问题,但是:
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"?检查这篇文章: “编程到接口”是什么意思?
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检查这篇文章: 依赖注入和依赖倒置之间的区别
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 支持的语言)中的存储库设计模式使用类似集合的接口在domain
和data 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:最后但并非最不重要的一点是,一般来说,有一些经过充分验证的架构模式很高兴知道你是否认真对待这段旅程:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.