如何使用ninject注入会话对象属性?

时间:2012-12-20 14:19:34

标签: c# ninject

我有一个服务对象可以对特定患者做一些工作。

public class PatientDxService
{
    public Patient Patient { get; set; }

    public PatientDxService(Patient patient)
    {
        this.Patient = patient;
    }
}

我的服务接收一个Patient对象,如上所示。

我有一个SessionManager对象,它有一个属性来获取会话患者。我想给患者注射。

Bind<PatientDxService>().ToConstructor(x => new PatientDxService(x.Inject<ISessionManager>().Patient));

Bind<ISessionManager>().To<SessionManager>().InSingletonScope();

以上对我不起作用。我真的不想注入ISessionManager,因为如果我想使用Web范围之外的服务,那就没有意义了。

1 个答案:

答案 0 :(得分:0)

将ISessionManager注入您的服务而不是患者。然后在构造函数中(或者更好,在您实际使用患者的地方),从会话中请求患者。

public class PatientDxService
{
    private readonly ISessionManager _session;

    public PatientDxService(ISessionManager session)
    {
        this._session = session;
    }

    public void DoStuff()
    {
        var patient = _session.GetPatient();
        ...
    }
}
相关问题