2012-02-15 27 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