统一与构造者

发布于 2024-10-06 00:58:19 字数 813 浏览 5 评论 0原文

是否有可能使统一尝试所有已定义的构造函数,从参数最多的构造函数开始一直到最不具体的构造函数(默认构造函数)?

编辑

我的意思是:

foreach (var constructor in concrete.GetConstructorsOrderByParameterCount())
{
   if(CanFulfilDependencies(constructor))
   {
       UseConstructor(constructor);
       break;
   }
}

我不希望 Unity 只尝试具有大多数参数的构造函数。我希望它继续尝试,直到找到合适的构造函数。如果 Unity 默认情况下不提供此行为,是否可以创建一个扩展或其他东西来执行此操作?

编辑2

我得到了一个带有两个构造函数的类:

public class MyConcrete : ISomeInterface
{
    public MyConcrete (IDepend1 dep, IDepend2 dep2)
    {}

    public MyConcrete(IDepend1 dep)
    {}
}

该类存在于多个项目使用的库中。在这个项目中,我想使用第二个构造函数。但 Unity 停止了,因为它无法通过第一个构造函数满足依赖关系。而且我不想想要更改该类,因为第一个构造函数已被其他项目中的 DI 使用。

因此,Unity 需要尝试解析所有构造函数。

Is it possible to make unity try all defined constructors starting with the one with most arguments down to the least specific one (the default constructor)?

Edit

What I mean:

foreach (var constructor in concrete.GetConstructorsOrderByParameterCount())
{
   if(CanFulfilDependencies(constructor))
   {
       UseConstructor(constructor);
       break;
   }
}

I don't want Unity to only try the constructor with most parameters. I want it to continue trying until it finds a suitable constructor. If Unity doesn't provide this behavior by default, is it possible to create an extension or something to be able to do this?

Edit 2

I got a class with two constructors:

public class MyConcrete : ISomeInterface
{
    public MyConcrete (IDepend1 dep, IDepend2 dep2)
    {}

    public MyConcrete(IDepend1 dep)
    {}
}

The class exists in a library which is used by multiple projects. In this project I want to use second constructor. But Unity stops since it can't fulfill the dependencies by the first constructor. And I do not want to change the class since the first constructor is used by DI in other projects.

Hence the need for Unity to try resolving all constructors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2024-10-13 00:58:19

Unity 将选择参数最多的构造函数,除非您使用 [InjectionConstructor] 属性显式标记构造函数,该属性随后将定义供 Unity 使用的构造函数。

当您指定合适的构造函数时;这在一定程度上取决于环境。例如,如果您总是想保证在使用 Unity 时使用某个构造函数,请使用前面提到的属性,否则显式调用您要使用的构造函数。

Unity“尝试”所有构造函数有什么意义?它的目的是以解耦的方式提供类型的实例。如果任何构造函数都会创建该类型的实例,为什么它会迭代构造函数?

编辑:

您可以允许在项目中使用具有最多参数的构造函数,而该项目在其容器中没有对该类型的引用,方法是使用 子容器。这不会强制使用具有单个参数的构造函数,但它将允许具有 2 个参数的构造函数现在可以跨项目工作。

您还可以切换到全面使用单个构造函数,并通过另一种形式的 DI(属性注入)强制另一个接口,而不是构造函数注入……因此,该基础适用于整个项目,这将更有意义。

Unity will choose the constructor with the most parameters unless you explicitly tag a constructor with the [InjectionConstructor] attribute which would then define the constructor for Unity to use.

When you state a suitable constructor; that is somewhat contingent on the environment. If for instance you always want to guarantee that a certain constructor is used when making use of Unity use the attribute mentioned previously, otherwise explicitly call the constructor you want to use.

What would be the point of Unity "trying" all constructors? It's purpose is to provide an instance of a type in a decoupled manner. Why would it iterate through the constructors if any constructor will create an instance of the type?

EDIT:

You could allow the constructor with the most params to be used within the project that does not have a reference to that type within its container by making use of a child container. This will not force the use of the constructor with a single param but it will allow the constructor with 2 params to work across the projects now.

You could also switch to using the single constructor across the board and force the other interface in via another form of DI (Property Injection), not Constructor Injection...therefore the base is applicable across the projects which would make more sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文