2009-05-20 53 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(); 
}