`
- 浏览:
5940370 次
- 性别:
- 来自:
上海
-
在Enterprise Library 2.0中开发自己的Application Block(3)
同种类型集合
上面可以通过配置实现不同的叫声了,但是一次只能有一个叫声,我们希望根据配置动物名称和叫声的对应关系,当程序指定某种动物的时候,会返回相应的叫声。比如下面的配置:
<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<configSections>
<sectionname="AnimalConfiguration"type="AnimalApplicationBlock.AnimalSettings,AnimalApplicationBlock"/>
</configSections>
<AnimalConfigurationdefaultAnimal="Cow">
<animalProviders>
<addname="Dog"type="AnimalApplicationBlock.Animal,AnimalApplicationBlock"word="Arf!"/>
<addname="Pig"type="AnimalApplicationBlock.Animal,AnimalApplicationBlock"word="Oink!"/>
<addname="Cow"type="AnimalApplicationBlock.Animal,AnimalApplicationBlock"word="Moooo!"/>
</animalProviders>
</AnimalConfiguration>
</configuration>
对应Dog,我们要返回Arf!;对应Pig返回Oink!;对应Cow返回Moooo!。而且,如果没有指定,默认为Cow。
先看AnimalSettings的变化:
namespaceAnimalApplicationBlock
...{
publicclassAnimalSettings:SerializableConfigurationSection
...{
publicconststringSectionName="AnimalConfiguration";
privateconststringanimalProviderProperty="animalProviders";
privateconststringdefaultAnimalProperty="defaultAnimal";
[ConfigurationProperty(animalProviderProperty,IsRequired=true)]
publicNameTypeConfigurationElementCollection<AnimalData>AnimalProviders
...{
get...{return(NameTypeConfigurationElementCollection<AnimalData>)base[animalProviderProperty];}
}
[ConfigurationProperty(defaultAnimalProperty,IsRequired=true)]
publicstringDefaultAnimal
...{
get...{return(string)base[defaultAnimalProperty];}
}
}
}
AnimalProviders属性用来对应多个动物和叫声的对应集合。使用NameTypeConfigurationElementCollection<T>作为类型,AnimalData是继承NameTypeConfigurationElement的。另外,加一个属性用来定义默认动物的名字。
AnimalCustomFactory需要做一点改动,最后的返回是,使用settings.AnimalProviders.Get(name)。
namespaceAnimalApplicationBlock
...{
publicclassAnimalCustomFactory:AssemblerBasedCustomFactory<IAnimal,AnimalData>
...{
protectedoverrideAnimalDataGetConfiguration(stringname,IConfigurationSourceconfigurationSource)
...{
AnimalSettingssettings=(AnimalSettings)configurationSource.GetSection(AnimalSettings.SectionName);
returnsettings.AnimalProviders.Get(name);
}
}
}
为了实现默认动物的实现,AnimalFactory加一个CreateAnimal的重载方法。
namespaceAnimalApplicationBlock
...{
publicstaticclassAnimalFactory
...{
privatestaticAnimalInstanceFactoryfactory=newAnimalInstanceFactory(ConfigurationSourceFactory.Create());
publicstaticAnimalCreateAnimal(stringanimalName)...{returnfactory.Create(animalName);}
publicstaticAnimalCreateAnimal()...{returnfactory.CreateDefault();}
}
}
这个重载方法是为了使用这个ApplicationBlock的开发人员方便,另外提供一种默认实现的方式,以后可以通过配置默认实现来改变很多程序的相关逻辑。这样就带来了如何找到默认实现的问题,要解决这个问题,需要添加一个类DefaultNameRetriever,用来获取配置的默认动物名。
namespaceAnimalApplicationBlock
...{
publicclassDefaultNameRetriever:IConfigurationNameMapper
...{
publicstringMapName(stringname,IConfigurationSourceconfigSource)
...{
returnname!=
null?name:((AnimalSettings)configSource.GetSection(AnimalSettings.SectionName)).DefaultAnimal;
}
}
}
然后需要在Animal类加上相应的属性ConfigurationNameMapper(typeof(DefaultNameRetriever))。
namespaceAnimalApplicationBlock
...{
[CustomFactory(typeof(AnimalCustomFactory))]
[ConfigurationNameMapper(typeof(DefaultNameRetriever))]
publicclassAnimal
...{
privatestringwordToSay;
publicAnimal():this("Bark!")...{}
publicAnimal(stringwordToSay)
...{
this.wordToSay=wordToSay;
}
publicstringSpeak()...{returnwordToSay;}
}
}
测试的代码如下
[TestMethod]
publicvoidPigsSayOink()
...{
Animalpig=AnimalFactory.CreateAnimal("Pig");
Assert.AreEqual("Oink!",pig.Speak());
}
[TestMethod]
publicvoidDogsSayArf()
...{
Animaldog=AnimalFactory.CreateAnimal("Dog");
Assert.AreEqual("Arf!",dog.Speak());
}
[TestMethod]
publicvoidDefaultAnimalIsCow()
...{
Animalcow=AnimalFactory.CreateAnimal();
Assert.AreEqual("Moooo!",cow.Speak());
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Enterprise Library --Data Access Application Block整理(入门,示例)
Enterprise Library – Data Access Application Block 学习手册(最新版) - Part 1 Enterprise Library for .Net Framework 3.5 – EntLib v4.1 是 patterns & practices 小组为.NET Framework 3.5 开发一套企业库...
The following sections of this guidance describe the ways that you can use Enterprise Library and the individual application blocks in your applications. The sections are: What Is Enterprise Library...
Microsoft Enterprise Library is a collection of reusable application blocks designed to assist software developers with common enterprise development challenges. This release includes: Data Access ...
微软的企业库( Microsoft.Enterprise.Library)相信大家应该不会陌生吧,... shows how to develop and use a new application block, and explains how Enterprise Library is changing for .NET Framework 2.0.
This release of Enterprise Library includes: Caching Application Block, Cryptography Application Block, Data Access Application Block, Exception Handling Application Block, Logging Application Block, ...
Enterprise Library consists of reusable software components that are designed to assist developers with common enterprise development challenges. It includes a collection of functional application ...
Enterprise Library 4.0版本包含以下应用程序块: Caching Application Block . 开发人员可以使用这个程序块在应用程序中应用缓存. 支持可插拔的缓存提供者(Provider)。 Cryptography Application Block . ...
Enterprise Library for .Net Framework 3.5 – EntLib v4.1 是patterns & practices 小组为.NET Framework 3.5 开发一套企业库,目前最新版本为v4.1,共包括9个Application Block,包括数据访问(Data Access ...
微软的企业库( Microsoft.Enterprise.Library)相信大家应该不会陌生吧,... shows how to develop and use a new application block, and explains how Enterprise Library is changing for .NET Framework 2.0.
Enterprise Library for .Net Framework 3.5 – EntLib v4.1 是patterns & practices 小组为.NET Framework 3.5 开发一套企业库,目前最新版本为v4.1,共包括9个Application Block,包括数据访问(Data Access ...
Microsoft Enterprise Library is a collection of reusable application blocks designed to assist software developers with common enterprise development challenges. This release includes: Caching Block, ...
Enterprise Library4.1中文教程,共8个模块。安全管理(Security Application Block), 缓存管理(Caching Application Block)...都一一做了介绍
Enterprise Library ——Data Access Application block学习手册。Enterprise Library for.net framework 3.5。
Microsoft Enterprise Library is a collection of reusable application blocks designed to assist software developers with common enterprise development challenges. This release includes: Caching Block,...
Enterprise Library for .Net Framework 3.5 – EntLib v4.1 是patterns & practices 小组为.NET Framework 3.5 开发一套企业库,目前最新版本为v4.1,共包括9个Application Block,包括数据访问(Data Access ...
微软今天发布了Enterprise Library 3.0,又有新的东西可以研究了,在Enterprise Library 3.0中包含如下8个应用程序块: l Caching Application Block l Cryptography Application Block l Data Access ...
Contents <br>Overview of Data Access Application Block Why DBHelper? Advantages of DBHelper Pre-requisites for DBHelper Downloads How to use DBHelper? Drawbacks of DBHelper...