Castle Windsor IoC inject Property into Constructor

孤人 提交于 2019-12-24 01:19:41

问题


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?

e.g.

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? (ref to correct windsor doc or example is totally fine too)


回答1:


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


来源:https://stackoverflow.com/questions/17304364/castle-windsor-ioc-inject-property-into-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!