论坛首页 Java企业应用论坛

Injection in different tiers

浏览 10188 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-03-13  
在一个应用有多层的时候,对各层接口的注射就成了一个比较重要的问题。如何将实现注入,并且不侵入该应用的代码,要做的比较妥帖,似乎并不容易。
比方说,在Action之下,会有一个Service层(Service之上可能会有Facade层),Service调用BO层完成工作,BO会调用DAO层完成持久化。这样就会有3到4层的接口需要注射.
一种做法就是在Action层注射完成之后,将注射完成的实例向下传递(或者通过包装)。还有一种做法就是在各层各自完成注射。
第一种做法会形成一个全局的变量,不是很喜欢。第二种做法会导致各层对注射造成依赖,而且注射的代码分散,不好管理。
不知道大家有什么更好的方式对多层的接口进行注射?
   发表时间:2005-03-15  
一般情况之下,有必要分那么多层吗?我的程序就service和dao层,只需要在action中注入service interface,在serviceimpl中注入dao interface即可
0 请登录后投票
   发表时间:2005-03-15  
小程序当然不必要分那么多的层,但当程序大了之后,不可能把逻辑都放在Service层。Service应该把工作delegate到Domain层,自己不应该做什么工作。而由Domain层去调用DAO层完成持久化。真正的逻辑应该在Domain层。
这是一个最基本的分层结构,如果还要灵活的话,在Service之上可能还要Facade层等等。
0 请登录后投票
   发表时间:2005-03-15  
不一定要用Domain Model方式处理业务逻辑,也可以用Transaction Script,我们就用BO+PO
0 请登录后投票
   发表时间:2005-03-16  
阿木欢 写道
不一定要用Domain Model方式处理业务逻辑,也可以用Transaction Script,我们就用BO+PO

如果要讨论Domain的话,可以在开一个Thread。
没有人考虑过多层的Interface怎样Injection吗?
我目前采用的方式是这样的:
当前层的接口的实现会引用下一层的接口,通过Setter注入下一层接口的实现,形成一个链,这样只需要在Action层获得最上面一层接口的实现就行了。
有没有其他更好的方式呢?
0 请登录后投票
   发表时间:2005-03-16  
kewan 写道
小程序当然不必要分那么多的层,但当程序大了之后,不可能把逻辑都放在Service层。Service应该把工作delegate到Domain层,自己不应该做什么工作。而由Domain层去调用DAO层完成持久化。真正的逻辑应该在Domain层。
这是一个最基本的分层结构,如果还要灵活的话,在Service之上可能还要Facade层等等。


I think Service Tier normally is used to process customized exception in web tier.  So I don't think it is necessary to have service tier. What's more, the different granularity action is enough to delegate web request to bussiness tier.

Action->Domain object or BO->DAO->PO


引用
第一种做法会形成一个全局的变量,不是很喜欢。第二种做法会导致各层对注射造成依赖,而且注射的代码分散,不好管理。
不知道大家有什么更好的方式对多层的接口进行注射

Could you explain more please?
0 请登录后投票
   发表时间:2005-03-16  
引用
I think Service Tier normally is used to process customized exception in web tier

这点倒是不敢苟同。Action的粒度毕竟是大了一点,而且和具体的技术相关。如果不想用Struts,比如改用Swing,工作量就比较大。
采用Service层,将Domain Logic呈现给Action,这样在Action中只是调用而已,更换也很方便。按我的意思,Service不做什么具体的事,甚至exception。只是组织一下Domain Logic,提供Client所需要的服务而已。
你说说Service只是处理exception,能仔细讲讲吗?
至于我说的第一种方法,是因为我想如果要其他的层也能够得到具体的实例,应该生成一个全局的变量,否则其他层没法访问到。我目前采用的链的方式,不是这样的。第二种方法,每一层都会有Spring的引用,当然不好。
不知我是否说清楚了。
0 请登录后投票
   发表时间:2005-03-16  
What you said is right.  Our difference depends on different situation.

I mentioned about exception if we use service tier. We know, we need the throwed exception is friendly to client. Therefore, we define different meaning exception classes in different tier.

For example,  Hibernate exception is just in PO tier.DAO exception is just in DAO tier.  Service exception is just in Service tier.  The exception transition is happend if we throw new unchecked exception or at least the unchecked subclass.

Now Suppose we use Struts, I think action can directly deal with exception instead of adding service tier.
0 请登录后投票
   发表时间:2005-03-17  
我想我明白你的意思了。每一层都有自己的Exception,我想是对的。但反过来说某层的作用是为了处理exception,就不是很对了。你认为呢?呵呵
我想我们的观点没有什么不同。
回到正题,你能说说对于多层的接口,有什么更好的方式注入吗?
0 请登录后投票
   发表时间:2005-03-17  
Kewan, nice to discuss with you.

I showed my opinion based on my supposed situation. Anyway,  the more decoupling, the more maintanence, extension, reusablity, flexibility we can get. The down side is introducing more complexty.

回到正题.
I think I agree with your second option. Suppose we have the following structure:
Action->BO->DAO->PO

I don't understand. what you said like 第二种做法会导致各层对注射造成依赖,而且注射的代码分散,不好管理。

Ioc is a good solution for decoupling.  In Spring Ioc, all of injection configuration is in the xml file.
Some people complained about Spring configuration is evil. As gigix said, we may divide one configuration xml file into at least 2 xml files. One is for the service tier.(Such as injecting different DAOImpl to differnt BO). the other is for the DAO and PO tiers(Such as injection sessionFactory to different DAOImple).  If like you said 不好管理, do you think serviceLocator, Factory pattern 好管理?

I don't think 各层对注射造成依赖.  If we have to say 注射造成依赖,  we may think action tier 对注射造成依赖 if we use getBean in action codes. We also can put this 依赖 into xml files. So no getBean at all. no 依赖 in java codes.

If possible, I would like you explain more detailed about your first option.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics