2

I have a generic repository that takes DbContext as a constructor parameter. To inject it my binding looks as such:

Bind<DbContext>().To<MyApplicationsContext>().InRequestScope();

I also have custom repositories, which take MyApplicationContext directly as a constructor parameter. How do I write a binding as such that regardless whether a repository requests the base class DbContext or the inheriting class MyApplicationContext it gets the same instance InRequestScope?

e36M3
  • 5,952
  • 6
  • 36
  • 47

1 Answers1

4

What you'll want to use is an overload of the Bind<>() method.

In your case your code would be:

Bind<DbContext, MyApplicationsContext>().To<MyApplicationsContext>().InRequestScope();

In case you want to use open generics or need to bind using System.Type you can use this overload

Bind(typeof(DbContext), typeof(MyApplicationsContext))
    .To(typeof(MyApplicationsContext))
    .InRequestScope();
Maximo Dominguez
  • 2,075
  • 18
  • 35