使用Unity Framework,注入System.Windows.Forms.Form页面 [英] Using Unity Framework, inject into System.Windows.Forms.Form page

查看:286
本文介绍了使用Unity Framework,注入System.Windows.Forms.Form页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单:

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

,我想为上述两个属性注入具体的类。

and I want to inject concrete classes for the two attributes above.

我每个都有一个类型,并且正在使用app.config作为配置信息。但是,我不想为每个页面创建一个界面,只是为了注入,所以,我如何才能注入每个页面?

I have a type for each of them, and am using the app.config for the configuration information. But, I don't want to create an interface for each page, just to inject, so, how can I inject into each page?

基本上,我应该输入什么在下面的type元素的type属性中,还是我该怎么做?

Basically, what to I put in the type attribute in the following type element, or, how can I do this?

  <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框架。

I am using the Unity Framework for this, btw.

谢谢。

编辑:我得到了容器,然后尝试使用此容器进行注入:

I get the container and then I am trying to inject by using this:

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


推荐答案

基本上,您想使用属性注入来注入mdiAuthenticationForm的依赖项。您不能执行以下操作吗?

Basically you want to use property injection to inject mdiAuthenticationForm's dependencies. Can't you do something like the following?

在配置文件中添加类型映射:

Add your type mappings in the config file:

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

在Authentication and Authorization属性上放置依赖项属性。

Put Dependency attributes on Authentication and Authorization properties.

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

然后最后在您的代码中,执行以下操作以获取mdiAuthenticationForm的实例:

Then finally in your code, do the following to get an instance of mdiAuthenticationForm:

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

如果您不想在配置文件中添加mdiAuthentication,还可以执行以下操作:

If you don't want to add mdiAuthentication in the config file, you can also do the following:

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

这应该解决现有实例上的依赖关系并将它们连接起来。

This should resolve the dependencies on an existing instance and wire them up.

这篇关于使用Unity Framework,注入System.Windows.Forms.Form页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆