统一 - 通过XML厂 [英] Unity - Factory via XML

查看:202
本文介绍了统一 - 通过XML厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的统一框架的IoC容器。

I am using the Unity framework as IoC container.

我的配置看起来有些像这样的:

My Config looks some like this:

<unity>
  <container>
    <register type="Namespace1.IFoo, FooAsm"
              mapTo="Namespace2.Bar, BarAsm">
    </register>
</conainer>

我想注册一个容器使用一个工厂方法。我该怎么办achive它使用的app.config?

I would like to register a container to use a factory method. How do i achive it using the app.config?

我要寻找的是这样的:

<unity>
  <container>
    <register type="Namespace1.IFoo, FooAsm"
              factory="Namespace2.Bar, BarAsm"
              method="create">
    </register>
  </conainer>
</unity>

任何suggestsions?

Any suggestsions?

推荐答案

<一个href="http://stackoverflow.com/questions/7143089/how-to-declare-the-unity-injectionfactory-in-xml-configuration">This螺纹提供了有关如何添加的工厂方法支持统一一个很好的回应。我其实你必须下载到位桶这个源(并更改引用4.0框架)。
如果你现在添加<一href="http://stackoverflow.com/questions/3486837/unity-2-0-registering-generic-types-via-xml">Generic团结支持你可能是这样的,一个pretty的真棒解决方案:

This thread offers a nice response about how to add support of Factory methods for Unity. I fact you have to download this bitbucket source (and change the references to 4.0 framework).
If you now add Generic support for Unity you get a pretty awesome solution that might look like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="Unity.FactoryConfig.FactoryConfigExtension, Unity.FactoryConfig"/>
    <alias alias="Factory" type="Namespace1.GenericFactory`1, asm1"/>
    <container>
      <register type="Namespace1.ITest, asm1">
        <factory type="Factory[[Namespace1.ITest, asm1]]" method="Create" />
      </register>
    </container>
  </unity>
</configuration>

支持通用的工厂使用xml配置统一的框架!
感谢所有的意见:)

Support of generic Factories for unity framework using xml configuration!
Thanks for all comments :)

这篇关于统一 - 通过XML厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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