Spring.Net&安培;属性注入 [英] Spring.Net & Attribute Injection

查看:181
本文介绍了Spring.Net&安培;属性注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要依赖注入使用Spring.Net在ASP.NET MVC的属性,我的属性是这样的(注意这是所有伪code,我刚才输入)...

 公共类InjectedAttribute:ActionFilterAttribute
{
    私人IBusinessLogic businessLogic;    公共InjectedAttribute(IBusinessLogic businessLogic)
    {
        this.businessLogic = businessLogic;
    }    公共覆盖无效OnActionExecuting(ActionExecutedContext filterContext)
    {
        //做一些业务逻辑
        businessLogic.DoSomethingImportant();
    }
}

我使用一个控制器工厂创建还可与不同的业务逻辑对象注入的控制器。我越来越从IoC容器控制器这样的...

  ContextRegistry.GetContext()GetObject的(MyMVCController);

我在我的配置控制器像这样传递业务逻辑

 <对象名称=MyMVCControllerTYPE =MyMVC.MyMVCController,MyMVC>
    <构造带参数的指数=0REF =businessLogic/>
< /对象>

有没有配置属性的注射的方法吗?我真的不希望把这个变成我的属性...

 公共类InjectedAttribute:ActionFilterAttribute
{
    私人IBusinessLogic businessLogic;    公共InjectedAttribute(IBusinessLogic businessLogic)
    {
        this.businessLogic = ContextRegistry.GetContext()GetObject的(businessLogic);
    }
    ....


解决方案

  

我在我的配置控制器像这样传递业务逻辑


本控制器定义为单身意味着它们将可能是灾难性的所有请求中被重用。确保控制器没有定义为单例:

 <对象名称=AnotherMovieFinderTYPE =MyMVC.MyMVCController,MyMVC单身=false的>
    <构造带参数的指数=0REF =businessLogic/>
< /对象>

现在,这是说,让我们回到关于属性的主要问题。

由于要在您的过滤器构造器注入可以不再装点任何控制器或动作与他们的属性值必须在编译时是已知的。您需要一种机制,在运行时应用这些过滤器,控制器/行动。

如果您使用的是ASP.NET MVC 3,你可以写一个的自定义过滤器提供商将由注入依赖进去你的行动过滤器应用到所需的控制器/行动。

如果您使用的是旧版本,你可以使用<一个href=\"http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/\"相对=nofollow>自定义ControllerActionInvoker 。

I want to dependency inject an attribute in ASP.NET MVC using Spring.Net, my attribute is something like this (note this is all pseudo code I've just typed in)...

public class InjectedAttribute : ActionFilterAttribute
{
    private IBusinessLogic businessLogic;

    public InjectedAttribute(IBusinessLogic businessLogic)
    {
        this.businessLogic = businessLogic;
    }

    public override void OnActionExecuting(ActionExecutedContext filterContext)
    {
        // do something with the business logic
        businessLogic.DoSomethingImportant();
    }
}

I'm using a controller factory to create the Controllers which are also injected with various business logic objects. I'm getting the controllers from the IoC container like this...

ContextRegistry.GetContext().GetObject("MyMVCController");

I'm configuring my Controllers like so passing in the business logic

<object name="MyMVCController" type="MyMVC.MyMVCController, MyMVC">
    <constructor-arg index="0" ref="businessLogic" />
</object>

Is there a way to configure the injection of the attributes? I don't really want to put this into my attributes...

public class InjectedAttribute : ActionFilterAttribute
{
    private IBusinessLogic businessLogic;

    public InjectedAttribute(IBusinessLogic businessLogic)
    {
        this.businessLogic = ContextRegistry.GetContext().GetObject("businessLogic");
    }
    ....

解决方案

I'm configuring my Controllers like so passing in the business logic

This defines controllers as singletons meaning that they will be reused among all requests which could be catastrophic. Ensure controllers are not defined as singletons:

<object name="AnotherMovieFinder" type="MyMVC.MyMVCController, MyMVC" singleton="false">
    <constructor-arg index="0" ref="businessLogic" />
</object>

Now, this being said let's go back to the main question about attributes.

Because you want constructor injection in your filters you can no longer decorate any controllers or actions with them as attribute values must be known at compile time. You need a mechanism to apply those filters at runtime to controllers/actions.

If you are using ASP.NET MVC 3 you could write a custom filter provider which will apply your action filter to desired controllers/actions by injecting dependencies into it.

If you are using an older version you could use a custom ControllerActionInvoker.

这篇关于Spring.Net&安培;属性注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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