Ninject configuration - generics
I have a group of base repositories that are setup like this ...
Bind<IRepository<SomeObject>>().To<SomeObjectRepository>().WithConstructorArgument("connection", connection);
Bind<IRepository<SomeOtherObject>>().To<SomeOtherObjectRepository>().WithConstructorArgument("connection", connection);
//and so on
Those repositories are simple - the whole class goes as such.
public class SomeObjectRepository : Repository<SomeObject>
{
public SomeObjectRepository (string connection) : base(connection)
{
}
}
So I thought, hey that's kinda dumb, so I created a generic base repository and replaced all those bindings with this.
Bind(typeof(IRepository<>)).To(typeof(Repository<>)).W开发者_运维问答ithConstructorArgument("connection", connection);
which of course broke and got hit with an error
Error activating IRepository{SomeObject} using binding from IRepository{T} to Repository{T} No constructor was available to create an instance of the implementation type.
Why wouldn't this work?
A little late but you need to be binding IRepository to SomeObjectRepository.
精彩评论