2010-10-08 64 views
0

有沒有辦法將當前作爲依賴項執行的IObjectFactory或ApplicationContext傳遞給對象?例如Spring.Net:注入ApplicationContext/ObjectFactory本身

<object id="SpringActionInvoker" type="xxx.SpringActionInvoker, xxx"> 
    <constructor-arg ref="reference_to_the_ApplicationContext_or_ObjectFactor_that_is_executing" /> 
    </object> 

我想用我Spring.Net實施Asp.Net MVC ControllerActionInvoker的將被注入到控制器

public class SpringActionInvoker : ControllerActionInvoker 
{ 
    private IObjectFactory objectFactory; 

    public SpringActionInvoker(IObjectFactory objectFactory) 
    { 
     this.objectFactory = objectFactory; 
    } 

    protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) 
    { 
     //use objectFactory to inject dependencies into filters 
    } 
} 

回答

4

只是讓你的類實現IApplicationContextAware這應該讓你自動注入當前的IApplicationContext。

0

如何:

<object id="SpringActionInvoker" type="xxx.SpringActionInvoker, xxx" /> 

然後:

public class SpringActionInvoker : ControllerActionInvoker 
{ 
    protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) 
    { 
     IObjectFactory objectFactory = ContextRegistry.GetContext(); 
     //use objectFactory to inject dependencies into filters 
    } 
} 
+0

我想將SpringActionInvoker放入實用工具類中,並且我並不總是使用app.config和ContextRegistry創建ObjectContext – Fabiano 2010-10-08 13:48:43