IOC问题:运行时数据相关类的抽象工厂过多 [英] IOC issue: Too many Abstract Factories for Runtime data dependent classes

查看:55
本文介绍了IOC问题:运行时数据相关类的抽象工厂过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在我的一个项目中使用DI。对于运行时相关的类,我创建了相应的Abstract工厂。遵循这种模式后,我最终拥有太多的抽象工厂,几乎每个班级都有一个。

I have recently started to use DI in one of my projects. For runtime dependent classes, I created corresponding Abstract factories. After following this pattern I end up having too many abstract factories, almost one for each of my class.

使用IOC时,最终拥有太多抽象工厂是否很常见?

Is it common to end up having too many abstract factories when using IOC ?

方案:假设我从数据库中获得一个实体对象。用户可以在此实体对象上执行10种不同的用例。对于每个用例,我都有一个不同的类来处理它。在某些情况下,给定用例可能具有子用例组件,这些子用例组件也可能需要实体对象。
由于这些类依赖于运行时实体对象,因此我必须为它们中的每一个创建抽象工厂。最后,我将施工说明放入IOC容器中。

Scenario: Suppose I get an "Entity" object from a database. There are 10 different use cases a user could perform on this entity object. For each of the usecase I have a different class to handle it. In some cases a given use case could have sub use case components which may also need the entity object. As these classes are dependent on runtime entity object, I had to create abstract factory for each one of them. Finally I wire the construction instructions in the IOC container.

还有另一种方法可以做到这一点。我只是觉得创建所有这些工厂都是浪费时间,尤其是当所有子类都依赖于同一实体对象时。

Is there an alternative way of doing it. I just feel creating all these factories is waste of time, ESPECIALLY when all the sub classes are dependent on the same entity object.

我倾向于使用IOC容器为我的方案注册一个工厂/建筑商类。该工厂将为我的场景创建所需的对象图。我将IOC视为帮助实施DI概念的工具。只要我通过自定义生成器/工厂观察DI,一路不使用IOC容器可能还不错。

I am inclined on having a single factory / builder class registered for my scenario with IOC container. This factory would create the required object graph for my scenario. I see IOC as a tool to help implement DI concept. Not using IOC container all the way may not be bad as long as I am observing DI via a custom builder / factory.

我想知道你们对这种方法有何看法?

I wanted to know what do you guys think about this approach?

推荐答案

听起来您正在遭受 1:1接口过多的困扰。当发生这种情况时,通常表明人们应该停止思考重用抽象原理

It sounds like you are suffering from an overabundance of 1:1 interfaces. When that happens it's often a sign that one should stop and think about the Reused Abstractions Principle.

也许您可以重新设计界面,以便更少的工厂和更多的命令

Perhaps you can redesign your interfaces so that there are less factories and more commands.

这篇关于IOC问题:运行时数据相关类的抽象工厂过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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