简体   繁体   English

温莎城堡IoC将财产注入构造函数

[英]Castle Windsor IoC inject Property into Constructor

I have a session manager class that has a session property. 我有一个具有会话属性的会话管理器类。 I need to pass that into another class as a constructor parameter. 我需要将其作为构造函数参数传递给另一个类。 How should I configure the installer for castle windsor? 我应该如何配置温莎城堡的安装程序?

eg 例如

public interface ISessionManager
{
    ISession CurrentSession { get; set; }
}

public class SessionManager : ISessionManager
{
    private ISession _session;
    public ISession CurrentSession
    {
        get { return _session ?? (_session = NHibernateHelper.OpenSession()); }
        set { _session = value; }
    }
}

public interface IRequest
{
    TR Execute<TR>(IExecuteManager<TR> executeManager);
}

public class Request: IRequest
{
    private readonly ISession _session;

    public Request(ISession session)
    {
        _session = session;
    }
    public TR Execute<TR>(IExecuteManager<TR> executeManager)
    {
        return executeManager.Request(_session);
    }
}

I've been rooting around in the castle windsor docs but I must be searching for the wrong thing or missing something, because I'm sure it is there, just can't find it. 我一直在温莎城堡文档中扎根,但我必须寻找错误的东西或缺少某些东西,因为我确定它在那里,只是找不到。

How should I configure the castle windsor installer so that the SessionManager.CurrentSession is injected into the Request class' constructor? 如何配置城堡式Windsor安装程序,以便将SessionManager.CurrentSession注入到Request类的构造函数中? (ref to correct windsor doc or example is totally fine too) (参考正确的Windsor文档或示例也完全可以)

container.Register(Component.For<ISession>()
    .UsingFactoryMethod(() => container
        .Resolve<ISessionManager>().CurrentSession)
    .LifeStyle.Transient);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM