控制域对象构造问题的反演
据我了解,IoC 容器有助于创建应用程序级对象,例如服务和工厂。但域级对象应该手动创建。 Spring的手册告诉我们:“通常不会在容器中配置细粒度的域对象,因为创建/加载域对象通常是DAO和业务逻辑的责任。”
出色地。但是,如果我的域“细粒度”对象依赖于某些应用程序级对象怎么办? 例如我有一个 UserViewer(User user, UserConstantsconstants) 类。 user 是无法注入的域对象,但 UserViewer 还需要 UserConstants,它是由 IoC 容器注入的高级对象。
我想从 IoC 容器注入 UserConstants,但我还需要一个瞬态运行时参数 User 。
设计有什么问题吗?
提前致谢!
更新
看来我的问题不够精确。我真正需要的是一个如何执行此操作的示例:
创建类 UserViewer(User user, UserService service) 的实例,其中 user 作为参数传递,服务是从 IoC 注入的。
如果我注入UserViewer查看器,那么如何将用户传递给它?
如果我手动创建UserViewer查看器,那么如何将服务传递给它?
As I understand IoC-container is helpful in creation of application-level objects like services and factories. But domain-level objects should be created manually.
Spring's manual tells us: "Typically one does not configure fine-grained domain objects in the container, because it is usually the responsibility of DAOs and business logic to create/load domain objects."
Well. But what if my domain "fine-grained" object depends on some application-level object.
For example I have an UserViewer(User user, UserConstants constants) class.
There user is domain object which cannot be injected, but UserViewer also needs UserConstants which is high-level object injected by IoC-container.
I want to inject UserConstants from the IoC-container, but I also need a transient runtime parameter User here.
What is wrong with the design?
Thanks in advance!
UPDATE
It seems I was not precise enough with my question. What I really need is an example how to do this:
create instance of class UserViewer(User user, UserService service), where user is passed as the parameter and service is injected from IoC.
If I inject UserViewer viewer then how do I pass user to it?
If I create UserViewer viewer manually then how do I pass service to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个设计没有什么问题。为此,您可以使用工厂,它的一侧在域中,一侧在基础设施中。
您可以手动编写它们,也可以让容器为您执行此操作,例如 温莎的 TypedFactoryFacility。
此外,当您的域对象来自持久层时,您可以将容器插入其中以注入它们所需的服务(NHibernate 可以做到这一点)。
there's nothing wrong with this design. you use
Factories
for that, which have one leg in the domain, one leg in infrastructure.You can either write them manually, or have the container do that for you, by things like TypedFactoryFacility in Windsor.
Also when your domain objects come from persistence layer you can plug your container there to inject the services they require (NHibernate can do that).
正是这一点被认为是不好的做法。我想说问题可能是:
POJO风格是它们可以在所有环境中使用(持久化在数据库中,在业务算法和规则中处理,在视图技术中读取和设置,序列化并通过网络发送)。在其中注入应用程序级对象可能会导致以下问题:
通常,构成您的域的 pojo 是独立的。他们可以访问其他 pojo(以及许多枚举),仅此而已。
除了数据之外,它们还具有实现业务规则或算法细节的方法(记住对数据和处理数据的代码进行分组的面向对象思想;-) ):
<块引用>
事后,您经常会发现 pojo 方法被大量重用,并且由服务或管理器以不同的方式组合。这是减少重复的一大胜利,方法名称提供了急需的“含义”,并为刚接触模块的开发人员提供了更轻松的访问。
It is precisely this that is considered bad-practice. I would say the problems could be:
The POJO style is that they can be used in all environments (persisted in the database, processed in business algorithms and rules, read and set in view technologies, serialized and send over the network). Injecting application-level objects in them could cause the following problems:
Typically, the pojos that constitute your domain are self-contained. They can have access to other pojos (and many enums), that's all.
In addition to the data, they have methods that implement the details of the business rules or algorithms (remember the OO idea of grouping data and code that work on it ;-) ):
对于您的更新:
在这种情况下,您需要一个工厂方法(可能在您的工厂或定位器上)。它可以如下所示,将两部分分开:
For your update:
In that case, you need a factory method (possibly on a Factory or Locator of yours). It could look at follow, separating the two parts: