微软统一。如何指定构造一定的参数? [英] Microsoft Unity. How to specify a certain parameter in constructor?

查看:134
本文介绍了微软统一。如何指定构造一定的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用微软统一。我有一个接口 ICustomerService 及其实施的CustomerService 。我可以用下面的code为统一登记的容器其中:

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());

如果的CustomerService 具有一定的参数的构造函数(如 ISomeService1 ),我用下面的code(我需要指定 SomeService1

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.

的CustomerService 类有两个参数(而不是一个参数作为在previous为例)在其构造出现问题(例如, ISomeService1 ISomeService2 )。它正常工作时,我用下面的code:
container.RegisterType&LT; ICustomerService,为CustomerService&GT;(新TransientLifetimeManager(),新InjectionConstructor(新SomeService1(),新SomeService2()));

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()));

问题是,我不希望指定 SomeService2()第二个参数。我想仅指定第一个参数 - SomeService1()。但我得到我需要指定none或两个参数的错误。

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?

推荐答案

您最好的答案是实际使用的容器。

Your best answer is to actually use the container.

你正在做什么是说:建立这种类型时,使用对象的此特定实例。这并不需要的容器,建立实例,你的能力优势。相反,你应该在容器注册IService1和IService2。然后,告诉容器来解决这些依赖你。

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.

这看起来是这样的:

container.RegisterType<IService1, SomeService1>();
container.RegisterType<IService2, SomeService2>();

这样做是告诉容器只要有型IService1,新达类型SomeService1的新对象的依赖,并把它说:同样的IService2。

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.

所以,下次,你需要告诉容器做一下ICustomerService什么。在大多数的通用性,你应该这样做:

So next, you need to tell the container what to do about ICustomerService. In most generality, you'd do this:

container.RegisterType<ICustomerService, CustomerService>(
    // Note, don't need to explicitly say transient, that's the default
    new InjectionConstructor(new ResolvedParameter<IService1>(),
        new ResolvedParameter<IService2>()));

这告诉容器:使用需要IService1和IService2构造解析ICustomerService时,新一轮上涨的CustomerService的实例。为了得到这些参数,调用回容器中,以解决这些类型。

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.

这是一个有点冗长,以及常见的情况,所以有一些捷径。首先,你可以传递一个类型的对象,而不是做新ResolvedParameter,就像这样:

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:

container.RegisterType<ICustomerService, CustomerService>(
    new InjectionConstructor(typeof(IService1), typeof (IService2)));

另一个速记,如果为CustomerService只有一个构造函数,或者如果你想叫一个是取最大的参数列表中的一个,你可以完全离开InjectionConstructor出来,因为这是该容器将在挑构造没有其他的配置。

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.

container.RegisterType<ICustomerService, CustomerService>();

您正在使用的表格,当你想为一个构造函数参数传递,而不是通过容器解决服务回报一个特定的值,通常使用。

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.

请注意,你也可以混合使用这两种形式。例如,如果你想解决IService1并传递null作为IService2参数,做到这一点:

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:

container.RegisterType<ICustomerService, CustomerService>(
    new InjectionConstructor(typeof(IService1), null));

*编辑*

,你真正想要的是另一种功能 - 名为注册

Based on the comment below, what you actually want is another feature - named registrations.

基本上,你有IService1的两种实现和IService2之一。所以,你可以做的是注册他们两个,然后告诉要使用哪一个容器。

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:

container.RegisterType<IService1, OtherService1Impl>("other");

这时你可以告诉解决IService1,但使用的名称的容器。这是该ResolvedParameter类型存在的主要原因。既然你只是想为IService2的默认值,你可以使用typeof运算()作为简写。您仍然需要同时指定类型的参数,但你并不需要一个特定的值。如果让任何意义。

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.

container.RegisterType<ICustomerService, CustomerService>(
    new InjectionConstructor(new ResolvedParameter<IService1>("other"), typeof(IService2));

这是应该做的,你所需要的。

That should do what you need.

这篇关于微软统一。如何指定构造一定的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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