本文介绍了设计DAL和BLL-相关表的单个/多个数据存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在设计新的多层应用程序时,我面临着难以确定我的 DAL 和 BLL 层设计的决定。While designing a new multi-tier application, I am facing difficulty making a decision for my DAL and BLL layer design.假设我将Employee信息散布在与主表具有1-1和1-Many关系的多个表中。下面列出了一些:Suppose I have Employee information spread in multiple tables having both 1-1 and 1-Many relationship with master table. Few are listed below: Employee(主表), Employee_Contact_Detail, Employee_Education, Employee_Skill, Employee_ExperienceEmployee (Master Table),Employee_Contact_Detail,Employee_Education,Employee_Skill,Employee_Experience在DAL级别,我有一个通用的数据存储库,它提供了诸如GetAll,GetSingle,Add,Edit,Delete的通用功能每个表。At DAL level, I have a generic data repository providing common functionality like GetAll, GetSingle, Add, Edit, Delete for each table.现在,我应该设计从通用数据存储库派生的员工数据存储库,并在单个类(如GetEmployeePersonalDetail)中为上面列出的所有相关表添加函数, GetEmployeeContactDetail,GetEmployeeEducation,AddEmployeePersonalDetail,EditEmployeePersonalDetail等。这样,我从通用数据存储库中获得的收益将大大减少。另一种方法是,我为每个表创建一个通用数据存储库(并从通用存储库派生),然后在业务逻辑层为雇员创建一个类。Now, should I design my "Employee Data Repository" derived from my "Generic Data Repository" and add functions for all related tables listed above in a single class like GetEmployeePersonalDetail, GetEmployeeContactDetail, GetEmployeeEducation, AddEmployeePersonalDetail, EditEmployeePersonalDetail etc. In this way I would gain very less benefit for "Generic Data Repository". The other way is that I create (and derive from Generic Repository) a separate data repository for each table and then create a single class at business logic layer for "Employee". 编辑如果我选择每个表的单独数据存储库选项,请选择DAL级别,而不是单个业务逻辑如果我创建与每个数据存储库相对应的单独的业务逻辑类,那么这是否是不雅的方法?If I go for the "separate data repository for each table" option, at DAL level and then instead of "single business logic class" for "Employee", if I create separate business logic classes, corresponding to each data repository, will it be indecent approach keeping in view the scenario?非常感谢您的指导。推荐答案如果您已经有基本存储库,为什么要为每个表创建特定的存储库?同样,在BLL中,您将为每个存储库创建单独的 Service 类(如您提到的那样,称为业务逻辑类)。这都是重复的工作,很难管理和理解。If you already have base repository, why create specific repository for each table? Again, in BLL, you are creating separate Service class ("business logic class" as you mentioned) for each repository. This is all a repeat work and difficult to manage and understand. 您有没有提到您正在使用什么ORM(如果有);因此,我假设您的ORM提供了实现以下体系结构所需的必要功能。 我建议您在通用存储库中关闭DAL。不要为特定的表编写DAL类。您所有的服务类都将对基本CRUD功能使用通用DAL。所有扩展功能都应在Service类本身中实现。You have not mentioned what ORM (if any) you are using; so I assume your ORM provides necessary features to implement below architecture.I suggest you close your DAL at generic repository. Do not write DAL classes for specific tables. All your Service classes will use generic DAL for basic CRUD functions. All extended functionality should be implemented in Service class itself.关于服务类,而不是为每个存储库/表创建一个类,都是重复工作。最好在一个服务中将相关的多个表(以上提到的存储库毫无疑问)组合在一个服务中。About service classes, instead of creating one class for each Repository/Table is again repeat work. Better group your related multiple tables (Repository is out of question with reference to above paragraph) in one service.例如,您创建通用DAL,该DAL为所有表提供基本功能表。您创建 EmployeeService 来涵盖所有与员工相关的表。您创建 AccountService 来涵盖所有与会计相关的表。同样,对于所有与日志记录相关的活动, LogService 。当您将所有相关成员归为一类时,这使与服务的交互变得容易。For example, you create generic DAL that serves basic functionality for all tables. You create EmployeeService that covers all your employee related tables. You create AccountService that covers all your accounting related table. Similarly, LogService for all logging related activities. This makes easy to interact with services as you get all related members in one class. This also reduces repeat work.请参阅我的此和此问题及其接受的答案。Refer my this and this questions and their accepted answers.我希望这可以解释您的如您的评论所述,您是EF。它支持实现我建议的必要功能。As mentioned in your comment, you us EF. It supports necessary features to implement what I suggested. 在BLL中代替DAL实现扩展功能将重叠层角色 implementing extended functionality in BLL instead of DAL will overlap the tier roles是;是的。另一面也一样。当为每个表编写DAL时,实际上是在DAL中泄漏业务逻辑。如果业务逻辑发生任何更改,则需要修改没有意义的DAL。 在我的建议中,即使各层重叠,关注点仍然是分开的。 DAL仅处理CRUD。 BLL处理所有逻辑,包括数据库逻辑。Yes; it does. Other side is also same. When you write DAL for each table, you are actually leaking Business Logic in DAL. In case any change in Business Logic, you need to modify DAL that does not make sense.In my suggestion, even though layers are overlapping, concerns are still separate. DAL only handles CRUD. BLL handles all logic including Database logic. 您是否在任何项目中使用相同的策略? Have you used same strategy in any of your projects?是。在之前的一些项目中,我为每个表创建了单独的DAL,而不是通用DAL。这引起了巨大的重复工作。尽管项目规模较小,但维护费用却更多。几个月前,我从一个相对较大的项目开始,按照上面的建议实施了该项目。我们可以看到现在给我们的救济。Yes. In some projects earlier, I was creating separate DAL for each table instead of generic DAL. This caused huge repeat work. Even though projects were relatively small, maintenance overhead was more. Few months back, I started with relatively big project where I implemented what I suggested above. We can see the relief that gave to us now. 这篇关于设计DAL和BLL-相关表的单个/多个数据存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 13:26