I have a generic repository, abstract and concrete as below

public interface IGenericRepository<T> where T : class

public class GenericRepository<T> : IGenericRepository<T> where T : class

I then have an repository that inherits from this, again abstract and concrete

public interface ISkyDiveCentreRepository : IGenericRepository<DiveCentre>

public class SkyDiveCentreRepository : GenericRepository<DiveCentre>

In my ninject config I then have

kernel.Bind(typeof(IGenericRepository<>)).To(typeof(GenericRepository<>));
kernel.Bind<ISkyDiveCentreRepository>().To<SkyDiveCentreRepository>();

This is the first time I've tried to do this but am getting the error:

Error   2   The type 'UKSkyDiveCentres.DAL.imp.SkyDiveCentreRepository' cannot be used as type parameter 'TImplementation' in the generic type or method 'Ninject.Syntax.IBindingToSyntax<T1>.To<TImplementation>()'. There is no implicit reference conversion from 'SkyDiveCentres.DAL.imp.SkyDiveCentreRepository' to 'SkyDiveCentres.DAL.ISkyDiveCentreRepository'. C:SOMEPATH\UKSkyDiveCentres\App_Start\NinjectWebCommon.cs   56  13  SkyDiveCentres
有帮助吗?

解决方案

Your SkyDiveCentreRepository inherits from GenericRepository<DiveCentre> and doesn't implement the ISkyDiveCentreRepository interface.

Simply explicitly implement it:

public class SkyDiveCentreRepository : 
    GenericRepository<DiveCentre>, ISkyDiveCentreRepository
                               //  ^^^^^^^^^^^^^^^^^^^^^^^^ this

Without it.. you can't do simple things like this:

ISkyDiveCentreRepository repo = new SkyDiveCentreRepository();

If you can't do it.. Ninject can't either.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top