需要将Castle.Windsor作为Asp.Net MVC ControllerFactory的帮助 [英] Need help with Castle.Windsor as an Asp.Net MVC ControllerFactory please

查看:48
本文介绍了需要将Castle.Windsor作为Asp.Net MVC ControllerFactory的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从他的专业Asp.Net MVC框架书(很棒的书,btw)中实现史蒂文·桑德森的WinsorControllerFactory,但我正陷入一个问题。我不确定您还需要知道什么才能做出回应,但对此我将不胜感激。谢谢!

I'm trying to implement Steven Sanderson's WinsorControllerFactory from his pro Asp.Net MVC Framework book (great book, btw) and I'm stumbling into an issue. I'm not sure what else you'll need to know in order to formulate a response but I greatly appreciate any help in this. Thanks!

这里是代码:

WindsorControllerFactory

public class WindsorControllerFactory : DefaultControllerFactory
{
    private WindsorContainer _container;

    public WindsorControllerFactory()
    {
        _container= new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
        var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                              where typeof (IController).IsAssignableFrom(t)
                              select t;
        foreach(Type t in controllerTypes)
        {
            _container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
        }
    }
    protected override IController GetControllerInstance(Type controllerType)
    {
        return (IController)_container.Resolve(controllerType);
    }
}

Web.Config

  <castle>
    <components>
      <component id="MenuRepository"
                 service="****.IMenuRepository, ****.Model"
                 type="****.FakeMenuRepository, ****.Model">
      </component>
      <component id="NewsRepository"
                 service="****.INewsRepository, ****.Model"
                 type="****.FakeNewsRepository, ****.Model">
      </component>
    </components>
  </castle>

NewsArticleController

public class NewsArticleController : Controller
{
    private INewsRepository _repository { get; set; }
    public NewsArticleController(INewsRepository repository)
    {
        _repository = repository;
    }

Global.asax

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
        ControllerBuilder.Current.SetControllerFactory((new WindsorControllerFactory()));
    }

错误消息
没有用于支持该服务****。NewsArticleController被发现
描述:当前Web请求的执行过程中发生未处理的异常。请查看堆栈跟踪以获取有关该错误及其在代码中起源的详细信息。

ERROR MESSAGE No component for supporting the service ****.NewsArticleController was found Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service ****.NewsArticleController was found

Source Error:

Line 29:         protected override IController GetControllerInstance(Type controllerType)
Line 30:         {
Line 31:             return (IController)_container.Resolve(controllerType);
Line 32:         }
Line 33:     }


推荐答案

S#arpArchitecture项目可以将Windsor用作DI框架(用于控制器或其他任何东西),几乎不需要添加web.config部分-> http://code.google.com/p/sharp-architecture/

The S#arpArchitecture project has a decent implentation of using Windsor as a DI framework (for controllers or anything else), with little or no need to add web.config sections - http://code.google.com/p/sharp-architecture/

代码示例:

CastleWindsor / ComponentRegistrar.cs:

CastleWindsor/ComponentRegistrar.cs:

public class ComponentRegistrar
{
    public static void AddComponentsTo(IWindsorContainer container) {
        AddGenericRepositoriesTo(container);
        AddCustomRepositoriesTo(container);

        container.AddComponent("validator",
            typeof(IValidator), typeof(Validator));
    }

    private static void AddCustomRepositoriesTo(IWindsorContainer container) {
        container.Register(
            AllTypes.Pick()
            .FromAssemblyNamed("Northwind.Data")
            .WithService.FirstNonGenericCoreInterface("Northwind.Core"));
    }

    private static void AddGenericRepositoriesTo(IWindsorContainer container) {
        container.AddComponent("entityDuplicateChecker",
            typeof(IEntityDuplicateChecker), typeof(EntityDuplicateChecker));
        container.AddComponent("repositoryType",
            typeof(IRepository<>), typeof(Repository<>));
        container.AddComponent("nhibernateRepositoryType",
            typeof(INHibernateRepository<>), typeof(NHibernateRepository<>));
        container.AddComponent("repositoryWithTypedId",
            typeof(IRepositoryWithTypedId<,>), typeof(RepositoryWithTypedId<,>));
        container.AddComponent("nhibernateRepositoryWithTypedId",
            typeof(INHibernateRepositoryWithTypedId<,>), typeof(NHibernateRepositoryWithTypedId<,>));
    }
}

Global.asax(di的主要初始化方法):

Global.asax (primary initialization method for di):

protected virtual void InitializeServiceLocator() {
        IWindsorContainer container = new WindsorContainer();
        ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));

        container.RegisterControllers(typeof(HomeController).Assembly);
        ComponentRegistrar.AddComponentsTo(container);

        ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
    }

这篇关于需要将Castle.Windsor作为Asp.Net MVC ControllerFactory的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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