Castle Windsor IoC inject Property into Constructo

2019-07-20 05:20发布

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条回答
Juvenile、少年°
2楼-- · 2019-07-20 05:26
container.Register(Component.For<ISession>()
    .UsingFactoryMethod(() => container
        .Resolve<ISessionManager>().CurrentSession)
    .LifeStyle.Transient);
查看更多
登录 后发表回答