3
votes

I have a IRepository that I have implemented in Repository and I extended Repository for specific type as UsersRepository I need to bind all types using the generic binding for Ninject however when requesting instance for IRepository I need to get UsersRepository instead of Repository.

Bind<IDbContext>().To<SMSDataContext>()
.WithConstructorArgument("connectionStringName", "dbcsname");

Here I am binding the generic repository:

Bind(typeof(IRepository<>)).To(typeof(Repository<>))
.WithConstructorArgument("dbContext",new SMSDataContext("dbcsname"));        

Here I am trying to bind a specific instance:

Bind<IRepository<Setting>>().ToConstant(settingsRepository);

Tried different approaches with ".ToConstant" and with only ".To" also tried to bind to concrete implementation like follows:

 UsersRepository usersRepository = new UsersRepository(new SMSDataContext("SMSDB"));
 Bind<IRepository<Setting>>().To<SettingsRepository>().WithConstructorArgument("dbContext", new SMSDataContext("dbscname")); ;  

Please advise.

1
Thanks for the Edit :) .Adel

1 Answers

3
votes

Currently it is only possible using some cheat because open generic bindings have the same priority as closed generic bindings. But you can increase the priority of a binding by adding a condition.

Bind<IRepository<Setting>>().ToConstant(settingsRepository).When(ctx => true);