2

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)

4

1 回答 1

1
container.Register(Component.For<ISession>()
    .UsingFactoryMethod(() => container
        .Resolve<ISessionManager>().CurrentSession)
    .LifeStyle.Transient);
于 2013-06-25T18:11:29.190 回答