温莎城堡的IoC物业注射简单的操作方法 [英] Castle Windsor IoC Property Injection simple how-to

查看:132
本文介绍了温莎城堡的IoC物业注射简单的操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我觉得有可能是的的有关温莎城堡的信息,因为寻找这些关键词给了我一切的例子,坦白说,我不知道有足够的了解它是如何工作正确地解决此。我曾尝试与点点运气相当多的排列在这一点上。

OK I think there is maybe too much information about Castle Windsor because looking for these keywords gives me examples of everything, and frankly I don't understand enough about how it works to properly troubleshoot this. I have tried quite a few permutations with little luck at this point.

我有一个 IUnitOfWorkFactory ,我想要实例为单例。所以,我安装温莎城堡,写一点code像这样:

I have an IUnitOfWorkFactory that I want to instantiate as a singleton. So, I install Castle Windsor, write a bit of code like so:

iocContainer = new WindsorContainer()
    .Install(FromAssembly.This());

var propInjector = iocContainer.Register(
    Component.For<IUnitOfWorkFactory>()
    .LifestyleSingleton()
    .Instance(new NHUnitOfWorkFactory())
    );

propInjector.Resolve<IUnitOfWorkFactory>();

这会从我的的Application_Start 方法调用。

我有一个的AccountController 有线了,像这样:

I have an AccountController wired up like so:

public class AccountController : SecureController
{
    public IUnitOfWorkFactory UnitOfWorkFactory { get; set; }
    ...

...至于我自己看着办,这应该只是工作(虽然不问我怎么)。但是,我的财产总是空当我尝试使用它。

...as far as I can figure, this should just "work" (although don't ask me how). But my property is always null when I try to use it.

好像我失去了一些东西愚蠢和简单,但我不知道它是什么。

It seems like I'm missing something silly and simple, but I have no idea what it is.

我也试过

var propInjector = iocContainer.Register(
    Component.For<IUnitOfWorkFactory>()
    .ImplementedBy<NHUnitOfWorkFactory>()
    .LifestyleSingleton()
    );

,但没有成功。

我在做什么错了?

结论

我在这里缺少的几个步骤。我已经建立了一个安装程序,并按照<一个引导程序href=\"http://docs.castleproject.org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx\">tutorial,但我注册了我的服务在错误的地方......构建控制器工厂之前。现在我的引导程序是这样的:

I was missing several steps here. I had built an installer and a bootstrapper per the tutorial, but I registered my services at the wrong spot... before building the controller factory. Now my bootstrapper looks like this:

iocContainer = new WindsorContainer()
    .Install(FromAssembly.This());

var controllerFactory = new WindsorControllerFactory(iocContainer.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);

iocContainer.Register(
    Component.For<IUnitOfWorkFactory>()
    .ImplementedBy<NHUnitOfWorkFactory>()
    .LifestyleSingleton()
    );

...和我的财产注射已不再空....现在我只需要调试等87问题...

... and my property injections were no longer null.... now I just have to debug the other 87 problems...

推荐答案

这两个构造和财产注入工作时,根对象由容器解决。在这种情况下,你的的AccountController 将根对象温莎需要创建。

Both constructor and property injection work when the root object is resolved by the container. In this case, yourAccountController would be the root object that Windsor would need to create.

为了这样组装起来,你应该使用<一个href=\"http://docs.castleproject.org/Windsor.Windsor-tutorial-part-two-plugging-Windsor-in.ashx\">controller工厂。一旦控制器注册和容器解决,像您期望的一切都应该工作。

In order to wire this up, you should use a controller factory. Once the controller is registered and resolved by the container, everything should work as you expect.

这篇关于温莎城堡的IoC物业注射简单的操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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