2009-08-13 104 views
4

我目前使用ASP.NET(標準,而不是 MVC),我使用Ninject作爲我的IOC容器。Ninject,ASP.NET和自定義控件

我已經在使用它來向我的頁面注入依賴關係,但是,我想知道是否有方法將依賴注入到我的自定義控件中?

如果沒有,我會進行擴展Ninject :)

回答

6

好了,所以我結束了延長Ninject並增加了兩個班Ninject.Framework.Web DLL。

繼承人的人誰是有興趣加入它自己的補丁:

Index: src/Framework/Web/Ninject.Framework.Web.csproj 
=================================================================== 
--- src/Framework/Web/Ninject.Framework.Web.csproj (revision 158) 
+++ src/Framework/Web/Ninject.Framework.Web.csproj (working copy) 
@@ -2,7 +2,7 @@ 
    <PropertyGroup> 
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 
- <ProductVersion>9.0.21022</ProductVersion> 
+ <ProductVersion>9.0.30729</ProductVersion> 
    <SchemaVersion>2.0</SchemaVersion> 
    <ProjectGuid>{C46075DB-A0FB-466B-BA76-C093227FA9C7}</ProjectGuid> 
    <OutputType>Library</OutputType> 
@@ -42,17 +42,24 @@ 
    <Reference Include="System.Core"> 
     <RequiredTargetFramework>3.5</RequiredTargetFramework> 
    </Reference> 
+ <Reference Include="System.Data" /> 
+ <Reference Include="System.Drawing" /> 
    <Reference Include="System.Web" /> 
    <Reference Include="System.Web.Services" /> 
+ <Reference Include="System.Xml" /> 
    </ItemGroup> 
    <ItemGroup> 
    <Compile Include="..\..\GlobalAssemblyInfo.cs"> 
     <Link>Properties\GlobalAssemblyInfo.cs</Link> 
    </Compile> 
+ <Compile Include="WebControlBase.cs" /> 
    <Compile Include="NinjectHttpApplication.cs" /> 
    <Compile Include="HttpHandlerBase.cs"> 
    </Compile> 
    <Compile Include="NinjectHttpModule.cs" /> 
+ <Compile Include="UserControlBase.cs"> 
+  <SubType>ASPXCodeBehind</SubType> 
+ </Compile> 
    <Compile Include="WebServiceBase.cs"> 
     <SubType>Component</SubType> 
    </Compile> 
Index: src/Framework/Web/UserControlBase.cs 
=================================================================== 
--- src/Framework/Web/UserControlBase.cs (revision 0) 
+++ src/Framework/Web/UserControlBase.cs (revision 0) 
@@ -0,0 +1,65 @@ 
+#region License 
+// 
+// Author: Nate Kohari <[email protected]>, Nik Radford <[email protected]> 
+// Copyright (c) 2007-2008, Enkari, Ltd. 
+// 
+// Licensed under the Apache License, Version 2.0 (the "License"); 
+// you may not use this file except in compliance with the License. 
+// You may obtain a copy of the License at 
+// 
+// http://www.apache.org/licenses/LICENSE-2.0 
+// 
+// Unless required by applicable law or agreed to in writing, software 
+// distributed under the License is distributed on an "AS IS" BASIS, 
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+// See the License for the specific language governing permissions and 
+// limitations under the License. 
+// 
+#endregion 
+#region Using Directives 
+using System; 
+using Ninject.Core.Logging; 
+using Ninject.Core; 
+using System.Web.UI; 
+#endregion 
+ 
+namespace Ninject.Framework.Web 
+{ 
+ /// <summary> 
+ /// A <see cref="UserControl"/> that supports injection 
+ /// </summary> 
+ public class UserControlBase : UserControl 
+ { 
+  /*----------------------------------------------------------------------------------------*/ 
+  private ILogger _logger; 
+  /*----------------------------------------------------------------------------------------*/ 
+  /// <summary> 
+  /// Gets or sets the logger associated with the object. 
+  /// </summary> 
+  [Inject] 
+  public ILogger Logger 
+  { 
+   get { return _logger; } 
+   set { _logger = value; } 
+  } 
+  /*----------------------------------------------------------------------------------------*/ 
+  /// <summary> 
+  /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event to initialize the page. 
+  /// </summary> 
+  /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param> 
+  protected override void OnInit(EventArgs e) 
+  { 
+   base.OnInit(e); 
+   RequestActivation(); 
+  } 
+  /*----------------------------------------------------------------------------------------*/ 
+  /// <summary> 
+  /// Asks the kernel to inject this instance. 
+  /// </summary> 
+  protected virtual void RequestActivation() 
+  { 
+   KernelContainer.Inject(this); 
+  } 
+  /*----------------------------------------------------------------------------------------*/ 
+ } 
+} 
Index: src/Framework/Web/WebControlBase.cs 
=================================================================== 
--- src/Framework/Web/WebControlBase.cs (revision 0) 
+++ src/Framework/Web/WebControlBase.cs (revision 0) 
@@ -0,0 +1,65 @@ 
+#region License 
+// 
+// Author: Nate Kohari <[email protected]>, Nik Radford <[email protected]> 
+// Copyright (c) 2007-2008, Enkari, Ltd. 
+// 
+// Licensed under the Apache License, Version 2.0 (the "License"); 
+// you may not use this file except in compliance with the License. 
+// You may obtain a copy of the License at 
+// 
+// http://www.apache.org/licenses/LICENSE-2.0 
+// 
+// Unless required by applicable law or agreed to in writing, software 
+// distributed under the License is distributed on an "AS IS" BASIS, 
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+// See the License for the specific language governing permissions and 
+// limitations under the License. 
+// 
+#endregion 
+#region Using Directives 
+using System; 
+using System.Web.UI.WebControls; 
+using Ninject.Core.Logging; 
+using Ninject.Core; 
+#endregion 
+ 
+namespace Ninject.Framework.Web 
+{ 
+ /// <summary> 
+ /// A <see cref="WebControl"/> that supports injection 
+ /// </summary> 
+ public class WebControlBase : WebControl 
+ { 
+  /*----------------------------------------------------------------------------------------*/ 
+  ILogger _logger; 
+  /*----------------------------------------------------------------------------------------*/ 
+  /// <summary> 
+  /// Gets or sets the logger associated with the object. 
+  /// </summary> 
+  [Inject] 
+  public ILogger Logger 
+  { 
+   get { return _logger; } 
+   set { _logger = value; } 
+  } 
+  /*----------------------------------------------------------------------------------------*/ 
+  /// <summary> 
+  /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event to initialize the page. 
+  /// </summary> 
+  /// <param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data.</param> 
+  protected override void OnInit(EventArgs e) 
+  { 
+   base.OnInit(e); 
+   RequestActivation(); 
+  } 
+  /*----------------------------------------------------------------------------------------*/ 
+  /// <summary> 
+  /// Asks the kernel to inject this instance 
+  /// </summary> 
+  protected virtual void RequestActivation() 
+  { 
+   KernelContainer.Inject(this); 
+  } 
+  /*----------------------------------------------------------------------------------------*/ 
+ } 
+} 
+0

你能請至少發佈原始的XML而不是格式化的東西,你來自IE瀏覽器? – 2009-08-16 15:36:34

+0

這是一個Ninject的SVN補丁,它增加了在ASP.NET中使用Ninject和用戶控件和自定義控件的能力。 – Sekhat 2009-08-18 08:14:35

+0

它的確行得通!謝謝! – Seiti 2011-03-29 04:44:31

0

我通過所有控件的頁面上循環,並注射那些誰實現一些接口,解決了這個。

接口爲空:

public interface INinjectControl { } 

我添加了一個循環,PageBase和MasterPageBase注入其實現此接口的所有控件:

protected virtual void RequestActivation() 
{ 
    KernelContainer.Inject(this); 
    InjectControls(this.Controls); 
} 

private void InjectControls(ControlCollection controls) 
{ 
    foreach (Control control in controls) 
    { 
     if (control is INinjectControl) 
      KernelContainer.Inject(control); 
     this.InjectControls(control.Controls); 
    } 
} 

有一些缺點:

  • 這隻適用於當OnInit觸發時已經在頁面上的控件。如果稍後在過程中添加控件,則循環已經運行並且不會注入依賴關係 。
  • 由於您請求Controls集合,因此會在某些控件上調用CreateChildControls()。這可能會導致問題,因爲此方法現在在生命週期中更早地調用。
  • 它遍歷所有控件,所以效率不高。