Help with Ninject in mvc3!

2019-06-02 15:22发布

问题:

Heres my problem. My app has several projects.

  • WEB (Controllers and views)
  • Services
  • Data (edmx and Repositories)
  • Entities (POCO)
  • Tests

So in my Web project I have the ninject configuration

[assembly: WebActivator.PreApplicationStartMethod(typeof(PublicPanama.AppStart_NinjectMVC3), "Start")]

    namespace Web{
        public static class AppStart_NinjectMVC3 {
            public static void RegisterServices(IKernel kernel) {
                //kernel.Bind<IThingRepository>().To<SqlThingRepository>();
                kernel.Bind<IContributorService>().To<ContributorService>();
            }

            public static void Start() {
                // Create Ninject DI Kernel 
                IKernel kernel = new StandardKernel();

                // Register services with our Ninject DI Container
                RegisterServices(kernel);

                // Tell ASP.NET MVC 3 to use our Ninject DI Container 
                DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
            }
        }
    }

The problem is, I also want to add

kernel.Bind<IRepository>().To<Repository>();

But my Web project does not have a reference to the Data project.. and just adding the reference for this doesnt seem right..

what am I missing? please help!

回答1:

http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/

And dont forget to read @Brad Wilson's blog series (ref'd in article)