First we try, then we trust

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

[]Object Builder Application Block

文/黄忠成 ;2006/9/21

原文链接:http://blog.csdn.net/Code6421/archive/2006/09/25/1282167.aspx

整理:吕震宇

八、ObjectBuilder实务

8-1、ObjectBuilderWithConfiguration

截至目前为止,本文一直以程序方式来配置ObjectBuidler建立对象所需的各种对象,但在实务上,这些动作应该是交由配置文件来负责,这样才能在不重新编译应用程序的情况下,改变其行为或增加其功能。很幸运的,Microsoft于ObjectBuilder的范例中提供了一个途径来达到此目的,该范例定义了一个.xsd文件,其内定义了ConstructorInjection、SetterInjection、Singleton、TypeMapping所需要的schema,当然!这个xsd中也定义了Strategy的schema,允许设计者通过配置文件来添加对象建立时所需使用的Strategys。

  • ConsturctorInjectionWithConfiguration

我将Microsoft所提供可配置ObjectBuidler的范例中关于处理配置文件时的.xsd及相关文件提取出来,并添加MethodInjection时所需要的schema及程序代码,放置于本文的范例程序档中,当读者们需要使用配置文件这个功能时,可以将Config目录中的ObjectBuilderXmlConfig.xsd、ObjectBuilderXmlConfig.xsx、ObjectBuilderXmlConfig.cs、ObjectBuilderXmlConfig.Generate.cs等文件复制到项目目录中,并将ObjectBuilderXmlConfig.xsd、ObjectBuilderXmlConfig.Generate.cs加入到项目中,完成后再将ObjectBuilderXmlConfig.xsd设置成EmbeddedResource,如图10所示。

图10

接着将修改ObjectBuilderXmlConfig.cs中关于由Resource中取得.xsd内容的程序代码,修正namespace为项目的默认namespace即可。

程序44

private static ObjectBuilderXmlConfig ParseXmlConfiguration(string config)
{
    XmlSerializer ser = new XmlSerializer(typeof(ObjectBuilderXmlConfig));
    StringReader stringReader = new StringReader(config);
    XmlSchema schema = XmlSchema.Read(Assembly.GetExecutingAssembly().GetManifestResourceStream(
        "OB_CSConfigurationTest.ObjectBuilderXmlConfig.xsd"), null);
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.Schemas.Add(schema);
    XmlReader reader = XmlReader.Create(stringReader, settings);
    ObjectBuilderXmlConfig configData = (ObjectBuilderXmlConfig)ser.Deserialize(reader);
    return configData;
}

要使用配置文件来完成ConstructorInjection,我们必须在项目中新增一个xml文件,内容如下所示。

<?xml version="1.0" encoding="utf-8"?>
<object-builder-config xmlns="pag-object-builder" >
     <build-rules >
         <build-rule type="OB_ConfigurationTest.InputAccept,OB_CSConfigurationTest" mode="Instance" >
             <constructor-params >
                 <ref-param type="OB_ConfigurationTest.PromptDataProcessor,OB_CSConfigurationTest" />
             </constructor-params>
         </build-rule>
     </build-rules>
</object-builder-config>

在ObjectBuilderXmlConfig.xsd定义中,build-rules代表着此BuilderContext中所有的对象建立规则,每个build-rule对应着一个“类型/id”,类型格式为<type,Assembly>,id部份则可通过添加name这个attribute来设置,未指定时就以null为默认值,如下。

<?xml version="1.0" encoding="utf-8"?>
<object-builder-config xmlns="pag-object-builder" >
     <build-rules >
         <build-rule type="OB_ConfigurationTest.InputAccept,OB_CSConfigurationTest" name="id1" mode="Instance" >
             ......

每个build-rule可以拥有一个constructor-params区段,设计者可以在这个区段中添加value-param或是ref-param定义,前者是直接设置该参数的值,后者是通过reference方式来设值,本例中是将InputAccept构造函数的第一个参数值指定为PromptDataProcessor。程序45是使用这个配置文件的程序列表。

程序45

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace OB_ConfigurationTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream("XmlFile1.xml", FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    Builder builder = new Builder(ObjectBuilderXmlConfig.FromXml(sr.ReadToEnd()));
                    InputAccept accept = builder.BuildUp<InputAccept>(new Locator(), null, null);
                    accept.Execute();
                    Console.Read();
                }
            }
        }
    }

    public class InputAccept
    {
        private IDataProcessor _dataProcessor;
        public void Execute()
        {
            Console.Write("Please Input some words:");
            string input = Console.ReadLine();
            input = _dataProcessor.ProcessData(input);
            Console.WriteLine(input);
        }
        public InputAccept([Dependency(Name = "dataProcessor")]IDataProcessor dataProcessor)
        {
            _dataProcessor = dataProcessor;
        }
    }

    public interface IDataProcessor
    {
        string ProcessData(string input);
    }

    public class DummyDataProcessor : IDataProcessor
    {
        #region IDataProcessor Members
        public string ProcessData(string input)
        {
            return input;
        }
        #endregion
    }

    public class PromptDataProcessor : IDataProcessor
    {
        #region IDataProcessor Members
        public string ProcessData(string input)
        {
            return "your input is:" + input;
        }
        #endregion
    }
}
  • SetterInjectionWithConfiguration

同样的,我们也可以通过配置文件来完成SetterInjection,此范例所使用的配置文件如下。

<?xml version="1.0" encoding="utf-8"?>
<object-builder-config xmlns="pag-object-builder" >
    <build-rules >
        <build-rule type="OB_CSPropertyInjectionTest.InputAccept,OB_CSPropertyInjectionTest" mode="Instance" >
            <property name="DataProcessor" >
                <ref-param type="OB_CSPropertyInjectionTest.PromptDataProcessor,OB_CSPropertyInjectionTest" />
            </property>
        </build-rule>
    </build-rules>
</object-builder-config>

设计者必须将要设置的属性定义放置于build-rule区段中的property,property有一个nameattribute,代表着欲设置属性的名称,如要设置一个以上的属性,只需添加多个property区段即可,程序46是使用此配置文件的程序代码。

程序46

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace OB_CSPropertyInjectionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream("XmlFile1.xml", FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    Builder builder = new Builder(ObjectBuilderXmlConfig.FromXml(sr.ReadToEnd()));
                    InputAccept accept = builder.BuildUp<InputAccept>(new Locator(), null, null);
                    accept.Execute();
                    Console.Read();
                }
            }
        }
    }

    public class InputAccept
    {
        private IDataProcessor _dataProcessor;

        [Dependency(Name = "DataProcessor")]
        public IDataProcessor DataProcessor
        {
            get
            {
                return _dataProcessor;
            }
            set
            {
                _dataProcessor = value;
            }
        }

        public void Execute()
        {
            Console.Write("Please Input some words:");
            string input = Console.ReadLine();
            input = _dataProcessor.ProcessData(input);
            Console.WriteLine(input);
        }
    }

    public interface IDataProcessor
    {
        string ProcessData(string input);
    }

    public class DummyDataProcessor : IDataProcessor
    {
        #region IDataProcessor Members
        public string ProcessData(string input)
        {
            return input;
        }
        #endregion
    }

    public class PromptDataProcessor : IDataProcessor
    {
        #region IDataProcessor Members
        public string ProcessData(string input)
        {
            return "your input is:" + input;
        }
        #endregion
    }
}
  • MethodInjectionWithConfiguration

Microsoft所提供的配置文件机制并未定义MethodInjection的功能,我将其稍微修改来支持此功能,下面是MethodInjection时所用的配置文件。

<?xml version="1.0" encoding="utf-8"?>
<object-builder-config xmlns="pag-object-builder" >
    <build-rules >
        <build-rule type="OB_CSPropertyInjectionTest.InputAccept,OB_CSPropertyInjectionTest" mode="Instance" >
            <property name="DataProcessor" >
                <ref-param type="OB_CSPropertyInjectionTest.PromptDataProcessor,OB_CSPropertyInjectionTest" />
            </property>
        </build-rule>
    </build-rules>
</object-builder-config>

用法与property大致相同,程序47是程序代码列表。

程序47

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Practices.ObjectBuilder;
namespace OB_CSMethodInjectionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream("XmlFile1.xml", FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    Builder builder = new Builder(ObjectBuilderXmlConfig.FromXml(sr.ReadToEnd()));
                    InputAccept accept = builder.BuildUp<InputAccept>(new Locator(), null, null);
                    accept.Execute();
                    Console.Read();
                }
            }
        }
    }

    public class InputAccept
    {
        private IDataProcessor _dataProcessor;

        public void SetDataProcessor([Dependency(Name = "dataProcessor")]IDataProcessor dataProcessor)
        {
            _dataProcessor = dataProcessor;
        }

        public void Execute()
        {
            Console.Write("Please Input some words:");
            string input = Console.ReadLine();
            input = _dataProcessor.ProcessData(input);
            Console.WriteLine(input);
        }
    }

    public interface IDataProcessor
    {
        string ProcessData(string input);
    }

    public class DummyDataProcessor : IDataProcessor
    {
        #region IDataProcessor Members
        public string ProcessData(string input)
        {
            return input;
        }
        #endregion
    }

    public class PromptDataProcessor : IDataProcessor
    {
        #region IDataProcessor Members
        public string ProcessData(string input)
        {
            return "your input is:" + input;
        }
        #endregion
    }
}
  • SingletonWithConfiguration

通过配置文件,也可以定义那个“类型/id”的对象是使用Singleton模式,如下所示。

<build-rule type="OB_CSMethodInjectionTest.PromptDataProcessor,OB_CSMethodInjectionTest" mode="Singleton" />
  • TypeMappingWithConfiguration

我们也可以在配置文件中定义TypeMapping,如下所示。

<build-rule type="OB_CSMethodInjectionTest.IDataProcessor,OB_CSMethodInjectionTest" mode="Instance" >
    <mapped-type type="OB_CSMethodInjectionTest.PromptDataProcessor,OB_CSMethodInjectionTest" />
</build-rule>
  • CustomizeStrategys

通过配置文件,也可以定义建立对象时所需要的Strategys,如下所示。

<object-builder-config xmlns="pag-object-builder" >
    <strategies include-default="false" >
        <strategy type="Microsoft.Practices.ObjectBuilder.TypeMappingStrategy,Microsoft.Practices.ObjectBuilder" />
        <strategy type="Microsoft.Practices.ObjectBuilder.CreationStrategy,Microsoft.Practices.ObjectBuilder" />
        <strategy type="Microsoft.Practices.ObjectBuilder.MethodExecutionStrategy,Microsoft.Practices.ObjectBuilder" />
        .............
    </strategies>
</object-builder-config>

include-default属性决定是否包含Builder对象所内建的Strategys。

九、后记

ObjectBuilder是一个相当不错的DependencyInjection实现对象,虽然相对于Spring、Avalon,ObjectBuilder并不是相当的完整,例如没有完整的配置文件驱动特色,但它所具备的高度延展性,可以让我们轻易的达到这个需求,且日后相信ObjectBuilder也会内建这些功能的。

posted on 2007-02-06 10:47  吕震宇  阅读(3257)  评论(2编辑  收藏  举报