简体   繁体   English

业务层立面与混合业务组件

[英]Business Layer Facade vs Mingled Business Components

I'm currently designing the foundation for a large application. 我目前正在为大型应用程序设计基础。 We are going with the traditional 3 tier system using EF in the data layer, plain jane c# classes in the business layer and MVC / WCF for the ui layer. 我们将使用在数据层使用EF的传统3层系统,在业务层使用纯jane c#类,在ui层使用MVC / WCF。 We have prototyped enough of the application to realize that this will work for us, however due to the complexity of the business requirements it will be common for some of the business components interact with one another. 我们已经对应用程序进行了足够的原型设计,以意识到这对我们有用,但是由于业务需求的复杂性, 某些业务组件相互交互是很常见的。

Consider the following two business components: 请考虑以下两个业务组件:

  • RetailManager - Deal with everything related to retail in the system RetailManager-处理系统中与零售相关的一切
  • CartManager - Deals with everything related to the shopping cart experience CartManager-处理与购物车体验有关的所有事情

The two interact, for instance, during the checkout process when an item is purchased. 例如,在购买商品的结帐过程中,两者会相互影响。 The inventory for the purchased item needs to be reduced. 购买物品的库存需要减少。

Here is my thought process so far: 到目前为止,这是我的思考过程:

  1. Let business components reference each other and ensure cyclical references never happen (CartManager references RetailManager, but never the other way). 让业务组件互相引用,并确保永远不会发生循环引用(CartManager引用RetailManager,但绝不相反)。 "Checkout" would be a method on the CartManager class, and it would call a method on the RetailManager to adjust inventory. “结帐”是CartManager类上的一种方法,它将调用RetailManager上的一种方法来调整库存。 While this will work, I'm not sure how well it will scale, and what the maintenance cost will be over time. 虽然这行得通,但我不确定它的扩展性如何,以及随着时间的推移维护成本会是多少。 It doesn't feel 100% "right" to me. 我觉得这不是100%“正确”。

  2. Create a Facade between the business components and the UI tier. 在业务组件和UI层之间创建一个Facade。 In this example, the Facade would have the checkout method and a reference to both managers. 在此示例中,Facade将具有checkout方法和对两个管理者的引用。 I like this approach more than the first, however I know that not all of my business objects will need a Facade, and I don't want to create a ton of Facade classes just to have empty pass through methods. 我比第一种更喜欢这种方法,但是我知道并不是我的所有业务对象都需要Facade,并且我不想创建大量Facade类只是为了让空的传递方法。

I'm leaning towards 2, with the caveat that I will only create facade classes where needed. 我倾向于2,但需要注意的是,我只会在需要的地方创建外观类。 The UI tier will have access to both the Facade and the business layer components and will have to know when to use which (the only part I don't like about this solution). UI层将可以访问Facade和业务层组件,并且必须知道何时使用哪个组件(这是我唯一不喜欢此解决方案的部分)。

I've done a lot of research but haven't been able to come to come up with a solution that feels completely right. 我已经做了很多研究,但是还没有想出一种感觉完全正确的解决方案。

Any thoughts on using the facade pattern in this way, or other ideas to solve the problem are welcome. 欢迎对以这种方式使用立面图案的任何想法,或其他解决问题的想法。

Thanks in advance. 提前致谢。

That's a typical problem for using manager/service classes. 这是使用管理器/服务类的典型问题。 they always tend to get bloated. 他们总是会肿。 When you come to that point it's probably better to start using commands instead. 到了这一点,最好改用命令。

The great thing since you are using an IoC is that you doesn't have to refactor all the code directly, but can do it when there is time. 自从使用IoC以来,很棒的事情是您不必直接重构所有代码,而是可以在有时间的时候进行重构。 Simply start writing commands for all new features while keeping the old architecture for everything else. 只需开始为所有新功能编写命令,同时保留所有其他旧结构。

Here is a intro to commands: http://blog.gauffin.org/2012/10/writing-decoupled-and-scalable-applications-2/ 这是命令的简介: http : //blog.gauffin.org/2012/10/writing-decoupled-and-scalable-applications-2/

And an intro to my own framework: http://blog.gauffin.org/2012/10/introducing-griffin-decoupled/ 以及我自己的框架的简介: http : //blog.gauffin.org/2012/10/introducing-griffin-decoupled/

I would tend to go with facade implementation. 我倾向于采用立面实施。

I would first ask myself, whose responsibility is it to make sure that inventory is reduced when a checkout happens? 我首先问自己,确保结账时减少库存是谁的责任? I don't think it is responsibility of CartManager to reduce the inventory. 我认为CartManager减少库存不负有责任。 I would have a third class (in your case facade) that makes sure that whenever an item is checked out by CartManager , corresponding item is reduced from inventory. 我将拥有第三类(在您的情况下为外墙),以确保每当CartManager检出某件商品时,相应的物品就会从库存中减少。

Another option I would consider is event based implementation. 我会考虑的另一种选择是基于事件的实现。 CartManager would raise a ItemCheckedOut event whenever an item is checked out. 每当检出项目时, CartManager都会引发ItemCheckedOut事件。 RetailManager would subscribe to this event and would reduce the inventory whenever an event is raised. RetailManager将订阅此事件,并在发生事件时减少库存。 If you are new to event driven design, follow this question on quora - http://www.quora.com/What-are-some-good-resources-on-event-driven-software-design 如果你是新的事件驱动设计,遵循在Quora上这个问题- http://www.quora.com/What-are-some-good-resources-on-event-driven-software-design

我个人喜欢CQRS的模式,它很自然地适合其他架构模式,例如事件源,并且适用于复杂领域。

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

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