温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WCF利用企业库Unity框架的IOC层次解耦

发布时间:2020-06-12 00:39:38 来源:网络 阅读:1222 作者:破狼 栏目:编程语言

由于工作几个月来框架开发已经好久没写博客了,今天想抽点时间出来冒泡。在领域驱动开发中SOA已经成为我们开发的主流技术,在面对当前众多的UI框架选择(asp.net mvc,silverlight,wpf,winform,wp7等一些列甚至跨语言),我们能够重用和抽象的业务逻辑是不会变化的,为了屏蔽这些UI的复杂性和差异性,我们可能会采用诸如wcf soap的服务发布。再说在领域驱动开发中我们肯定会把各个逻辑分层次隔离解除耦合,这就出现了N层架构,在于我们的每一层次之间的耦合度当前流程的解决方案IOC框架,以及业务解耦AOP。这里要解决的是SOA框架WCF和IOC框架的结合。

    WCF框架是一个极易扩展的框架,提供了非常多的扩展点(InstanceProvider,MessageInspector,CallContextInitializer,MessageFilter,MessageFormatter,ParameterInspector等等)。在这里IOC是作为一个容器组装创建的框架,我们需要的是对服务对象的创建,所以我们选择的当然是InstanceProvider扩展点。

     多说一句废话,正如前篇利用Attribute简化Unity框架IOC注入和 AOP之PostSharp7-解决IOC 不能直接new问题,简化IOC开发和IOC对象LazyLoad中所说,我是一个固执的人,个人希望注入支持自定义配置文件,不喜欢把配置信息全部写在一个web.config/app.config中,也不喜欢el的写在同一个外部配置文件中,倾向于每个模块在一个不同的配置文件,并在模块中在区分container容器,所以特别写了每个单独配置文件的延时加载,缓存。

      下面就是对InstanceProvider的扩展:

  1. View Code   
  2.  
  3. public class ELUnityInstanceProvider : IInstanceProvider   
  4.    {   
  5.        private Type contractType;   
  6.        private string container;   
  7.        private string configFile;   
  8.        private string name;   
  9.        private static object lockObj = new object();   
  10.        private static Dictionary<string, UnityConfigurationSection> sectionDict = new Dictionary<string, UnityConfigurationSection>();   
  11.  
  12.        public ELUnityInstanceProvider(Type contractType, string container, string configFile, string name)   
  13.        {   
  14.            this.name = name;   
  15.            this.configFile = configFile;   
  16.            this.contractType = contractType;   
  17.            this.container = container;   
  18.        }   
  19.  
  20.        #region IInstanceProvider 成员   
  21.  
  22.        public object GetInstance(System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message)   
  23.        {   
  24.            Microsoft.Practices.Unity.Configuration.UnityConfigurationSection unitySection = GetUnityConfigurationSection();   
  25.  
  26.            if (unitySection != null)   
  27.            {   
  28.                var container = new Microsoft.Practices.Unity.UnityContainer().LoadConfiguration(unitySection, string.IsNullOrEmpty(this.container) ? unitySection.Containers.Default.Name : this.container);   
  29.                var obj = string.IsNullOrEmpty(this.name) ? container.Resolve(this.contractType) : container.Resolve(this.contractType, this.name);   
  30.  
  31.                var piabAtttr = obj.GetType().GetCustomAttributes(typeof(ELPolicyinjectionAttribute), falseas ELPolicyinjectionAttribute[];   
  32.                if (piabAtttr.Length > 0)   
  33.                {   
  34.                    obj = Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Wrap(this.contractType, obj);   
  35.                }   
  36.                return obj;   
  37.            }   
  38.            return null;   
  39.        }   
  40.        private Microsoft.Practices.Unity.Configuration.UnityConfigurationSection GetUnityConfigurationSection()   
  41.        {   
  42.            if (!string.IsNullOrEmpty(this.configFile))   
  43.            {   
  44.                if (!sectionDict.ContainsKey(this.configFile))   
  45.                {   
  46.                    lock (lockObj)   
  47.                    {   
  48.                        if (!sectionDict.ContainsKey(this.configFile))   
  49.                        {   
  50.                            Microsoft.Practices.Unity.Configuration.UnityConfigurationSection unitySection = null;   
  51.                            var fileMap = new System.Configuration.ExeConfigurationFileMap { ExeConfigFilename = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, this.configFile) };   
  52.                            System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);   
  53.                            unitySection = configuration == null ? null : configuration.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName) as Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;   
  54.                            if (unitySection == null)   
  55.                                return null;   
  56.                            sectionDict.Add(this.configFile, unitySection);   
  57.                        }   
  58.                    }   
  59.                }   
  60.                return sectionDict[this.configFile];   
  61.            }   
  62.  
  63.            return System.Configuration.ConfigurationManager.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName) as Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;   
  64.  
  65.        }   
  66.  
  67.        public object GetInstance(System.ServiceModel.InstanceContext instanceContext)   
  68.        {   
  69.            return this.GetInstance(instanceContext, null);   
  70.        }   
  71.  
  72.        public void ReleaseInstance(System.ServiceModel.InstanceContext instanceContext, object instance)   
  73.        {   
  74.            IDisposable disposable = instance as IDisposable;   
  75.            if (disposable != null)   
  76.            {   
  77.                disposable.Dispose();   
  78.            }   
  79.            instance = null;   
  80.        }   
  81.  
  82.        #endregion   
  83.    }  
  84. 复制代码 

  下面我们需要已Attribute方式贴在Contract上:

  1. View Code   
  2.  
  3. public class ELUnityBehaviorAttribute : Attribute, IContractBehavior   
  4.    {   
  5.        public string Container   
  6.        {   
  7.            get;   
  8.            set;   
  9.        }   
  10.  
  11.        public string ConfigFile   
  12.        {   
  13.            get;   
  14.            set;   
  15.        }   
  16.  
  17.        public string Name   
  18.        {   
  19.            get;   
  20.            set;   
  21.        }   
  22.  
  23.        #region IContractBehavior 成员   
  24.  
  25.        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)   
  26.        {   
  27.  
  28.        }   
  29.  
  30.        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)   
  31.        {   
  32.  
  33.        }   
  34.  
  35.        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.DispatchRuntime dispatchRuntime)   
  36.        {   
  37.            dispatchRuntime.InstanceProvider = new ELUnityInstanceProvider(contractDescription.ContractType, this.Container, this.ConfigFile, this.Name);   
  38.        }   
  39.  
  40.        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)   
  41.        {   
  42.        }   
  43.  
  44.        #endregion   
  45.    }  
  46. 复制代码  
  47.    

     对于wcf同时我们也该支持配置文件扩展:

  1. View Code   
  2.  
  3. public class ELUnityBehaviorElement : BehaviorExtensionElement   
  4.    {   
  5.        [ConfigurationProperty("Container", IsRequired = false, DefaultValue = "")]   
  6.        public string Container   
  7.        {   
  8.            get;   
  9.            set;   
  10.        }   
  11.  
  12.        [ConfigurationProperty("ConfigFile", IsRequired = false, DefaultValue = "")]   
  13.        public string ConfigFile   
  14.        {   
  15.            get;   
  16.            set;   
  17.        }   
  18.  
  19.        [ConfigurationProperty("Name", IsRequired = false, DefaultValue = "")]   
  20.        public string Name   
  21.        {   
  22.            get;   
  23.            set;   
  24.        }   
  25.  
  26.        protected override object CreateBehavior()   
  27.        {   
  28.            return new ELUnityBehavior(this.Container, this.ConfigFile, this.Name);   
  29.        }   
  30.  
  31.        public override Type BehaviorType   
  32.        {   
  33.            get { return typeof(ELUnityBehavior); }   
  34.        }   
  35.    }  
  36.  
  37. public class ELUnityBehavior : IEndpointBehavior   
  38.    {   
  39.        public string Container   
  40.        {   
  41.            get;   
  42.            set;   
  43.        }   
  44.  
  45.        public string ConfigFile   
  46.        {   
  47.            get;   
  48.            set;   
  49.        }   
  50.  
  51.        public string Name   
  52.        {   
  53.            get;   
  54.            set;   
  55.        }   
  56.  
  57.        public ELUnityBehavior(string container, string configFile, string name)   
  58.        {   
  59.            this.Name = name;   
  60.            this.ConfigFile = configFile;   
  61.            this.Container = container;   
  62.        }   
  63.        #region IEndpointBehavior 成员   
  64.  
  65.        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)   
  66.        {   
  67.  
  68.        }   
  69.  
  70.        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)   
  71.        {   
  72.  
  73.        }   
  74.  
  75.        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)   
  76.        {   
  77.            endpointDispatcher.DispatchRuntime.InstanceProvider = new ELUnityInstanceProvider(endpoint.Contract.ContractType, this.Container, this.ConfigFile, this.Name);   
  78.        }   
  79.  
  80.        public void Validate(ServiceEndpoint endpoint)   
  81.        {   
  82.  
  83.        }   
  84.  
  85.        #endregion   
  86.    }  
  87. 复制代码 

目前我们已经简单实现了:看看测试:

  1. View Code   
  2.  
  3. Contract:  
  4.  
  5.    [ServiceContract()]   
  6.    [Green.WCFExtensions.ELUnityBehavior(Container = "test", ConfigFile = "App1.config")]     
  7.   public interface IHelloService   
  8.   {   
  9.       [OperationContract]   
  10.       string Say(string name);   
  11.   }  
  12.  
  13. Service:  
  14.  
  15. public class HelloService1 : IHelloService   
  16.    {   
  17.        [Microsoft.Practices.Unity.Dependency("proxy")]   
  18.        public IHelloService Service   
  19.        {   
  20.            get;   
  21.            set;   
  22.        }   
  23.  
  24.        #region IHelloService 成员   
  25.           public string Say(string name)   
  26.        {   
  27.                      return Service.Say(name);   
  28.  
  29.        }   
  30.  
  31.        #endregion   
  32.    }   
  33.  
  34.    public class HelloServiceProxy : IHelloService   
  35.    {   
  36.        #region IHelloService 成员   
  37.        public string Say(string name)   
  38.        {   
  39.            return string.Format("Hello:{0}"name);   
  40.        }   
  41.  
  42.        #endregion   
  43.    }  
  44. 复制代码 

配置app1.config:

<register type="WcfService.IHelloService,WcfService" mapTo="WcfService.HelloServiceProxy,WcfService"  name="proxy"> </register> 
<register type="WcfService.IHelloService,WcfService" mapTo="WcfService.HelloService1,WcfService" > </register>

IOC,AOP博客参考:

  • AOP之PostSharp初见-OnExceptionAspect
  • AOP之PostSharp2-OnMethodBoundaryAspect
  • AOP之PostSharp3-MethodInterceptionAspect
  • AOP之PostSharp4-实现类INotifyPropertyChanged植入
  • AOP之PostSharp5-LocationInterceptionAspect
  • AOP之PostSharp6-EventInterceptionAspect
  • AOP之PostSharp7-解决IOC 不能直接new问题,简化IOC开发和IOC对象LazyLoad
  • http://www.cnblogs.com/whitewolf/category/312638.html
  • 利用Attribute简化Unity框架IOC注入
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI