1

Ok, I feel like a total idiot. I have read the docs and still cant get this working with 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
}

As you can see I am need to inject the contructor for the class above. Well, it is not going so well. Help!! before I go back to writing perl code. Kidding!! LOl

Thoughts folks?

Code Jammr
  • 1,867
  • 3
  • 14
  • 18

1 Answers1

2

Your class ContextAdapter does not implement IContextFactory. Do you have a class like class Factory : IContextFactory? That is what you are missing here. Then you can bind it kernel.Bind<IContextFactory>.To<Factory>() and Ninject will create that type for you when you request an object or when it needs to fulfill a contract. I think your confusion comes from the binding syntax. You are, in general, not binding parameters together, you are binding interfaces to concrete implementations. Here is a quick example:

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.

  • So in order to inject the dependency to satisfy the constructor the class has to inherit from IcontextFactory? Interesting, I was thinking about the logic behind all of this a bit whack then. – Code Jammr Apr 09 '11 at 01:11
  • What if I put the [Inject] Attribute in like so: [Inject] public ContextAdapter(IContextFactory contextFactory) { this._contextFactory = contextFactory; } – Code Jammr Apr 09 '11 at 01:15
  • You still need to bind a concrete implementation of `IContextFactory`. `kernel.Bind.To()` is wrong. –  Apr 09 '11 at 01:52
  • The `[Inject]` attribute on a constructor is only used if you have more than one constructor in your class and you want Ninject to use a specific one. You can also apply it to properties to have them injected. –  Apr 09 '11 at 01:53
  • well the docs must be incorrect then. http://www.docstoc.com/docs/31783175/What-is-Ninject or I am reading it worng :) If ninject sees the [Inject] attribute it will try to activate an object for each of the parameters – Code Jammr Apr 09 '11 at 02:14
  • That is true. But that very document also says that if you have a single contructor, `[Inject]` is optional. –  Apr 09 '11 at 02:25
  • Yeps, I am now at page 53 and reading about how you don't have to use Inject and should use by convention lol... God I love open source – Code Jammr Apr 09 '11 at 02:42
  • So essentially having the factory is not needed if I can use ninject conditional's for Dbcontext. Considering that is really the only dependency I am trying to resolve – Code Jammr Apr 09 '11 at 03:12
  • Have a look at `ToMethod` as a binding target. Your `CreateDbSet` method could be called here if you `IDataSet<>` to it. For example: `kernel.Bind(typeof(IDataSet<>)).ToMethod(ctx => CreateDbSet)`. You might need a bit of experimentation to get it working, but it would eliminate the need for your `ContextAdapter` class. If `typeof(IDataSet<>)` looks strange to you, look at http://stackoverflow.com/questions/2173107/net-what-exactly-is-an-open-generic-type for info on open generics. –  Apr 09 '11 at 03:22
  • funny how we look back at the things we have posted. LOL – Code Jammr Mar 15 '13 at 02:23