EnterPrise Lbrary学习笔记(附录) 依赖注入模块(Part 2 读取配置)

和 Enterprise Library 的其他应用程序块一样,Unity 的行为也可以通过配置来指定。

Unity 应用程序块可以从 XML 配置文件中读取配置信息。配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET 应用程序的 Web.config。当然,也可以从任何其他 XML 格式的文件或者其他数据源中加载配置信息。

在本文中,将和大家一起来学习 Unity 配置文件的格式、配置的读取、通过示例说明实例的获取。

0.准备工作

创建三个项目:

image

     Interface 接口,包含一个接口

using System;
using System.Collections.Generic;
using System.Text;

namespace Interface
{
    /// <summary> 
    /// 实际调用的接口 
    /// </summary> 
    public interface IHello
    {
        void SayHello();
    } 
}
 
Libaray 库包含两个继承于该接口的类
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Unity;
using Interface;

namespace Libaray
{

    /// <summary> 
    /// 实现A 
    /// </summary> 
    class ClassA : IHello
    {
        public void SayHello()
        {
            Console.Write(this.GetType().Name);
        }
    }
    /// <summary> 
    /// 实现B 
    /// </summary> 
    class ClassB : IHello
    {
        public void SayHello()
        {
            Console.Write(this.GetType().Name);
        }
    }
}

 

testUnity 控制台输出程序

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Unity;
using System.Data;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using System.Data.Common;
using Interface;

namespace UnityDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ///获取容器 
            IUnityContainer contaner = new UnityContainer(); 
            ///获取配置 
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); 
            ///使用指定的配置节对容器进行配置初始化 
            section.Containers["Hello"].Configure(contaner); 
            ///通过容器获取指定的类型的实时绑定的类型 
            IHello hello = contaner.Resolve<IHello>(); 
            ///调用方法 
            hello.SayHello(); 
            Console.ReadKey(); 
        }
    }
}
 

1. Unity 配置文件的格式

Unity 配置文件看起来像下面这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity>
    <typeAliases>
      <typeAlias alias="SayHello" type="Interface.IHello,Interface" />
      <typeAlias alias="SayHelloA" type="Libaray.ClassA,Libaray" />
      <typeAlias alias="SayHelloB" type="Libaray.ClassB,Libaray" />
    </typeAliases>
    <containers>
      <container name="Hello">
        <types>
          <type type="SayHello" mapTo="SayHelloA" >
          </type>
        </types>
      </container>
    </containers>
  </unity>
</configuration>
 

 

1.1 配置节

Unity  的配置节的名称为”unity",节处理程序的类型为 Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,它包含在程序集 Microsoft.Practices.Unity.Configuration 中。
1.2 unity 的子元素
unity包含一个typeAlias元素,typeAlias:为指定的类型添加别名,因为直接使用类型说明字符串来进行下面的配置将会很复杂而且容易出错。alias指定别名,type指定该别名对应的实际类型。

unity 的子元素包含了一个 containers 元素。

1.3 typeAliases节

typeAlias 节包含了一个注入类/接口 alias 属性指该类对应的易记名称,type 属性指该类对应程序集名称。

1.4containers节

container节包含一个types节,每个type子节包含两个属性,type属性用于在程序中绑定容器内的对象

section.Containers["Hello"].Configure(contaner);

mapTo用于指定当前对象对应typeAlias节里具体的类

2.运行结果

image

3.结束语

依赖注入模块减少了类库之间的耦合,有利于程序版本的更新。

转自 http://blog.csdn.net/zlAItlj/archive/2009/01/12/3760329.aspx 略有修改

posted @ 2009-08-27 16:58  何翔华  阅读(470)  评论(0编辑  收藏  举报