如何将参数传递给IOC框架中的构造函数

[英]How to pass arguments to a constructor in an IOC-framework


How can I pass arguments to a constructor in an IOC-framework? I want to do something like: (Trying to be IOC-framework agnostic ;) )

如何将参数传递给IOC框架中的构造函数?我想做类似的事情:(试图成为IOC框架不可知;))

object objectToLogFor = xxx;
container.Resolve<ILogging>(objectToLogFor); 

public class MyLogging : ILogging
{
    public MyLogging(object objectToLogFor){}
}

It seems that this is not possible in StructureMap. But I would love to see someone prove me wrong.

看起来这在StructureMap中是不可能的。但我很乐意看到有人证明我错了。

Are other frameworks more feature-rich? Or am I using the IOC-framework in the wrong way?

其他框架是否功能更丰富?或者我是以错误的方式使用IOC框架?

7 个解决方案

#1


9  

In structure map you could achieve this using the With method:

在结构图中,您可以使用With方法实现此目的:

string objectToLogFor = "PolicyName";
ObjectFactory.With<string>(objectToLogFor).GetInstance<ILogging>();

See: http://codebetter.com/blogs/jeremy.miller/archive/2008/09/25/using-structuremap-2-5-to-inject-your-entity-objects-into-services.aspx

#2


5  

For Castle Windsor:

对于温莎城堡:

var foo = "foo";
var service = this.container.Resolve<TContract>(new { constructorArg1 = foo });

note the use of an anonymous object to specify constructor arguments.

请注意使用匿名对象来指定构造函数参数。

using StructureMap:

var foo = "foo";
var service = container.With(foo).GetInstance<TContract>();

#3


4  

How can this be language-agnostic? This is implementation detail of the framework in question.

这怎么可能与语言无关?这是相关框架的实现细节。

Spring alows you to specify c'tor args as a list of values/references, if that's your thing. It's not very readable, though, compared to property injection.

Spring允许您将c'tor args指定为值/引用列表,如果这是您的事情。然而,与物业注入相比,它不是很易读。

Some people get hot under the collar about this, and insist that c'tor injection is the only thread-safe approach in java. Technically they're correct, but in practice it tends not to matter.

有些人对这方面的问题很热,并且坚持认为c'tor注入是java中唯一的线程安全方法。从技术上讲,它们是正确的,但在实践中它往往无关紧要。

#4


3  

It should not be a very common need, but sometimes it is a valid one. Ninject, which is lighter than StructureMap, allows you to pass parameters when retrieving transient objects from the context. Spring.NET too.

它不应该是一个非常普遍的需求,但有时它是一个有效的需求。 Ninject比StructureMap轻,允许您在从上下文中检索瞬态对象时传递参数。 Spring.NET也是。

Most of the time, objects declared in an IoC container aren't transient, and accept others non-transient objects through constructors/properties/methods as dependencies.

大多数情况下,在IoC容器中声明的对象不是瞬态的,并且通过构造函数/属性/方法作为依赖性接受其他非临时对象。

However, if you really wan't to use the container as a factory, and if you have enough control on the objects you want to resolve, you could use property or method injection even if it sounds less natural and more risky in some way.

但是,如果您真的不想将容器用作工厂,并且如果您对要解析的对象有足够的控制权,则可以使用属性或方法注入,即使它听起来不那么自然且风险更大。

#5


0  

Yes, other frameworks are more feature-rich - you need to use an ioc framework that allows for constructor injection. Spring is an example of a multi-language ioc container that allows constructor dependency injection.

是的,其他框架功能更丰富 - 您需要使用允许构造函数注入的ioc框架。 Spring是一个允许构造函数依赖注入的多语言ioc容器的示例。

#6


0  

Other IoC frameworks are more feature rich.

其他IoC框架功能更丰富。

I.e. check out the ParameterResolution with Autofac

即使用Autofac检查ParameterResolution

#7


0  

You can also do that with Windsor easily

你也可以轻松地与温莎这样做

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2008/09/24/7201b39374fa756dd65530bdd222a5d9.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告