`
CoderSmith
  • 浏览: 1091 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论

[译]关于在领域对象中使用依赖注入或服务定位

阅读更多

写在前面


这是我第一次写博客, 还不太会写:(, 不过不怕, 一回生, 两回熟, 慢慢就好了 :-)

下面的这篇译文是我在看完uiafzhdl写的"从贫血领域模型到丰富领域模型"(http://uiafzhdl.iteye.com/blog/40022)系列文章后, uiafzhdl又推荐的一篇博客(http://igorstoyanov.blogspot.com/2005/12/dependency-injection-or-service.html), 就那这个作为我的开篇了^_^, 译文采用英汉对照的形式, 如有不妥, 请大家不吝指正. 谢谢!

Dependency Injection OR Service Locator in Domain Objects
with Spring and Hibernate frameworks <o:p></o:p>

<o:p> </o:p>

The Problem<o:p></o:p>

问题描述<o:p></o:p>

<o:p> </o:p>

Currently, many java applications use Spring framework together with Hibernate. One of the advantages
当下,许多的Java应用都在将HibernateSpring框架一起使用。其中的一个优点就是<o:p></o:p>

of that is that we can implement our domain and service objects as POJOs. Most of us are already
我们可以用POJO的方式去实现我们的领域(domain)对象和服务(service)对象。我们中的许多人<o:p></o:p>

convinced of all the benefits that such a type of architecture can have. In architecture like that, domain
已经深信,如此的架构所带来的所有益处。在像这样的架构中,领域对象<o:p></o:p>

objects are managed by Hibernate and service objects are managed by Spring. Also, another advantage
是由Hibernate来管理,而服务对象则是由Spring来管理。同样地,另一个着意使用Spring的优点是<o:p></o:p>

of using Spring specifically is Dependency Injections (DI) of services and resources.<o:p></o:p>

服务和资源(resource)的依赖注入(Dependency InjectionDI)。<o:p></o:p>

The problem is how to retrieve/find/locate services or resources in domain objects since they are
而现在的问题是如何返回/找到/定位在领域对象中的服务或者资源,因为这些领域对象是由<o:p></o:p>

managed by Hibernate and not created through the bean factory of Spring framework?
Hibernate
来管理的,而不是通过Spring框架的Bean Factory创建的。<o:p></o:p>

<o:p> </o:p>

Note: we should differentiate between domain, infrastructure and application services (Domain Driven
说明:我们应该区分领域(domain),基础构件(infrastructure)和应用服务(application service[<o:p></o:p>

Design book - http://domaindrivendesign.org/book). Using infrastructure or application services in
领域驱动设计  http://domaindrivendesign.org/book]。在领域对象中使用基础构件或者应用服务<o:p></o:p>

domain objects is rather not appropriate. The problem I am most concerned with here is in the case when
是有些不太合适。不过现在我最关心的问题是在什么情况下<o:p></o:p>

we need to invoke domain services inside of domain objects.
我们需要调用领域对象中领域服务。<o:p></o:p>

<o:p> </o:p>

Can we avoid the problem?
我们能避免这样的问题吗?<o:p></o:p>

<o:p> </o:p>

Probably! The only way to avoid this problem is not to deal with any services/resources in domain objects.
也许吧!惟一能避免这样问题的方式是不去处理领域对象中的任何服务或资源。<o:p></o:p>

We can do this if we coordinate all domain-service cooperation from a service layer. In this case, we don’t
我们能这样做,如果我们来协调位于服务层的领域服务的动作(*)。在这种情况下,我们就不需要<o:p></o:p>

need to bother with dependency injections, service locators, etc. This makes a lot of sense when we
为依赖注入,服务定位以及其他的费心了。这非常有意义,当我们<o:p></o:p>

invoke services in an asynchronous fashion and especially in SOA /EAI types of architectures.
在异步方式种调用服务时,尤其是在SOA/EAI这种类型的架构中。<o:p></o:p>

<o:p> </o:p>

However, there are some disadvantages:
可是呢,这也有一些不足的地方:<o:p></o:p>

- We deprive ourselves from some of the benefits and power of OOP/OOD.
-
我们丧失了OOP/OOD所带来的一些益处和强大的功能.<o:p></o:p>

- Sometimes, service behavior fits very nicely into the domain model when we have a rich domain model
-
有时,服务的行为非常地适合丰富领域模型<o:p></o:p>

rather than an anemic one http://www.martinfowler.com/bliki/AnemicDomainModel.html and http://wrschneider.blogspot.com/2005/01/avoiding-anemic-domain-models-with.html
而不是贫血领域模型[关于贫血模型,参看http://www.martinfowler.com/bliki/AnemicDomainModel.htmlhttp://wrschneider.blogspot.com/2005/01/avoiding-anemic-domain-models-with.html]

<o:p> </o:p>

Why do we get to the problem?
我们为什么会有这样的问题呢?<o:p></o:p>

<o:p> </o:p>

When I start a project I always try to have very well separated service layer from domain objects (usually
当我开始一个项目的时候,我总试图建立一个与领域对象完全分开的服务层(通常,<o:p></o:p>

enforced with different compilation modules for domain objects and service layer). However, in practice
将领域对象和服务层分为不同的编译模块)。但是在实践中<o:p></o:p>

(at least until now) it always turns out that at some point I will need to use some kind of service or
(至少是目前),可是这样的情况总是出现,有时我们需要在领域对象中使用某些<o:p></o:p>

resource in my domain objects (the simplest example is Time Service. A Time Service is usually needed to
服务或者资源(最简单的一个例子就是时间服务。时间服务通常在这种情况下使用:<o:p></o:p>

give synchronized time when the application is deployed / clustered to more than one server. Also, if you
当应用被部署到集群环境时,提供同步的时间。同样的,如果你<o:p></o:p>

need to play with the current time in your tests, this is a perfect solution. Usually when you deal with some
在你的测试中需要使用到时间,这是个很好的解决方案。通常,当你开发某些支付系统时,<o:p></o:p>

kind of payment/billing systems, you will definitely need something like the above). There can be
你会很明确地需要类似上述的服务)。实际开发中会有<o:p></o:p>

numerous examples of domain objects using some kind of service. Usually, most of the applications that
大量的这样的例子,就是使用某种服务的领域对象。通常,多数的有复杂领域模型的<o:p></o:p>

have more complex domains use some kind of services or resources in domain objects.
应用都在领域对象中用着某种服务或资源。<o:p></o:p>

<o:p> </o:p>

Common solution to the problem
这个问题的常用解决办法<o:p></o:p>

In the past when faced with this sort of situations we have usually used some form of Service Locator
在过去每当遇到这样的情形,我们就会在领域对象内使用某种形式的服务定位(Service Locator<o:p></o:p>

within domain objects in order to retrieve/locate services or resources.
来获取/定位这些服务或资源。<o:p></o:p>

<o:p> </o:p>

However, these days we routinely use Dependency Injection with some form of Inversion of Control
可是呢,在目前的情况下,我们总是习惯性地在某个轻量级的支持反转控制(Inversion of Control, IoC<o:p></o:p>

lightweight container (Spring for example) to wire up practically everything else in an app - and we would<o:p></o:p>

的容器[Spring]中使用依赖注入(Dependency Injection, DI)来把实际应用中的一切组织在一起。我们<o:p></o:p>

like to use a consistent approach if at all possible. Unfortunately, our domain objects are constructed by
都希望在任何可能的情况下,使用一种通用的方式来做这件事情。但非常不幸的是,我们的领域对象是由<o:p></o:p>

Hibernate and not Spring.
Hibernate
,而不是Spring创建的。<o:p></o:p>

<o:p> </o:p>

The most common solution (and the only one that I have seen in practice for now) is to use Spring
最通用的解决方案(也是我到现在为止,唯一在实践中看到的)是使用Spring框架的DI <o:p></o:p>

framework DI to inject services into the service layer and to wire up practically everything else in your app
把各种服务注入到服务层中;也把实际应用中的除领域对象外的一切组织在一起。<o:p></o:p>

beside domain objects. The domain objects, on the other side use Spring-based service locators to find
另一个方面,领域对象使用基于Spring的服务定位来找寻<o:p></o:p>

further services/collaborators they need to do their job.
其他在工作中需要的服务/合作者。<o:p></o:p>

This could be workable approach since both the DI and service location mechanisms use the same
这可能是有效的方法,因为无论是依赖注入还是服务定位机制都使用相同的<o:p></o:p>

underlying infrastructure (Spring bean factory), so they can share the same configuration files etc. Also,
底层框架(Spring bean factory),所以他们能共有相同的配置文件等。同样的,<o:p></o:p>

this common solution to the problem has been kind of described in the javadocs for Spring’s
这个问题的一般解决方案是类似于Springjavadoc中的关于<o:p></o:p>

BeanFactoryBootstrap class with the statement “one singleton to rule them all”. In other words, allow
BeanFactoryBootsrap
类的这样的描述:“one singleton to rule them all”。换句话说就是,允许以<o:p></o:p>

singleton access to the bean factory or application context that holds all of the service layer objects.
单实例的形式访问Bean Factory或者拥有所有的服务层对象应用的上下文<o:p></o:p>

<o:p> </o:p>

However, as I stated above, I would like to use a consistent approach if at all possible for retrieving
但是,正如我上面说的,我更愿意用一种一致的方式,如果能最大可能得取得所有的<o:p></o:p>

services. Not to mention all problems that comes with using Service Locator pattern. I guess using Service
服务。在这里不去提及在使用服务定位模式所带来的问题,我猜,将服务定位和依赖注入一起使用要比<o:p></o:p>

Locator with DI injections is a little better than the old style Service Locator but at the end, this is still a
原有的方式好一些。但说到最后,这仍是个单实例的形式。<o:p></o:p>

singleton. At least there is only ONE singleton in the entire app instead of one for each service object.
至少这样,在整个应用中只有唯一一个单实例,而不是每个服务对象一个。<o:p></o:p>

<!---->
(未完)<!---->
分享到:
评论

相关推荐

    设计模式-服务器定位模式

    服务器定位模式,也称为服务定位器模式,是设计模式的一种,它在iOS开发中扮演着重要的角色,特别是在处理服务请求和依赖注入时。这个模式的主要目标是提供一种方式来查找和管理应用程序中的服务实例。 服务定位器...

    注入器(万能注入器)

    它可以是简单的字面量地图,也可以是复杂的服务定位器或依赖注入容器。常见的实现有Spring框架中的ApplicationContext(Java)和AngularJS中的$injector(JavaScript)。 3. **类型注入**:注入器可以基于类型自动...

    Dependency Injection

    依赖注入(Dependency Injection,简称DI)是软件设计领域的一个核心概念,尤其在现代软件架构,如微服务架构、模块化架构以及大型企业级应用中扮演着至关重要的角色。本文将深入探讨依赖注入的概念、原理及其在Java...

    Dependency injection in action

    本书在业界得到了良好的评价,书中引用了多位业界专家的推荐和点评,强调了本书在.NET开发环境中的重要性,并被认定为.NET平台上关于依赖注入的权威指南。读者通过阅读这本书,能够深入理解依赖注入的原则、实践和...

    轻量级的.NET对象查找服务和AOP开发框架测试源码

    在.Netop框架中,IOC容器负责管理对象的生命周期,包括实例化、依赖注入和对象的销毁。开发者可以定义接口,并让IOC容器根据配置自动创建和管理这些接口的实现,降低了代码之间的耦合度。 **面向切面编程(AOP)**...

    前端开源库-catberry-locator

    总之,Catberry-locator是Catberry框架中不可或缺的一部分,它提供了强大的服务定位和依赖注入功能,极大地提升了Catberry应用的开发效率和代码质量。通过深入理解和使用`catberry-locator`,开发者能够更好地驾驭...

    ioc AOP深入了解

    传统的编程模式中,对象创建自身依赖的对象并调用它们,而在IoC模式下,对象依赖的其他对象是由外部容器提供的。这样做的目的是为了减少对象间的耦合度,提高代码的可维护性和可测试性。 **控制反转的核心思想**:...

    深入PHP:面向对象、模式与实践(第3版)第9到12章

    这一章主要讲解了单例模式、工厂模式以及依赖注入等对象生成策略。单例模式确保一个类只有一个实例,并提供一个全局访问点,以避免资源的重复分配。工厂模式则是一种创建对象的抽象方法,允许代码在运行时决定创建...

    接入钉钉必备jar包(含加解密依赖包).zip

    2. **Spring框架**: Spring是Java领域最流行的轻量级框架,它简化了企业级应用的开发,提供了依赖注入、AOP(面向切面编程)以及众多模块,如Spring MVC用于Web开发。在接入钉钉API时,Spring可以用来管理和服务组件...

    Java on Guice

    - **依赖注入**:通过将对象所需的依赖项自动注入到对象中,避免了对象之间硬编码式的耦合。 - **模块化**:Guice支持模块化设计,可以通过定义不同的模块来管理不同领域的依赖关系。 - **可扩展性**:Guice允许...

    spring 开发手册

    **依赖注入(Dependency Injection)**是Spring框架的核心特征之一,它允许对象在其生命周期中被动地接收依赖,而不是主动创建或查找依赖。这种方式提高了代码的可测试性与可维护性,降低了组件之间的耦合度。 - **...

    学习资料\MVC双外间关联调用例子.rar

    在这个"双外间关联调用"的例子中,我们可以推测这可能是关于两个独立的外部对象或服务如何在MVC框架下进行通信。这通常涉及到依赖注入(Dependency Injection,DI)或者服务定位器(Service Locator)等设计模式,...

    毕业设计,基于java语言,ssm框架和微信小程序开发的地图定位系统.7z

    Spring是一个全面的开发框架,提供了依赖注入、AOP(面向切面编程)等特性,便于管理对象和事务。SpringMVC是Spring的一部分,用于处理HTTP请求和响应,构建MVC(模型-视图-控制器)架构。MyBatis则是一个持久层框架...

    spring+MVC中文教程pdf.pdf

    - **Bean Factory**:Spring容器的基础,负责实例化、定位和配置应用程序中的对象,即Bean。 - **ApplicationContext**:扩展自Bean Factory,提供了更多的企业服务支持,如国际化、事件发布等。 - **Web Context...

    IoC.rar_beancreateexception_ioc

    **IOC(Inversion of Control)**,即控制反转,是软件设计模式中的一种核心概念,尤其是在Java等面向对象编程语言的领域中广泛应用。它通过将对象的创建和管理权限交给一个外部容器,如Spring框架,来实现解耦。在...

Global site tag (gtag.js) - Google Analytics