如何將參數傳遞給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

你也可以輕松地與溫莎這樣做


注意!

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



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