使用反射+注入

很多时候,我们希望程序可以有多种处理事情的方式,只需要修改一下配置文件,添加一个DLL就可以实现了。这就是需要用到了反射+注入了。

我下面举一个实际上要用到的例子:

一个数据中心的管理平台,数据进行处理时会产生很多的事件,而事件的通知方式有N种(现在有短信、邮件、工单)等,因为未知以后将会有其它什么通知方式,故只能通过先定义了接口的方式,用反射载入处理的DLL来实现……

(靠,发现我的文字表达能力真差,不断学习中……)

 

先插代码了:

app.config的配置文件:

<configuration>
  <configSections>
    <!-- connection configuration -->
    <section name="EventHandlers" type="Bingosoft.Time.ServiceRunner.EventHandlerConfig,Bingosoft.Time.ServiceRunner" />
    <section name="SMSConfig" type="Bingosoft.Workflow.EventHandler.SMSConfig,Bingosoft.Workflow.EventHandler" />
    <section name="ServiceRunner" type="Bingosoft.Common.Service.RunnerConfig,Bingosoft.Common.Service" />
  </configSections>
  <SMSConfig>
    <Connection>Data Source=server11,64235;User ID=GMCCPartner;Password=pass@word1;Initial Catalog=SMSCenter;</Connection>
    <ServiceNo>106</ServiceNo>
  </SMSConfig>
  <ServiceRunner>
    <Runner Type="TimerService" Runner="Bingosoft.Time.ServiceRunner.Runner,Bingosoft.Time.ServiceRunner"/>
    <!--<Runner Type="WorkflowRunner" Runner="Bingosoft.Workflow.ServiceRunner.Runner,Bingosoft.Workflow.ServiceRunner"/>-->
  </ServiceRunner>
  <EventHandlers>
    <Handler Type="WorkRemind"	Handler="Bingosoft.Workflow.EventHandler.WorkRemindEvent,Bingosoft.Workflow.EventHandler" />
    <Handler Type="SMS"	Handler="Bingosoft.Workflow.EventHandler.SMSEvent,Bingosoft.Workflow.EventHandler" />
    <Handler Type="SQL"	Handler="Bingosoft.Workflow.EventHandler.SQLEvent,Bingosoft.Workflow.EventHandler" />
  </EventHandlers>
</configuration>

 


读取配置文件:

public sealed class RunnerConfig:Dictionary<string,string>,IConfigurationSectionHandler
    {        
		public string GetRunner(string type)
		{       
			object result =  this[type.ToLower()];
			if (result == null)
				throw new ArgumentOutOfRangeException("type", string.Format("没有类型{0}的Runner注册!", type));
			return result.ToString();
		}
	
		public object Create(object parent, object configContext, XmlNode section)
		{
			XmlNodeList nodes = section.SelectNodes("//Runner");
			foreach (XmlNode node in nodes)
			{
				string name = node.Attributes.GetNamedItem("Type").Value.ToLower();
				string className = node.Attributes.GetNamedItem("Runner").Value;
				this.Add(name, className);
			}
			return this;
		}
    }

 



internal class RunnerFactory
    {
        private RunnerConfig _config;
        static RunnerFactory _instance = new RunnerFactory();

        internal static RunnerFactory Instance
        {
            get { return RunnerFactory._instance; }
            set { RunnerFactory._instance = value; }
        }

        public RunnerConfig Config
		{
			get
			{
				if(_config==null)
                    _config = (RunnerConfig)System.Configuration.ConfigurationManager.GetSection("ServiceRunner");
				return _config;
			}
			set { _config = value; }
		}


        private RunnerFactory()
		{
			
		}
		public static IServiceRunner CreateRunner(string type)
		{
			string handlerInfo	=_instance.Config[type].ToString();
			string typeName	= handlerInfo.Split(',')[0];
			string asmName	= handlerInfo.Split(',')[1];
			Assembly asm	= AppDomain.CurrentDomain.Load(asmName);
			System.Type hType	= asm.GetType(typeName);
            IServiceRunner result = (IServiceRunner)hType.InvokeMember(typeName, BindingFlags.CreateInstance, null, null, new object[] { });
			return result;
		}
    }


[本日志由 海风 于 2009-12-16 01:23 AM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 723
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 支持Gravatar头像.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭