开发者

Binding Generic Repository and Specific Repository with Ninject 2.2

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<>))开发者_StackOverflow社区.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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜