创建使用Unity对象解决额外的参数 [英] Creating objects using Unity Resolve with extra parameters
问题描述
我使用的棱镜,这使是很好的统一IoC容器了。我是新来的概念,所以我还没有得到我的手都围绕它。我想现在要做的就是创建一个使用IoC容器,而是传递一个额外的参数也是一个对象。请允许我用一个例子说明..:
我有一个类,需要一个命令对象。这是注册在IoC容器,所以它会处理它很好:
公共类Person
{
公众人物(IApplicationCommands命令){..}
..
}
人人= _container.Resolve<人>();
现在 - 我想通过另一种说法 - 例如:的人的名字。不过,我还是想使用IoC容器来处理解决,因此从IoC容器得到其他paramenters。但通过在名称为自定义的参数。可以这样做?
公共类Person
{
公众人物(IApplicationCommands命令字符串名称){..}
..
}
字符串名称=约翰;
人人= _container.Resolve<人>(名称); // ....?
这个例子似乎并没有工作,但有什么办法,使其工作?或者没有统一的IoC容器要求所有要在容器中注册呼叫解决之前的参数?
编辑:这个答案已经过时了,在我看来,是因为它有统一的旧版本的假设。 NotDan的回答是更好的。
您已经有了几个选择。他们是诚实有点跛,但他们的工作。
选项1:作用域货柜
如果你想使用构造函数 注射,你需要创建一个 范围内的容器,并把你的数据 到该范围的容器:
IUnityContainer子容器= _container.CreateChildContainer();
//不要这样......创造出比字符串的自定义类型,如
// MyConstructorParams或类似的东西...这得到点跨越。
subContainer.RegisterInstance<字符串>(约翰福音);
人人= subContainer.Resolve<人>();
选项2:初始化方法
我通常做的,虽然是有 在单独的初始化方法我 实例变量目标对象:
公共类Person { 公众人物(IApplicationCommands命令) {..} 公共无效初始化(字符串名称){..} .. }
然后您的使用情况变为:
人员的人= container.Resolve<人>();
person.Initialize(约翰福音);
也不是特别愉快,但它会完成这项工作。最重要的是选择一个公约,并坚持下去,否则你会得到一个有点失落。
希望这有助于。
I'm using Prism, which gives be the nice Unity IoC container too. I'm new to the concept, so I haven't gotten my hands all around it yet. What I want to do now is to create an object using the IoC container, but passing an extra parameter too. Allow me to explain with an example..:
I have a class that takes a commands object. This is registered in the IoC container, so it will handle it nicely:
public class Person
{
public Person(IApplicationCommands commands) { .. }
..
}
Person person = _container.Resolve<Person>();
Now - I want to pass in another argument - e.g. the name of the person. However, I still want to use the IoC container to handle the resolving and hence get the other paramenters from the IoC container. But pass in the name as a "custom" parameter. Can this be done?
public class Person
{
public Person(IApplicationCommands commands, string name) { .. }
..
}
string name = "John";
Person person = _container.Resolve<Person>(name); // ....??
This example doesn't seem to work, but is there a way to make it work? Or does Unity IoC container require all parameters to be registered in the container before calling Resolve?
Edit: this answer is obsolete, in my opinion, because it has an assumption of an older version of Unity. NotDan's answer is better.
You've got a few options. They are honestly a bit lame, but they will work.
Option 1: Scoped Container
If you want to use constructor injection, you'll need to create a scoped container and put your data into that scoped container:
IUnityContainer subContainer = _container.CreateChildContainer();
//Don't do this... create a custom type other than string, like
// MyConstructorParams or something like that... this gets the point across.
subContainer.RegisterInstance<string>("John");
Person person = subContainer.Resolve<Person>();
Option 2: Initialize Method
What I typically do, though, is have a seperate Initialize method on my target objects for instance variables:
public class Person { public Person(IApplicationCommands commands) { .. } public void Initialize(string name) { .. } .. }
And then your usage becomes:
Person person = container.Resolve<Person>();
person.Initialize("John");
Neither is particularly pleasant, but it'll get the job done. The important thing is to pick a convention and stick to it, otherwise you'll get a bit lost.
Hope this helps.
这篇关于创建使用Unity对象解决额外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!