如何获得当前的温莎城堡集装箱? [英] How do I get the current Castle Windsor container?

查看:55
本文介绍了如何获得当前的温莎城堡集装箱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是温莎城堡小子。我有一个很烂的WebForm项目。我正在尝试解决依赖关系以测试用户注册。如何到达当前的WindsorContainer?

I am a Castle Winsor Noob. I have a WebForm project that is a hot mess. I am trying to resolve a dependency to test user registration. How do I get to the current WindsorContainer?

IWindsorContainer container = ???;
IRegistrationLogic registrationLogic = container.Resolve<IRegistrationLogic>();
_registrationLogic.Register();

这是我的引导程序:

public class WindsorConfigTask : ICastleBootstrapperTask
{

    public void Execute()
    {
        Container.AddFacility<WcfFacility>();
        Container.Register(
            Component.For<IProcessMessageRequest>()
                .ActAs(
                    new DefaultClientModel
                    {
                        Endpoint =
                            WcfEndpoint.ForContract<IProcessMessageRequest>().FromConfiguration("surveyClient2")
                    }
                ),
            Component.For<ILocalMembershipService>()
                .ActAs(
                    new DefaultClientModel
                    {
                        Endpoint =
                            WcfEndpoint.ForContract<ILocalMembershipService>().FromConfiguration(
                                "localMembershipClient")
                    })


            );

        Container.Register(Component.For<IRegistrationLogic>()
            .ImplementedBy<RegistrationLogic>()
            .LifeStyle.Is(LifeStyleType));
    }

    public IWindsorContainer Container { get; set; }


    #region ICastleBootstrapperTask Members


    public Castle.Core.LifestyleType LifeStyleType
    {
        get;
        set;
    }

    #endregion
}


推荐答案

有很多方法可以解决此问题,但我认为最常见的方法是创建一个单例助手类来保存引用。请记住,您要让应用程序使用DI来自动从容器中获取所有内容。也许只有几个来自应用程序的呼叫会到达容器。看看温莎的控制器工厂。

There are many ways to solve this problem but I think the most common is to create a singleton helper class to hold the reference. Keep in mind you want to app to use DI to get everything from the container automatically. Perhaps only a few calls from the app will be to the container. Look at the controller factories for Windsor.

类似这样的东西...

Something like this...

public static class ContainerManager
{
    public static IWindsorContainer Container = null;
}

现在我已经知道可以更进一步了,您可以添加一些

Now I have been known to take it a step further and you could include some utilities with a get...

    public static class ContainerManager
    {
        private static IWindsorContainer _container = null;
        public static IWindsorContainer Container
        {
             get {
                 if (_container == null) {
                      // run installers, set _container = new container
                 }
                 return _container;
             }

        }
    }

我也意识到您可能会问我如何从下游依赖对象获取容器...您可以使用自身注册容器。默认情况下,它将注册IKernel,但是您可以稍后注册IWindsorContainer以进行注入。我强烈不建议直接使用该容器。就像上面的代码一样……完成后您会发布吗?

I also realize you might be asking how do I get the container from a downstream dependent object... you can register the container with its self. By default it will register IKernel, but you can register IWindsorContainer for injection later. I would highly discourage using the container directly. As in you code above... do you do a Release when you are done???

这篇关于如何获得当前的温莎城堡集装箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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