简体   繁体   English

n层业务/服务层设计

[英]n-layer business/service layer design

I'm trying to reevaluate our n-layer architecture and would love to get some suggestions based on your experiences. 我正在尝试重新评估我们的n层架构,并希望根据您的经验获得一些建议。 Here is our typical .NET n-layer (sometimes n-tier) design. 这是我们典型的.NET n层(有时是n层)设计。

Project.UI
Project.Services
Project.Business
Project.Model
Project.DataAccess

DataAccess typically consists of Entity Framework 4 and Repository classes. DataAccess通常由Entity Framework 4Repository类组成。 I attempt to follow the Aggregate Root concept in order to avoid having a repository for table, easier said than done in my experience. 我试图遵循Aggregate Root概念,以避免有一个表的存储库,说起来比我的经验更容易。 I tend to have ~70% match between Repositories and Tables. 我倾向于在存储库和表之间匹配约70%。

Model usually consists of my Entity Framework 4 entities, I've been using Self-Tracking EF entities with success. 模型通常由我的Entity Framework 4实体组成,我一直在使用自我跟踪EF实体成功。

Business is what I struggle with the most. 商业是我最挣扎的事情。 I typically have a Manager class for every Repository . 我通常为每个Repository都有一个Manager类。 This class will contain methods like .Add() which will perform business validation before forwarding down to repository.Add(). 此类将包含.Add()等方法,这些方法将在转发到repository.Add()之前执行业务验证。

Services, typically I will only implement this if in fact I am looking to create a web service based solution. 服务,通常我只会实现这个,但实际上我正在寻找创建基于Web服务的解决方案。 This layer will be tasked with marshaling requests/responses between DTOs and entities. 该层的任务是在DTO和实体之间编组请求/响应。 And most importantly provide the more coarse grained interface. 最重要的是提供更coarse grained界面。 For example a TradingService.SubmitTrade(), which is really a facade for a business transaction which might include AccountManager.ValidateCash(), OrderManager.SubmitOrder(), etc. 例如,TradingService.SubmitTrade(),它实际上是业务事务的facade ,可能包括AccountManager.ValidateCash(),OrderManager.SubmitOrder()等。

Concerns 关注
My business layer is very entity centric, really it's just the glue between the entities and the repository, with validation in between. 我的业务层是以实体为中心的,实际上它只是实体和存储库之间的粘合剂,两者之间进行了验证。 I've seen many designs where the Service Layer is what holds a reference to the repositories (in essence skipping the "business layer"). 我见过许多设计,其中服务层是对存储库的引用(实质上是跳过“业务层”)。 In essence it serves the same purpose as my Business layer, it does the validation, however its' responsibility (and naming) is a higher level, more coarse grained business transaction. 本质上,它与我的业务层具有相同的目的,它进行验证,但其责任(和命名)是更高级别,更粗粒度的业务事务。 Using the example above the TradingService.submitTrade() will not delegate to any business manager classes, it would itself query the necessary repositories, perform all the validation etc. 使用上面的示例,TradingService.submitTrade()不会委托给任何业务经理类,它本身会查询必要的存储库,执行所有验证等。

I like my design in a sense that I can reuse a business layer method in multiple service calls, however I hate the fact that for every repository I have a matching business layer manager, creating tons of extra work. 我喜欢我的设计,因为我可以在多个服务调用中重用业务层方法,但是我讨厌这样一个事实:对于每个存储库,我都有一个匹配的业务层管理器,创建了大量的额外工作。 Maybe the solution is a different type of grouping at the Business Layer level? 也许解决方案是业务层级别的不同类型的分组? For example combine individual Manager classes like PhoneManager and EmailManager (note I have Phone entities and Email entities) into a logical Manager class such as ContactsManager (note I don't have a "Contact" entity type). 例如,将各个Manager类(如PhoneManager和EmailManager(请注意我有Phone实体和Email实体))组合到逻辑Manager类(如ContactsManager)中(注意我没有“Contact”实体类型)。 With methods such as ContactManager.GetPhones() and ContactManager.GetEmail(), etc. 使用ContactManager.GetPhones()和ContactManager.GetEmail()等方法。

I guess more than anything I am wondering how others organize and delegate responsibilities, whether they have the Service layer, Business layer, both, etc. What holds the ORM context reference, etc. 我想,我想知道其他人如何组织和委派职责,无论他们是否有服务层,业务层,两者等等。什么是ORM上下文引用等等。

I tend to do what you outlined near the end of your concerns, and at the business layer group things into managers that make more logical sense from a business point of view. 我倾向于在您关注的最后阶段完成您所概述的内容,并且在业务层将事物分组到管理者中,从业务角度来看更具逻辑意义。

Using Contacts for example, I certainly wouldn't have a PhoneManager or EmailManager. 例如,使用Contacts,我当然不会有PhoneManager或EmailManager。 "ContactsManager" is a more useful grouping to me, accomplishing pretty much the same thing only with a lot fewer managers to deal with. “ContactsManager”对我来说是一个更有用的分组,只有很少的管理者才能完成同样的事情。 From a business point of view, phone numbers and emails are just small pieces of a Contact anyway. 从商业角度来看,电话号码和电子邮件只是联系人的一小部分。 Just because they have their own tables and entities doesn't mean they need their own manager. 仅仅因为他们有自己的表和实体并不意味着他们需要自己的经理。 That doesn't mean that you can't reuse them. 这并不意味着你不能重复使用它们。 If you have a need to do work with email addresses in several places, ContactsManager can have the relevant methods. 如果您需要在多个地方使用电子邮件地址,ContactsManager可以使用相关方法。

What we tend to have in our environment is a database/entity layer, then a business layer that sits on the server and handles business rules and business logic. 我们在环境中往往拥有的是数据库/实体层,然后是位于服务器上并处理业务规则和业务逻辑的业务层。 That business layer is exposed as a service via WCF to the client (or at least, stuff relevant to clients is). 该业务层通过WCF作为服务公开给客户端(或者至少是与客户相关的东西)。

It sounds like you already know what you want to do, so I'd suggest doing a bit of prototyping work on it and see how it works for you. 听起来你已经知道你想要做什么,所以我建议做一些原型设计工作,看看它是如何为你工作的。 :) :)

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

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