Ninject / DI具体实施

时间:2016-08-16 21:27:58

标签: c# asp.net-mvc dependency-injection ninject

我有以下课程。我不会列出接口,但假设每个公共方法都在接口上定义。

public class LabelRepo : ILabelRepo
{
    private readonly IApplicationContext _appContext;

    public IEnumerable<ILabel> GetAllLabels()
    {
        int userId = _appContext.UserId;

        //some code here to ask a database to get me all labels belonging to the user Id;

        return listOfLabelsForThatUser; 
    }

    public LabelRepo(IApplicationContext appContext)
    {
        _appContext = appContext;
    }
}

public class ApplicationContext : IApplicationContext
{
    public int UserId { get; }

    public ApplicationContext(IUser user)
    {
       UserId = user.UserId;
    }
}

And User : IUser is someObject with fields userId, username, etc...

我遇到的问题是:

public ActionResult Login(string username, string password)
{
     IUser user = _serviceThatValidates(username, password);
     return RedirectToAction("See", "MethodBelow");
}

public ActionResult MethodBelow()
{

    var labels = _labelRepo.GetAllLabels();
}

似乎_labelRepo的{​​{1}}包含空ApplicationContext,这有点意义。有没有办法告诉NinjectKernel在登录方法中使用IUser并将其提供给IUser

此外,这是唯一存在ApplicationContext的实例。

0 个答案:

没有答案