1

好吧,我觉得自己像个白痴。我已经阅读了文档,但仍然无法与 Ninject 一起使用。

 public class ContextAdapter:IDbSetProvider
{
    private readonly IContextFactory _contextFactory;
    #region Implementation of IDbSetProvider

    public ContextAdapter(IContextFactory contextFactory)
    {
        this._contextFactory = contextFactory;
    }

    public IDbSet<TEntity> CreateDBSet<TEntity>() where TEntity : class
    {
        var context = _contextFactory.Create();
        return context.Set<TEntity>();
    }

    #endregion
}

如您所见,我需要为上面的类注入构造函数。好吧,事情进展得不太顺利。帮助!!在我回到编写 perl 代码之前。开玩笑!!哈哈

想的人?

4

1 回答 1

2

您的课程ContextAdapter没有实现IContextFactory. 你有这样的课class Factory : IContextFactory吗?这就是你在这里所缺少的。然后你可以绑定它kernel.Bind<IContextFactory>.To<Factory>(),当你请求一个对象或它需要履行合同时,Ninject 会为你创建该类型。我认为您的困惑来自绑定语法。通常,您不是将参数绑定在一起,而是将接口绑定到具体实现。这是一个简单的例子:

Bind<IEngine>.To<GasEngine>();
Bind<ICar>.To<Sedan>();

class Sedan : ICar
{
    public Sedan(IEngine engine) { }
}

// ...

kernel.Get<ICar>(); // get me a new car

When you ask Ninject for ICar, it will fulfill it with what was bound, Sedan. Sedan requires an IEngine in its constructor, which Ninject will fulfill with GasEngine since that is what was bound.

于 2011-04-09T00:45:30.540 回答