Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "MissingMethodException: Cannot create an instance of an interface" when binding generic interface to repository with Ninject

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

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.

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

 public ActionResult Index(IRepository<SomeEntityType> repo)
        {


            return View();
        }

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

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

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 just acts as a generic constraint.

   public interface IEntity
{
    Guid Id { get; set; }
}
like image 703
LaserBeak Avatar asked Apr 08 '13 14:04

LaserBeak


2 Answers

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

It should look like this:

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();
    }
}

Make sense?

like image 62
Dan Csharpster Avatar answered Oct 30 '22 17:10

Dan Csharpster


That sort of error usually indicates that you have a different version of the dll at runtime to the one you are referencing in the project.

Try just manually copying all the relevant dlls from your project dirs to your bin directory.

Failing that, check this (admittedly, very old) post for some ideas on how to debug the problem.

like image 43
Steve Avatar answered Oct 30 '22 15:10

Steve