使用“请求”注入对象范围到原型对象

发布于 2024-10-13 02:13:58 字数 668 浏览 2 评论 0原文

我在我的 Asp.Net MVC 应用程序中使用 Spring.Net,其中控制器必须定义为原型(非单例)。我有必须具有请求范围的对象(每个请求的新对象)。有没有办法将它们注入我的控制器?

  <object type="xx.CompanyController, xx" singleton="false">
    <property name="Service" ref="ServiceA" />
  </object>

  <object id="ServiceA" type="xx.ServiceA, xx" scope="request"/>    
    <property name="ObjectB" ref="ObjectB" />
  </object>

  <object id="ObjectB" type="xx.ObjectB, xx" scope="request"/>

像这样,除了控制器之外的所有对象都被视为单例。 ObjectB 不能是原型,因为它被需要共享同一实例的其他一些对象引用。从控制器中删除 singleton="false" 并添加scope="request" 也不起作用(控制器被视为单例)。

我将 Spring.Net 1.3.1 与 MvcApplicationContext 一起使用

I use Spring.Net in my Asp.Net MVC application where controllers must be defined as prototypes (non singleton). I have objects that must have a request scope (new object for each request). Is there a way to inject them into my controller?

  <object type="xx.CompanyController, xx" singleton="false">
    <property name="Service" ref="ServiceA" />
  </object>

  <object id="ServiceA" type="xx.ServiceA, xx" scope="request"/>    
    <property name="ObjectB" ref="ObjectB" />
  </object>

  <object id="ObjectB" type="xx.ObjectB, xx" scope="request"/>

Like this all objects except the controllers are treated as singleton.
ObjectB must not be prototype beause it is referenced by some other objects that need to share the same instance. Removing singleton="false" from the controller and add scope="request" also doesn't work (controller is treated as singleton).

I use Spring.Net 1.3.1 with MvcApplicationContext

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-10-20 02:13:58

您应该将 ControllerFactory 设置为您自己的。

protected void Application_Start()
{
//...
    ControllerBuilder.Current.SetControllerFactory(new IoCControllerFactory());
//...
}

IoCControllerFactory 必须继承自 DefaultControllerFactory

public class IoCControllerFactory : DefaultControllerFactory

并重写 GetControllerInstance

protected override IController GetControllerInstance (RequestContext requestContext, Type controllerType)
    {
        ObjectTypeUtility.ArgumentIsNull(controllerType, "controllerType", true);
         if (!typeof(IController).IsAssignableFrom(controllerType))
             throw new ArgumentException(string.Format(
                       "Type requested is not a controller: {0}",
                       controllerType.Name), 
                        "controllerType");    

            IController controller = IoCWorker.Resolve(controllerType) 
                                      as IController;                
            return controller;

    }

IoCWorker 是一个具有用于注入的 Resolve 方法的类,我正在使用 Unity,我无法提供 IoCWorker 实现 - 如果您需要我可以共享 - 。

它可以工作 ctor 注入为您的控制器。

祝你好运。

You should set ControllerFactory with your own.

protected void Application_Start()
{
//...
    ControllerBuilder.Current.SetControllerFactory(new IoCControllerFactory());
//...
}

IoCControllerFactory must inherit from DefaultControllerFactory

public class IoCControllerFactory : DefaultControllerFactory

And override GetControllerInstance

protected override IController GetControllerInstance (RequestContext requestContext, Type controllerType)
    {
        ObjectTypeUtility.ArgumentIsNull(controllerType, "controllerType", true);
         if (!typeof(IController).IsAssignableFrom(controllerType))
             throw new ArgumentException(string.Format(
                       "Type requested is not a controller: {0}",
                       controllerType.Name), 
                        "controllerType");    

            IController controller = IoCWorker.Resolve(controllerType) 
                                      as IController;                
            return controller;

    }

IoCWorker is a class has Resolve method for injection, I am using Unity and I could not give IoCWorker implementation -if you need i can share-.

It will work ctor inject for your controller.

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文