去松散的耦合和依赖注射的香蕉 [英] Going bananas with loose coupling and dependency injection

查看:210
本文介绍了去松散的耦合和依赖注射的香蕉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着我们的依赖注入框架(春季注释)的最新补充,创建DI管理组件的边际成本似乎已经达到了一些关键的新阈值。虽然以前有一个与春天(吨的XML和额外的限制)有关的开销,依赖注入似乎已经开始走向许多模式去;他们在引擎盖下消失。



这样做的结果是与 大数字 可以接受。有争议的是,我们可以创建一个系统,大多数类只会暴露
一个单一的公共方法,并通过将这些片段集成到一起,构建整个系统,如疯狂。在我们这个例子中,给了几件事情。您的应用程序的用户界面具有形成最高服务的一些功能需求。后端系统控制下部。但是在这两者之间,一切都是为了抓住。



我们不断的讨论真的是为什么我们在课上分组东西和原则应该是什么?有几件事情是肯定的;门面图案已经死亡和埋葬。任何包含多个不相关功能的服务也倾向于分解。 不相关的功能的解释比我以前做的非常严格。



在我们的团队中,有两条盛行的思路:实现依赖关系限制分组;单一类中的任何功能最好应该是所有注册的依赖关系的客户端。我们是一个DDD项目,另一个部分认为域限制分组(CustomerService或更细粒度的CustomerProductService,CustomerOrderService) - 注入依赖关系的标准化使用是不重要的。



所以在松散耦合的DI宇宙,为什么我们在类中分组逻辑?



编辑:duffymo指出,这可能正在朝着编程的功能风格发展;这带来了国家的问题。我们有很多代表(小)相关应用状态的状态对象。我们将这些注入到任何对此状​​态有合法需求的服务中。 (我们使用状态对象而不是常规域对象的原因是spring会在未指定的时间构造这些对象,我认为这是一个轻松的解决方法或替代解决方案,让Spring管理域对象的实际创建,可能会有更好的解决方案所以例如任何需要OrderSystemAccessControlState的服务都可以注入这个,而这个数据的范围对消费者来说是不容易知道的。一些安全关联状态通常用于很多不同的级别,但是在其间的级别上完全不可见。我真的认为这是从根本上违背功能原则的。我甚至很难从OO角度对这个概念进行调整,但是只要注入状态是精确而强烈的类型,那么需要是合法的,因为用例是正确的。

解决方案

优秀的OO设计的首要原则不要停止在松耦合,而是高凝聚力,这被忽略在大多数讨论中。



高凝聚力


在计算机程序设计中,凝聚力是一个
衡量一个
单个模块的强度相关性或
的重点。如果应用于
面向对象编程,如果给定类

方法在许多方面往往相似,则
那么该类被认为具有高
凝聚力在一个高度凝聚的系统中,
代码可读性和
重用的可能性增加,而
的复杂性保持可管理。



如果以下情况,凝聚力下降:

  *通过其方法访问的类中嵌入的功能
几乎没有共同之处。
*方法执行许多不同的活动,通常使用粗粒度或
无关的数据集。

低凝聚力(或弱凝聚力)的缺点是:

  *增加了解模块的难度。 
*维护系统的难度增加,因为域b $ b中的逻辑更改会影响多个模块,并且因为一个模块
中的更改需要相关模块的更改。
*由于大多数应用程序
不需要模块提供的随机操作,所以重用模块的难度增加。


当人们对IoC容器疯狂时,丢失的一件事是凝聚力的丧失和可追溯性是什么以及如何做某事成为一个噩梦,以便自己解决这个问题,因为所有的关系都被一堆XML配置文件(Spring我正在看着你)和很差的实现课程。


With the latest additions to our dependency injection framework (annotations in spring), the marginal cost of creating DI-managed components seems to have hit some critical new threshold. While there previously was an overhead associated with spring (tons of XML and additional indirections), dependency injection seems to have started going where lots of patterns go; they go under the hood and "disappear".

The consequence of this is that the conceptual overhead associated with a large number of components becomes acceptable. It's arguable that we could make a system where most classes only expose one single public method and build the whole system by just aggregating these pieces like crazy. In our case a few things are given; the user interface of your application has some functional requirements that shape the topmost services. And the back-end systems control the lower part. But in between these two, everything is up for grabs.

Our constant discussion is really why are we grouping things in classes and what should the principles be ? A couple of things are certain; the facade pattern is dead and buried. Any service containing multiple unrelated features also tend to get split up. "Unrelated feature" is interpreted in an extremely much stricter sense than I have ever done earlier.

In our team there are two prevailing trains of thought here: Implementation dependencies restrict grouping; any functionality in a single class should preferably be a client of all injected dependencies. We are a DDD project and the other fraction thinks the domain restricts grouping (CustomerService or finer grained CustomerProductService, CustomerOrderService) - normalized usage of injected dependencies is unimportant.

So in the loosely coupled DI universe, why are we grouping logic in classes ?

edit: duffymo point out that this may be moving towards a functional style of programming; which brings up the issue of state. We have quite a few "State" objects that represent (small) pieces of relevant application state. We inject these into any service that has a legitimate need for this state. (The reason we use "State" objects instead of regular domain objects is that spring construct these at an unspecified time. I see this as a slight workaround or alternate solution to letting spring manage the actual creation of domain objects. There may be better solutions here).

So for instance any service that needs OrderSystemAccessControlState can just inject this, and the scope of this data is not readily known to the consumer. Some of the security-relate state is typically used at a lot of different levels but totally invisible on the levels in-between. I really think this violates fundamentally with functional principles. I even had a hard time adjusting to this concept form an OO perspective - but as long as the injected state is precise and strongly type then the need is legit aka the use case is proper.

解决方案

The overriding principals of good OO Design do not stop at loose coupling, but also high cohesion, which gets ignored in most discussions.

High Cohesion

In computer programming, cohesion is a measure of how strongly-related or focused the responsibilities of a single module are. As applied to object-oriented programming, if the methods that serve the given class tend to be similar in many aspects, then the class is said to have high cohesion. In a highly-cohesive system, code readability and the likelihood of reuse is increased, while complexity is kept manageable.

Cohesion is decreased if:

* The functionality embedded in a class, accessed through its methods,
  have little in common.
* Methods carry out many varied activities, often using coarsely-grained or 
  unrelated sets of data.

Disadvantages of low cohesion (or "weak cohesion") are:

* Increased difficulty in understanding modules.
* Increased difficulty in maintaining a system, because logical changes in 
  the domain affect multiple modules, and because changes in one module 
  require changes in related modules.
* Increased difficulty in reusing a module because most applications
  won’t need the random set of operations provided by a module.

One thing that gets lost when people go crazy with IoC containers is the cohesion is lost and traceability of what and how something does something becomes a nightmare to figure out later own down the road, because all the relationships are obscured by a bunch of XML configuration files ( Spring I am looking at you ) and poorly named implementation classes.

这篇关于去松散的耦合和依赖注射的香蕉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆