【问题标题】:ASP.NET MVC Using Castle Windsor IoCASP.NET MVC 使用 Castle Windsor IoC
【发布时间】:2009-09-18 16:56:01
【问题描述】:

我有一个应用程序,以 Apress Pro ASP.NET MVC 的应用程序为模型,它使用 Castle Windsor 的 IoC 来实例化控制器及其各自的存储库,并且运行良好

例如

public class ItemController : Controller
{
    private IItemsRepository itemsRepository;
    public ItemController(IItemsRepository windsorItemsRepository)
    {
        this.itemsRepository = windsorItemsRepository;
    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
using System.Reflection;
using Castle.Core;

namespace WebUI
{
    public class WindsorControllerFactory : DefaultControllerFactory
    {
        WindsorContainer container;

        // The constructor:
        // 1. Sets up a new IoC container
        // 2. Registers all components specified in web.config
        // 3. Registers all controller types as components
        public WindsorControllerFactory()
        {
            // Instantiate a container, taking configuration from web.config
            container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));

            // Also register all the controller types as transient
            var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                                  where typeof(IController).IsAssignableFrom(t)
                                  select t;
            foreach (Type t in controllerTypes)
                container.AddComponentWithLifestyle(t.FullName, t, LifestyleType.Transient);
        }

        // Constructs the controller instance needed to service each request
        protected override IController GetControllerInstance(Type controllerType)
        {
            return (IController)container.Resolve(controllerType);
        }
    }
}

控制控制器的创建。

我有时需要在控制器中创建其他存储库实例,以从其他地方获取数据,我可以使用 CW IoC 执行此操作吗?如果可以,那么如何操作?

我一直在尝试创建新的控制器类,因为它们应该使用我现有的代码自动注册(如果我可以让它工作,我可以稍后正确注册它们)但是当我尝试实例化它们时一个明显的反对意见,因为我不能为构造函数提供一个 repos 类(我很确定这是错误的方法)。

任何帮助(尤其是示例)将不胜感激。 干杯 MH

【问题讨论】:

  • 你最后的结论是什么。我有同样的设计问题。
  • 看到这个后不久,我发现应用程序中存在内存泄漏,这来自 Castle Windsor 代码中的某个地方(是否是我使用不正确,我不知道,但我只是在一个简单的级别上使用它,所以我不能 100% 确定它我)所以我没有去检查这些解决方案 - 抱歉。如果您尝试以下解决方案并且有效,请告诉我,我会将其标记为正确。

标签: inversion-of-control castle-windsor


【解决方案1】:

只需在控制器构造函数中声明所需的依赖项,即:

public class MyController: Controller {
  private readonly IItemsRepository itemsRepo;
  private readonly IPersonRepository personRepo;
  public MyController(IItemsRepository i, IPersonRepository p) {
    itemsRepo = i;
    personRepo = p;
  }
}

Windsor 在实例化控制器时会自动解析依赖关系。

有很多关于谷歌代码的项目可以用来指导,例如WineCellarManager

顺便说一句:您不需要编写自己的 WindsorControllerFactory,您可以从 MVCContrib 获得(以及更多)

【讨论】:

  • 这并不总是实用的,例如在对象验证规则中,我需要检查针对主数据库添加的任何部件号 - 最好在对象中执行此操作,因为它可以避免业​​务逻辑控制器,但对象没有存储库(我也不想创建一个,除非我需要检查数据)
  • 验证与此无关...请为此创建另一个问题
  • 确实如此,因为在我的验证规则中,我需要访问数据库存储库。
  • 它与 Windsor 的接线控制器和服务无关。这更像是一个设计问题,而不是一个操作问题。请创建另一个问题...
【解决方案2】:

上一版本的windor castle 已经不行了,事实上,微内核组件在castle.core 中融化了

【讨论】:

  • 你的大黄鱼内尔在城堡里融化了?听起来像特洛伊木马,我的意思是青蛙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
相关资源
最近更新 更多