2012-02-15 59 views
3

我正在学习ASP.NE4 MVC3。我创建了一个NinjectDependencyResolver类,但我想知道如何去实现ServiceLocator类。目前我收到此错误“类型SportsStore.WebUI.Infrastructure.NinjectDependencyResolver似乎并未实现Microsoft.Practices.ServiceLocation.IServiceLocator。 参数名称:commonServiceLocator”。ninject依赖解析器和服务定位器实现

Global.asax 
     protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 

      RegisterGlobalFilters(GlobalFilters.Filters); 
      RegisterRoutes(RouteTable.Routes); 
      RegisterDependencyResolver(); 

      //ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); 
     } 

     private void RegisterDependencyResolver() 
     { 
      var kernel = new StandardKernel(); 

      DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel)); 

     } 

     NinjectDepencyResolver cs 
      public class NinjectDependencyResolver 
    { 
     private readonly IKernel _kernel; 

     public NinjectDependencyResolver(IKernel kernel) 
     { 
      _kernel = kernel; 
     } 

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

     public IEnumerable<object> GetServices(Type serviceType) 
     { 
      try 
      { 
       return _kernel.GetAll(serviceType); 
      } 
      catch (Exception) 
      { 
       return new List<object>(); 
      } 
     } 

回答

1

我不会那样做。首先,Mark Seemann的书“.NET中的依赖注入”清楚地表明Service Locator实际上是一种反模式。

在任何情况下尽量不要臃肿的Global.asax文件

如果改为使用的NuGet并得到了最新版NinjectMVC3的,你应该用干净的Application_Start方法最终

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 
    } 

然而,如果你愿意,你可以在这个方法的最后添加这个方法,因为我相信这是Adam和Steve在Apress MVC3书中的Sportstore应用程序中所做的。

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); 

因为这本书一经发布,Ninject发布新版本,使得它更容易,其实我也保证,那最终走出Apress的MVC4本书将展现更简单的方法。简单的方法是使用nuget并获取NinjectMVC3,然后它将有一个App_Start文件夹,它将在应用程序启动时运行它们中的文件。

这里是它的一个例子与一些绑定

using Products.Data.Abstract; 
using Products.Data.Concrete; 
using Products.Data.Infrastructure; 

[assembly: WebActivator.PreApplicationStartMethod(typeof(ProductsWeb.App_Start.NinjectMVC3), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(ProductsWeb.App_Start.NinjectMVC3), "Stop")] 

namespace ProductsWeb.App_Start 
{ 
    using System.Reflection; 
    using Microsoft.Web.Infrastructure.DynamicModuleHelper; 
    using Ninject; 
    using Ninject.Web.Mvc; 

public static class NinjectMVC3 
{ 
    private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

    /// <summary> 
    /// Starts the application 
    /// </summary> 
    public static void Start() 
    { 
     DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule)); 
     DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule)); 
     bootstrapper.Initialize(CreateKernel); 
    } 

    /// <summary> 
    /// Stops the application. 
    /// </summary> 
    public static void Stop() 
    { 
     bootstrapper.ShutDown(); 
    } 

    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    private static IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     RegisterServices(kernel); 
     return kernel; 
    } 

    /// <summary> 
    /// Load your modules or register your services here! 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel) 
    { 

     kernel.Bind<IProductsRepository>().To<FakeProductsRepository>(); 
     kernel.Bind<MovieRepository>().To<MovieRepository>(); 

    }   
} 

}

+0

通过这本书去掉的代码我得到的erorr“错误激活IProductRepository利用IProductRepository结合常数 提供商返回null 激活路径: 2)依赖IProductRepository的注入型ProductController的的构造函数的参数productRepository 1 )请求ProductController 建议: 1)确保提供程序正确处理创建请求。“ – MasterP 2012-02-15 14:22:58

+0

错误是由此造成的。 保护覆盖IController GetControllerInstance(RequestContext requestContext,Type controllerType) { return controllerType == null ? null(IController)ninjectKernel.Get(controllerType); } – MasterP 2012-02-15 14:23:08

+0

那么你是说问题解决了?如果不是,你能解释更多吗? – 2012-02-15 20:43:54

2

NinjectDependencyResolver必须从IDependencyResolver继承所以你的代码应该是这样的:

public class NinjectDependencyResolver : IDependencyResolver 
相关问题