缺少类之间的引用转换

时间:2017-05-06 09:37:46

标签: c# asp.net-mvc

大家好!我在方法AddBindings中遇到了Ninject的问题 - 无法正确理解解决此问题最好的方法:

  

错误CS0311类型'ClassLibrary1.Concrete.EFGameRepository'不能   在泛型类型中用作类型参数'TImplementation'   方法'IBindingToSyntax.To()'。   没有隐式引用转换   'ClassLibrary1.Concrete.EFGameRepository'来   'ClassLibrary1.Abstract.IGameRepository'。

以下是代码:

namespace GameStore.WebUI.Infrastructure
{
    public class NinjectDependencyResolver : IDependencyResolver
    {
        private IKernel kernel;
        public NinjectDependencyResolver(IKernel kernelParam)
        {
            kernel = kernelParam;
            AddBindings();
        }

        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }

        private void AddBindings()
        {

            kernel.Bind<IGameRepository>().To<EFGameRepository>();
        }
    }
}

EFGameRepository

public class EFGameRepository 
{ 
    EFDbContext context = new EFDbContext(); 
    public IEnumerable<Game> Games { get { return context.Games; } } 
}

1 个答案:

答案 0 :(得分:0)

要解决此问题,EFGameRepository必须实施IGameRepository

public class EFGameRepository : IGameRepository
{
    // implement IGameRepository members implicit or explicit
}