微软统一。如何在构造函数中指定某个参数?
我正在使用微软Unity。我有一个接口 ICustomerService
及其实现 CustomerService
。我可以使用以下代码将它们注册到 Unity 容器:
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager());
如果 CustomerService
在其构造函数中具有特定参数(例如 ISomeService1
),我使用以下代码(我需要指定 SomeService1
):
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1()));
这里没有问题。
当 CustomerService
类在其构造函数中具有两个参数(而不是上一示例中的一个参数)(例如 ISomeService1
和 ISomeService2
)时,就会出现此问题。当我使用以下代码时它工作正常: container.RegisterType
问题是我不想指定 SomeService2( )
作为第二个参数。我只想指定第一个参数 - SomeService1()
。但我收到错误,我需要指定一个或两个参数。
如何仅指定构造函数的第一个参数?
I'm using Microsoft Unity. I have an interface ICustomerService
and its implementation CustomerService
. I can register them for the Unity container using the following code:
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager());
If CustomerService
has a certain parameter in its constructor (e.g. ISomeService1
), I use the following code (I need to specify SomeService1
):
container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1()));
No problems here.
The problem appears when CustomerService
class has two parameters (not one param as in the previous example) in its constructor (e.g. ISomeService1
and ISomeService2
). It works fine when I'm using the following code:container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager(), new InjectionConstructor(new SomeService1(), new SomeService2()));
The problem is that I do not want specify SomeService2()
for the second parameter. I want to specify only first parameter - SomeService1()
. But I get the error that I need to specify none or both of parameters.
How can I specify only first parameter of constructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最好的答案是实际使用容器。
您所做的是说“构建此类型时,使用该对象的特定实例”。这没有利用容器为您构建实例的能力。相反,您应该在容器中注册 IService1 和 IService2。然后,告诉容器为您解决这些依赖关系。
它看起来像这样:
它的作用是告诉容器“每当存在 IService1 类型的依赖项时,新建一个 SomeService1 类型的新对象并将其传递给它”,对于 IService2 也是如此。
接下来,您需要告诉容器如何处理 ICustomerService。一般来说,您会这样做:
这告诉容器:在解析 ICustomerService 时,使用采用 IService1 和 IService2 的构造函数新建一个 CustomerService 实例。要获取这些参数,请回调容器来解析这些类型。
这有点冗长,而且是常见情况,因此有一些快捷方式。首先,您可以传递一个 Type 对象,而不是执行 new ResolvedParameter,如下所示:
作为另一种简写,如果 CustomerService 只有一个构造函数,或者您要调用的构造函数是采用最大参数列表的构造函数,则可以保留InjectionConstructor 完全退出,因为这是容器在没有其他配置的情况下将选择的构造函数。
当您希望为构造函数参数传递特定值而不是通过容器解析服务时,通常会使用您正在使用的形式。
回答你原来的问题 - 嗯,你不能完全按照你所说的去做。构造函数参数需要某种类型的值。不过,您可以在其中放置任何您想要的其他内容 - null 通常可以工作。
请注意,您也可以混合使用这两种形式。例如,如果您想要解析 IService1 并为 IService2 参数传递 null,请执行以下操作:
* 编辑 *
根据下面的注释,您真正想要的是另一个功能 - 名为注册。
基本上,您有 IService1 的两个实现和 IService2 的一个实现。因此,您可以做的就是注册它们两者,然后告诉容器使用哪一个。
首先,要注册第二个实现,您需要给出一个显式名称:
然后您可以告诉容器解析 IService1 但使用该名称。这是 ResolvedParameter 类型存在的主要原因。由于您只需要 IService2 的默认值,因此可以使用 typeof() 作为简写。您仍然需要为参数指定两种类型,但不需要特定值。如果这有任何意义的话。
那应该可以满足您的需要。
Your best answer is to actually use the container.
What you're doing is saying "when building this type, use this specific instance of the object." This doesn't take advantage of the ability of the container to build up instance for you. Instead, you should register IService1 and IService2 in the container. Then, tell the container to resolve those dependencies for you.
It looks something like this:
What this does is tell the container "whenever there's a dependency of type IService1, new up a new object of type SomeService1 and hand it that" and similarly for IService2.
So next, you need to tell the container what to do about ICustomerService. In most generality, you'd do this:
This tells the container: when resolving ICustomerService, new up an instance of CustomerService using the constructor that takes IService1 and IService2. To get those parameters, call back into the container to resolve those types.
This is a bit verbose, and a common case, so there are some shortcuts. First off, you can pass a Type object instead of doing new ResolvedParameter, like so:
As another shorthand, if CustomerService has only one constructor, or if the one you want called is the one that takes the largest parameter list, you can leave the InjectionConstructor out completely, as that's the constructor that the container will pick in the absence of other configuration.
The form you're using is typically used when you want a specific value passed for a constructor parameter rather than resolving the service back through the container.
To answer your original question - well, you can't do exactly what you said. The constructor parameter needs A value of some sort. You could put anything else in there you want, though - null typically works.
Note you can also mix the two forms. For example, if you want to resolve IService1 and pass null for the IService2 parameter, do this:
* EDIT *
Based on the comment below, what you actually want is another feature - named registrations.
Basically, you have two implementations of IService1 and one of IService2. So, what you can do is register both of them, and then tell the container which one to use.
First off, to register the second implementation, you need to give an explicit name:
Then you can tell the container to resolve IService1 but use the name. This is the main reason that the ResolvedParameter type exists. Since you just want the default for IService2, you can use typeof() as a shorthand. You still need to specify both types for the parameters, but you don't need a specific value. If that makes any sense.
That should do what you need.
作为 Chris Tavares 答案的替代方案,您可以让容器仅解析第二个参数:
As an alternative of Chris Tavares' answer, you can let the container resolve only the second parameter:
Chris Tavares 给出了很好的答案,提供了很多信息。
如果您有许多参数要注入,通常这些参数是可以由 Unity 解析的接口或实例(使用 diffnet 技术)。但是,如果您只想提供一个无法自动解析的参数(例如文件名字符串)怎么办?
现在您必须提供所有
typeof(IMyProvider)
和一个字符串或实例。但说实话,仅仅提供类型就可以由 Unity 完成,因为 Unity 已经有一个选择最佳 ctor 的策略。所以我编写了
InjectionConstructor
的替代代码:用法:
Chris Tavares gave a good answer with a lot of information.
If you have many Parameters to inject usually these are interfaces or instances which can be resolved by Unity (using the differnet techniques). But what if you want to provide only one Parameter which cannot be resolved automatically, e.g. a string for a filename?
Now you have to provide all the
typeof(IMyProvider)
and one string or instance. But to be honest just providing the types could be done by Unity, because Unity has already a strategy to choose the best ctor.So I coded a replacement for
InjectionConstructor
:Usage:
您可以使用容器层次结构。在父容器中注册公共实现,如果通过主容器解析,则该实例将解析。然后创建子容器,并在子容器中注册替代实现。如果通过子容器解决,则此实现将解决,即子容器中的注册覆盖父容器中的注册。
示例如下:
请注意,使用容器层次结构,您对构造函数中的参数数量一无所知,这很好,因为只要您绑定到参数计数及其类型,您就无法使用 IoC 的全部功能。你知道的越少越好。
You can use containers hierarchy. Register common implementation in parent container, this instance will resolve if resolved through master container. Then create child container, and register alternative implementation in child container. This implementation will resolve If resolved through child container, i.e. registration in child container overrides registration in parent container.
Here's the example:
Please note that using containers hierarchy you can know nothing about number of parameters in constructor, and that's great, because as long as you are bound to parameters count and their types you can't use full power of IoC. The less you know the better.
您可以将第二个参数默认为构造函数(例如,
=null
),或者通过重载除了双参数构造函数之外还提供单参数构造函数。You can default the second parameter to the constructor (e.g.,
=null
) or offer a single parameter constructor in addition to the two parameter constructor by overloading it.