2011-03-25 61 views
4

我正在使用ASP.NET MVC3和Ninject。我已經設置了標準的代碼實現中,設置了綁定,並增加了這樣一個內核的DependencyResolver「AppStart_NinjectMVC3.cs」:ASP.NET MVC - 在控制器外部使用Ninject綁定

public static void RegisterServices(IKernel kernel) 
    { 
     kernel.Bind<IUserRepository>().To<UserRepository>(); 
     ... 
    } 

    public static void Start() { 
     IKernel kernel = new StandardKernel(); 
     RegisterServices(kernel); 
     DependencyResolver.SetResolver(new NinjectServiceLocator(kernel)); 
    } 

所有在我的控制器運作良好 - 依賴正在被解決罰款。

我想能夠使用Ninject和控制器外部以及MVC堆棧之外的這些綁定。例如,我有一堆常規的aspx頁面,我希望使用我的ninject內核,還有一些代碼掛在global.asax上。

我可以在這些其他地方重新使用我的Ninject內核,還是我還需要在Global.asax appstart中註冊一個內核?

回答

2

http://teamcity.codebetter.com發現目前的開發版本提供了側面支撐的普通aspx頁面,MVC和WCF側使用。你可能想看看這個。

請注意這是一個開發版本,它沒有得到很好的測試。不過,我認爲它應該非常穩定。但是,因爲它正在進行中,所以界面可以改變。在我編寫Ninject 2.4預覽博客之前,我也不會給予很多支持。

你需要

  • Ninject
  • Ninject.Web.Common
  • Ninject.Web
  • Ninject.Web.MVC3
+0

啊,感謝您的信息,我會看一下這個。這是否與MVC定義的內核「一起工作」,以及如何從網頁獲取它的句柄? – 2011-03-28 15:42:57

+0

您不需要訪問內核。它注入普通的網頁和控件。但你必須爲他們使用財產注入。 – 2011-03-28 16:06:45

+0

非常感謝 - 我想我會等待2.4,然後從那裏抓取它,我可以等待這個。 – 2011-03-29 11:35:06

0

我在我的ASP.NET MVC應用程序中使用了Ninject MVC Extension

下面是我已經完成了我認爲你想要完成的方式。

的Global.asax.cs:

public class MvcApplication : NinjectHttpApplication 
{ 
    /// <summary> 
    /// Overridden Ninject method that is called once the application has started and is initialized 
    /// </summary> 
    protected override void OnApplicationStarted() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     RegisterRoutes(RouteTable.Routes); 

     // Tell the MVC Framework to use our implementation of metadataprovider. 
     ModelMetadataProviders.Current = new XXX.myNamespace.MetadataProvider(); 

     // Tell the MVC Framework to use our CartModelBinder class 
     ModelBinders.Binders.Add(typeof(Cart), new CartModelBinder()); 
    } 


    /// <summary> 
    /// Establish a reference to our DIFactory object 
    /// <remarks> 
    /// This application currently uses Ninject for dependency injection. 
    /// </remarks> 
    /// </summary> 
    /// <returns></returns> 
    protected override IKernel CreateKernel() 
    { 
     return DIFactory.GetNinjectFactory(); 
    } 

    // snip... additional global.asax.cs methods 
} 

DIFactory.cs:

/// <summary> 
/// This class is used as a container for dependency injection throughout the entire application 
/// </summary> 
public class DIFactory 
{ 
    public static IKernel _kernel = null; 
    /// <summary> 
    /// Method used to create a single instance of Ninject's IKernel 
    /// </summary> 
    /// <returns>IKernel</returns> 
    public static IKernel GetNinjectFactory() 
    { 
     if (_kernel == null) 
     { 
      var modules = new INinjectModule[] 
      { 
       new ServiceModule() 
      }; 

      _kernel = new StandardKernel(modules); 
     } 
     return _kernel; 
    } 

    /// <summary> 
    /// Method used as a service locator for the IConfiguration interface 
    /// </summary> 
    /// <returns></returns> 
    public static IConfiguration CreateConfigurationType() 
    { 
     return _kernel.Get<IConfiguration>(); 
    } 

    // snip....additional public static methods for all other Interafaces necessary 
} 

ServiceModule.cs:

/// <summary> 
/// Configures how abstract service types are mapped to concrete implementations 
/// </summary> 
internal class ServiceModule : NinjectModule 
{ 
    public override void Load() 
    { 
     Bind<IConfiguration>().To<XXX.myNamespace.Configuration>(); 

     // snip... all other bindings to interfaces 
    } 
} 

使用其他類除了控制器:

UserInteraction.cs:

public class UserInteraction : IUserInteraction 
{ 
    private IConfiguration configuration; 

    public bool SubmitFeedback(Feedback feedback) 
    { 
     try 
     { 
      this.configuration = DIFactory.CreateConfigurationType(); 
      // snip additional logic... 

     } 
     catch(Exception ex) 
     { 
      // snip 
     } 
    } 
}