I want to be able to use ninject to inject a different type of DBContext into different areas of my application. Does anyone know if you can do this with a ninject When clause?
Something along the lines of:
Bind<DbContext>().To<ServiceContext1>().When(t=>t.Target.Type.Assembly.FullName.StartsWith("Namespace.Context1."));
Bind<DbContext>().To<ServiceContext2>().When(t => t.Target.Type.Assembly.FullName.StartsWith("Namespace.Context2."));
Bind(typeof(IRepositoryDataSource<>)).To(typeof(DbContextRepositoryDataSource<>));
Bind(typeof(IRepository<>)).To(typeof(SimpleRepository<>));
request.Target.Typereturns the parameter type. You are looking forrequest.Target.Member.ReflectedType. Since you inject the context into the identical repository you probably have to check on the parent request usingrequest.ParentRequestor even the grand parent rather than on the request because there you will get always the repository type.