获取“MissingMethodException:无法创建接口的实例”当使用Ninject将通用接口绑定到存储库时 [英] Getting "MissingMethodException: Cannot create an instance of an interface" when binding generic interface to repository with Ninject

查看:289
本文介绍了获取“MissingMethodException:无法创建接口的实例”当使用Ninject将通用接口绑定到存储库时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照指南这里 ,而不是StructureMap试图使用Ninject。

Following the guide here, but instead of StructureMap attempting to use Ninject.

它会抛出MissingMethodException:无法创建接口的实例错误,无论何时我尝试注入 IRepository< SomeEntityType> 添加到动作方法中的参数中。

It throws up the "MissingMethodException: Cannot create an instance of an interface" error any time I attempt to inject an IRepository<SomeEntityType> into a parameter in an action method.

更新:同样给出bootstrapper.cs, MVC3 Ninject Nuget包。

Update: Also giving bootstrapper.cs not found, I used the MVC3 Ninject Nuget package.

 public ActionResult Index(IRepository<SomeEntityType> repo)
        {


            return View();
        }

NinjectWebCommon.cs

NinjectWebCommon.cs

        private static void RegisterServices(IKernel kernel)
    {
        string Cname = "VeraDB";
        IDbContext context = new VeraContext("VeraDB");
        kernel.Bind<IDbContext>().To<VeraContext>().InRequestScope().WithConstructorArgument("ConnectionStringName", Cname);
        kernel.Bind(typeof(IRepository<>)).To(typeof(EFRepository<>)).WithConstructorArgument("context",context);

    }      

IRepository

IRepository

    public interface IRepository<T> where T : class
{
    void DeleteOnSubmit(T entity);
    IQueryable<T> GetAll();
    T GetById(object id);
    void SaveOrUpdate(T entity);
}

EFRepository

EFRepository

    public class EFRepository<T> : IRepository<T> where T : class, IEntity
{
    protected readonly IDbContext context;
    protected readonly IDbSet<T> entities;

    public EFRepository(IDbContext context)
    {
        this.context = context;
        entities = context.Set<T>();
    }

    public virtual T GetById(object id)
    {
        return entities.Find(id);
    }

    public virtual IQueryable<T> GetAll()
    {
        return entities;
    }

    public virtual void SaveOrUpdate(T entity)
    {
        if (entities.Find(entity.Id) == null)
        {
            entities.Add(entity);
        }

        context.SaveChanges();
    }

    public virtual void DeleteOnSubmit(T entity)
    {
        entities.Remove(entity);

        context.SaveChanges();
    }
}

IEntity只是作为一个泛型约束。

IEntity just acts as a generic constraint.

   public interface IEntity
{
    Guid Id { get; set; }
}


推荐答案

错误。 Ninject将参数注入到您的构造函数中,但是您向索引控制器操作添加了参数。

I made the same simple mistake. Ninject injects parameters into your constructor, but you added parameters to the Index Controller action.

它应该看起来像这样:

public class HomeController : Controller
{
    private IRepository<SomeEntityType> _repo;

    public HomeController(IRepository<SomeEntityType> repo)
    {
        _repo= repo;
    }

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application. " +
                          _repo.HelloWorld();

        return View();
    }
}

有意义吗?

这篇关于获取“MissingMethodException:无法创建接口的实例”当使用Ninject将通用接口绑定到存储库时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆