hmemcpy / AgentMulder

** THIS PLUGIN IS NO LONGER MAINTAINED. PLEASE FOLLOW ERNICOMMUNITY FOR UPDATES **
https://github.com/ERNICommunity/AgentMulder
MIT License
151 stars 33 forks source link

Not working with NinJect? #10

Closed jclifford closed 12 years ago

jclifford commented 12 years ago

I have a very simple repository test using NinJect, but it appears that AgentM is not working at all. All the "old" ReSharper warnings and error messages are still displayed :(

AgentM: 1.0.3.0, ReSharper: 6.1.1000.82 VS: 10.0.40219.1 NinJect: 4.0.30319 - No other Resharper plug-ins installed.

Am I missing something obvious?

NinJect binding:

    private static void registerServices(IKernel kernel)
    {
        kernel.Bind<IPageRepository>().To<PageRepository>();
    }

Repository and Interface:

public interface IPageRepository : IRepository<Page>
{
    Page GetSingle(int id);
}

public class PageRepository : Repository<Context, Page>, IPageRepository   // Class PageRepository is never instantiated
{
    public Page GetSingle(int id)
    {
        return GetAll().FirstOrDefault(x => x.Id == id);
    }
}

Usage:

public partial class PageController : Controller
{
    private readonly IPageRepository m_repository;

    public PageController(IPageRepository repository)  // Constructor  PageController is never used
    {
        this.m_repository = repository;
    }
hmemcpy commented 12 years ago

Oh wow! My first real bug! Thank you very much for the report!

I figured out what's wrong! Since Agent Mulder works by matching patterns via ReSharper SSR, I used the abstract class Ninject.Syntax.BindingRoot as the base class for the search expression. I did this because this class is common to both StandardKernel and a Ninject Module - they both derive from this class.

However you have moved your registrations to another method which only took IKernel - IKernel does not implement the abstract BindingRoot, but rather the interface IBindingRoot, and that's why the pattern matching failed.

I will fix this in the next version of Agent Mulder, in the mean time, you can work around this problem by passing BindingRoot or StandardKernel as the parameter type to your registerServices method.

Very cool bug, thanks again!

hmemcpy commented 12 years ago

Hi again, didn't want to keep you waiting, the fix was literally 1 letter change (well, in several places :)) Please get the hotfix here: https://github.com/downloads/hmemcpy/AgentMulder/AgentMulder1.0.3.1.msi

If this works for you, please feel free to close the issue :)

jclifford commented 12 years ago

Many thanks for your prompt reply and excellent work!

AgentM 1.0.3.1 fixes the problem :)

Incidentally the use of registerServices(IKernel kernel) is direct from the Nuget package Ninject.MVC3 which adds some MVC specific code for Ninject, so I am sure someone else would have come up with the same problem.

Thanks again for an excellent plugin.