Castle IOC容器实践之EnterpriseLibrary Configuration Facility

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:
摘要:EnterpriseLibrary Configuration Facility就好像是在容器和数据类之间的桥,让我们可以轻松得去读取和操作配置文件。熟悉Enterprise Library的人都知道,在Enterprise Library中有一个Configuration Application Block,它可以使我们方便的从各种存储中读写配置信息,通过EnterpriseLibrary Configuration Facility我们就可以像使用普通的组件那样去注册一个数据类,它会用configurationkey来映射到Enterprise Library的配置文件中。
 
主要内容:
1 .概述
2 .使用Facility
3 .原理浅析
 
一.概述
EnterpriseLibrary Configuration Facility 就好像是在容器和数据类之间的桥,让我们可以轻松得去读取和操作配置文件。熟悉Enterprise Library的人都知道,在Enterprise Library中有一个Configuration Application Block,它可以使我们方便的从各种存储中读写配置信息,通过EnterpriseLibrary Configuration Facility我们就可以像使用普通的组件那样去注册一个数据类,它会用configurationkey来映射到Enterprise Library的配置文件中。先来看一下该Facility的相关信息:
Facility Information
Uses Proxy
No
Requires Configuration
Yes
Uses Attributes
No
Version
Beta 2
二.使用Facility
1 .配置文件,这里使用配置文件注册组件的方式,放在应用程序配置文件中,这里唯一需要注意的是configurationkey,这个特性不能写错:
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
None.gif
< configuration >
None.gif
None.gif    
< configSections >
None.gif
None.gif        
< section  name ="enterpriselibrary.configurationSettings"  type ="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"   />
None.gif
None.gif        
< section  name ="castle"  type ="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"   />
None.gif
None.gif    
</ configSections >
None.gif
None.gif    
< enterpriselibrary .configurationSettings xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
None.gif
None.gif        defaultSection
=""  applicationName ="Application"  xmlns ="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/configuration" >
None.gif
None.gif        
< configurationSections >
None.gif
None.gif            
< configurationSection  name ="EditorSettings"  encrypt ="false" >
None.gif
None.gif                
< storageProvider  xsi:type ="XmlFileStorageProviderData"  name ="XML File Storage Provider"  path ="../../EditorSettings.config"   />
None.gif
None.gif                
< dataTransformer  xsi:type ="XmlSerializerTransformerData"  name ="Xml Serializer Transformer" >
None.gif
None.gif                    
< includeTypes  />
None.gif
None.gif                
</ dataTransformer >
None.gif
None.gif            
</ configurationSection >
None.gif
None.gif        
</ configurationSections >
None.gif
None.gif        
< keyAlgorithmStorageProvider  xsi:nil ="true"   />
None.gif
None.gif    
</ enterpriselibrary.configurationSettings >
None.gif
None.gif    
< castle >
None.gif
None.gif        
< facilities >
None.gif
None.gif            
< facility  id ="configuration"  type ="Castle.Facilities.EnterpriseLibrary.Configuration.EnterpriseConfigurationFacility, Castle.Facilities.EnterpriseLibrary.Configuration"   />
None.gif
None.gif        
</ facilities >
None.gif
None.gif        
< components >
None.gif
None.gif            
< component  id ="editorfontdata"  type ="ConfigurationQuickStart.EditorFontData, Castle.Facilities.EnterpriseLibrary.Configuration.Tests"
None.gif
None.gif                configurationkey
="EditorSettings"   />
None.gif
None.gif            
< component  id ="editorservice"  type ="Castle.Facilities.EnterpriseLibrary.Configuration.Tests.EditorService, Castle.Facilities.EnterpriseLibrary.Configuration.Tests"   />
None.gif
None.gif        
</ components >
None.gif
None.gif    
</ castle >
None.gif
None.gif
</ configuration >
2 .编写数据类文件:
None.gif public   class  EditorFontData
ExpandedBlockStart.gif
{       
InBlock.gif    
private string  name;
InBlock.gif
InBlock.gif    
private float   size;
InBlock.gif
InBlock.gif    
private int     style;
InBlock.gif
InBlock.gif    
public EditorFontData()
ExpandedSubBlockStart.gif    
{          
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string Name 
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
getreturn name; }
InBlock.gif
ExpandedSubBlockStart.gif        
set{ name = value; }
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    
public float Size 
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
getreturn size; }
InBlock.gif
ExpandedSubBlockStart.gif        
set{ size = value; }
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    
public int Style 
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
getreturn style; }
InBlock.gif
ExpandedSubBlockStart.gif        
set{ style = value; }
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    
public override string ToString() 
ExpandedSubBlockStart.gif    
{
InBlock.gif        StringBuilder sb 
= new StringBuilder();
InBlock.gif
InBlock.gif        sb.AppendFormat(
"Name = {0}; Size = {1}; Style = {2}", name, size.ToString(), style.ToString());
InBlock.gif
InBlock.gif        
return sb.ToString();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
3 .采用XML方式的存储
None.gif <? xml version="1.0" encoding="utf-8" ?>
None.gif
None.gif
< EditorSettings >
None.gif
None.gif  
< xmlSerializerSection  type ="ConfigurationQuickStart.EditorFontData, Castle.Facilities.EnterpriseLibrary.Configuration.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
None.gif
None.gif    
< EditorFontData  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
None.gif
None.gif      
< Name > Microsoft Sans Serif </ Name >
None.gif
None.gif      
< Size > 9.25 </ Size >
None.gif
None.gif      
< Style > 0 </ Style >
None.gif
None.gif    
</ EditorFontData >
None.gif
None.gif  
</ xmlSerializerSection >
None.gif
None.gif
</ EditorSettings >
4 .使用数据类的组件
None.gif public   class  EditorService
ExpandedBlockStart.gif
{
InBlock.gif    
private readonly EditorFontData data;
InBlock.gif
InBlock.gif    
public EditorService(EditorFontData data)
ExpandedSubBlockStart.gif    
{
InBlock.gif        
this.data = data;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public EditorFontData Data
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
get return data; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
5 .在容器中使用数据类
None.gif [TestFixture]
None.gif
public   class  FacilityTestCase
ExpandedBlockStart.gif
{
InBlock.gif    [Test]
InBlock.gif    
public void LoadingConfig()
ExpandedSubBlockStart.gif    
{
InBlock.gif        IWindsorContainer container 
= new WindsorContainer( new XmlInterpreter(new AppDomainConfigSource()) );
InBlock.gif
InBlock.gif        EditorService service 
= (EditorService) container[ typeof(EditorService) ];
InBlock.gif
InBlock.gif        Assert.AreEqual(
"Microsoft Sans Serif", service.Data.Name);
InBlock.gif
InBlock.gif        Assert.AreEqual(
9.25,service.Data.Size);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}
可以看到,使用 EnterpriseLibrary Configuration Facility非常的简单。最后还要注意一点,使用这个 Facility需要安装 Enterprise Library,因为它依赖于:
None.gif Microsoft.Practices.EnterpriseLibrary.Common.dll
None.gif
None.gifMicrosoft.Practices.EnterpriseLibrary.Configuration.dll
三.原理分析
下面对这个Facility的原理做一下简单的分析。在初始化的时候,它注册了一个名为 EntLibConfigurationInspector 的分发器
None.gif public   class  EnterpriseConfigurationFacility : AbstractFacility
ExpandedBlockStart.gif
{
InBlock.gif    
protected override void Init()
ExpandedSubBlockStart.gif    
{
InBlock.gifKernel.ComponentModelBuilder.AddContributor( 
new EntLibConfigurationInspector() );
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
internal   class  EntLibConfigurationInspector : IContributeComponentModelConstruction
ExpandedBlockStart.gif
{
InBlock.gif    
public void ProcessModel(IKernel kernel, ComponentModel model)
ExpandedSubBlockStart.gif    
{
InBlock.gif        
if (model.Configuration == nullreturn;
InBlock.gif
InBlock.gif        String configKey 
= model.Configuration.Attributes["configurationkey"];
InBlock.gif
InBlock.gif        
if (configKey == nullreturn;
InBlock.gif
InBlock.gif        model.ExtendedProperties[
"configurationkey"= configKey;
InBlock.gif
InBlock.gif        model.CustomComponentActivator 
= typeof(EntLibComponentActivator);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
EntLibConfigurationInspector中为 ComponentModel注册一个 CustomComponentActivator类型的 Activator,这个 CustomComponentActivator的实现为 EntLibComponentActivator
None.gif internal   class  EntLibComponentActivator : AbstractComponentActivator
ExpandedBlockStart.gif
{
InBlock.gif    
public EntLibComponentActivator(ComponentModel model, IKernel kernel, 
InBlock.gif
InBlock.gif        ComponentInstanceDelegate onCreation, ComponentInstanceDelegate onDestruction) : 
base(model, kernel, onCreation, onDestruction)
ExpandedSubBlockStart.gif    
{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected override object InternalCreate()
ExpandedSubBlockStart.gif    
{
InBlock.gif        String configKey 
= (String) Model.ExtendedProperties["configurationkey"];
InBlock.gif
InBlock.gif        
return ConfigurationManager.GetConfiguration(configKey);
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected override void InternalDestroy(object instance)
ExpandedSubBlockStart.gif    
{
InBlock.gif        String configKey 
= (String) Model.ExtendedProperties["configurationkey"];
InBlock.gif
InBlock.gif        ConfigurationManager.WriteConfiguration(configKey, instance);
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

好了,关于 EnterpriseLibrary Configuration Facility就简单的介绍到这里。
更多Castle文章可以访问:《Castle 开发系列文章

















本文转自lihuijun51CTO博客,原文链接: http://blog.51cto.com/terrylee/67691  ,如需转载请自行联系原作者
相关文章
|
29天前
|
运维 安全 Devops
构建高效稳定的云基础设施:DevOps与容器化技术融合实践
在数字化转型的浪潮中,企业对于IT基础设施的要求越来越高,不仅需要快速响应市场变化,还要确保系统的稳定与安全。本文深入探讨了如何通过融合DevOps文化和容器化技术来构建一个高效、稳定且易于管理的云基础设施。通过实际案例分析,阐述了持续集成/持续部署(CI/CD)流程的优化、自动化测试、监控以及日志管理等关键环节的实施策略,旨在为运维专业人员提供一套切实可行的解决方案。
27 3
|
1月前
|
Java 测试技术 开发工具
ApplicationArguments读取应用程序参数并注入到IOC容器
ApplicationArguments读取应用程序参数并注入到IOC容器
ApplicationArguments读取应用程序参数并注入到IOC容器
|
1月前
|
运维 Kubernetes Devops
构建高效可靠的云基础设施:DevOps与容器化技术融合实践
【2月更文挑战第30天】 在当今快速迭代和竞争激烈的软件开发领域,传统的IT运维模式已难以满足业务发展的需要。本文将探讨如何通过整合DevOps文化和容器化技术,构建一个既高效又可靠的云基础设施。文章首先回顾了DevOps的核心理念及其对运维工作流的影响,接着深入讨论了容器化技术的优势和挑战,并提出了一套结合两者的实施方案。最后,通过案例分析展示了该方案在实际环境中的应用效果和潜在益处。
|
7天前
|
运维 Kubernetes Devops
构建高效自动化运维体系:DevOps与容器技术融合实践
【4月更文挑战第15天】 在当今快速发展的信息技术时代,传统的IT运维模式已难以满足业务敏捷性的需求。本文旨在探讨如何通过整合DevOps理念和容器技术来构建一个高效的自动化运维体系。文章将详细阐述DevOps的核心原则、容器技术的基础知识,以及两者结合的优势。此外,文中还将分享一系列实践经验,包括持续集成/持续部署(CI/CD)流程的搭建、微服务架构的应用,以及监控和日志管理策略的优化,以期帮助企业实现快速、可靠且安全的软件交付过程。
|
9天前
|
运维 Devops 持续交付
构建高效稳定的云基础设施:DevOps与容器化技术融合实践
【4月更文挑战第13天】 在当今快速迭代和持续部署的软件开发环境中,传统的IT运维模式已难以满足业务发展的需求。本文聚焦于如何通过融合DevOps理念与容器化技术,构建一个高效、稳定且易于管理的云基础设施。文章将探讨持续集成/持续交付(CI/CD)流程的优化、容器化技术的最佳实践、以及微服务架构下的应用管理,以期为企业提供一种改进运维效率、加速产品上市时间,同时保障系统稳定性的解决方案。
|
25天前
|
运维 Kubernetes Devops
构建高效稳定的云基础设施:DevOps与容器化技术融合实践
随着企业数字化转型的不断深入,传统的IT运维模式已经难以满足快速迭代和持续交付的需求。本文将探讨如何通过结合DevOps文化与容器化技术,构建一个既高效又稳定的云基础设施。文章首先概述了DevOps的核心理念及其在现代运维中的重要性,然后详细介绍了容器化技术,特别是Docker和Kubernetes在实现微服务架构中的应用。最后,文中通过案例分析展示了这一融合实践如何在真实环境中提升运维效率和系统稳定性。
21 7
|
28天前
|
运维 Kubernetes 监控
构建高效稳定的云基础设施:DevOps与容器化技术融合实践
在当今云计算时代,企业追求敏捷性、可扩展性以及成本效益的云基础设施。本文将探讨如何通过DevOps文化与容器化技术的融合,打造一个既高效又稳定的运维环境。文章不仅阐述了DevOps和容器化技术各自的优势,还提供了一个具体的实施案例,展示了这种结合如何优化资源利用、提高部署速度并降低运维复杂性。
|
1月前
|
运维 监控 Devops
构建高效自动化运维体系:基于容器技术的持续集成与持续部署实践
在数字化转型的浪潮中,企业的IT基础设施和软件交付模式正经历着深刻的变革。传统的运维方式已难以满足快速迭代、灵活扩展的现代业务需求。本文将探讨如何通过容器技术实现高效的自动化运维体系,重点分析持续集成(CI)与持续部署(CD)的实践方法及其对企业运维效率的影响。通过引入微服务架构、容器编排、DevOps文化等概念,我们旨在为读者提供一套全面的自动化运维解决方案,以支持业务的敏捷性和可扩展性。
|
1月前
|
存储 前端开发 Java
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
springboot中的第二个IOC容器BootstrapContext
|
1月前
|
Kubernetes Go 开发者
Go语言与Docker容器结合的实践应用与案例分析
【2月更文挑战第23天】本文通过分析实际案例,探讨了Go语言与Docker容器技术结合的实践应用。通过详细阐述Go语言在容器化环境中的开发优势,以及Docker容器技术在Go应用部署中的重要作用,本文旨在为读者提供Go语言与Docker容器结合的具体实现方法和实际应用场景。