开发者

Ninject configuration - generics

开发者 https://www.devze.com 2023-02-03 21:01 出处:网络
I have a group of base repositories that are setup like this ... Bind<IRepository<SomeObject>>().To<SomeObjectRepository>().WithConstructorArgument(\"connection\", connection);

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.

0

精彩评论

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

关注公众号