2009-05-20 28 views
1

我有一個表格:使用Unity框架,注入System.Windows.Forms.Form中頁

public partial class mdiAuthenticationForm : Form 
    { 
     public Services.Authentication.IAuthentication Authenticator { get; set; 
     public Services.Authentication.IAuthorization Authorizor { get; set; } 

,我想對上述兩種屬性注入的具體類。

我有他們每個人的類型,並使用app.config的配置信息。但是,我不想爲每個頁面創建一個接口,只是爲了注入,所以我怎樣才能注入每個頁面?

基本上,我在以下類型元素中的type屬性中放置了什麼,或者,我該如何做?

<type type="" mapTo="mdiAuthenticationForm,project"> 
    <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration"> 
     <property name="Authenticator" propertyType="Services.Authentication.IAuthentication,project"> 
     <dependency name="mssqlauth" /> 
     </property> 
    <property name="Authorizor" propertyType="Services.Authentication.IAuthorization,project"> 
     <dependency name="mssqlautz" /> 
    </property> 
    </typeConfig> 
    </type> 

我正在使用Unity框架,順便說一句。

謝謝。

編輯:我得到的容器,然後我想用這注:

Container.Configure<InjectedMembers>().ConfigureInjectionFor<mdiAuthenticationForm>(); 

回答

0

基本上你想使用屬性注入來注入mdiAuthenticationForm的依賴關係。你不能做下面的事嗎?

添加您的類型映射在配置文件中:

<container> 
    <types> 
    <type type="IAuthentication,PutAssemblyNameHere" mapTo="Authentication,PutAssemblyNameHere"/> 
    <type type="IAuthorization,PutAssemblyNameHere" mapTo="Authorization,PutAssemblyNameHere"/> 
    <type type="mdiAuthenticationForm,PutAssemblyNameHere"/> 
    </types> 
</container> 

放在身份驗證和授權的屬性依賴的屬性。

[Dependency] 
public Services.Authentication.IAuthentication Authenticator { get; set;} 
[Dependency] 
public Services.Authentication.IAuthorization Authorizor { get; set; } 

後來終於在你的代碼,請執行以下操作來獲得mdiAuthenticationForm的一個實例:

mdiAuthenticationForm form = container.Resolve<mdiAuthenticationForm>(); 

如果你不希望添加mdiAuthentication在配置文件中,你也可以做以下:

mdiAuthenticationForm form = new mdiAuthenticationForm(); 
container.BuildUp<mdiAuthenticationForm>(form); 

這應該解決對現有實例的依賴關係並將它們連接起來。

+0

我想在我的代碼之外沒有任何東西,除了我必須爲接線。我希望儘可能不顯眼,不幸的是這比我想象的要難。 – 2009-05-21 19:29:59

0

我得到它的工作,不漂亮,但它的作品。最後一個問題是我必須在我的界面中包含Form.ShowDialog。

首先是我的代碼的主要部分,從Program.cs創建並調用窗體,然後第二個是我的界面。我希望這是模式,這就是我使用ShowDialog的原因。我還將這兩個屬性移到了新的控制器中,但我仍然有一個屬性需要設置,以確保它正常工作。

container.Configure<InjectedMembers>().ConfigureInjectionFor<IAuthenticationForm>(); 
    containers.Configure(container); 
    IAuthenticationForm f = container.Resolve<IAuthenticationForm>(); 
    f.ShowDialog(); 


public interface IAuthenticationForm 
{ 
    Optimal4.Services.Authentication.IAuthorization Authorizor { get; set; } 
    void checkAuthentication(); 
    System.Windows.Forms.DialogResult ShowDialog(); 
}