由于工作幾個月來框架開發(fā)已經(jīng)好久沒寫博客了,今天想抽點(diǎn)時間出來冒泡。在領(lǐng)域驅(qū)動開發(fā)中SOA已經(jīng)成為我們開發(fā)的主流技術(shù),在面對當(dāng)前眾多的UI框架選擇(asp.net mvc,silverlight,wpf,winform,wp7等一些列甚至跨語言),我們能夠重用和抽象的業(yè)務(wù)邏輯是不會變化的,為了屏蔽這些UI的復(fù)雜性和差異性,我們可能會采用諸如wcf soap的服務(wù)發(fā)布。再說在領(lǐng)域驅(qū)動開發(fā)中我們肯定會把各個邏輯分層次隔離解除耦合,這就出現(xiàn)了N層架構(gòu),在于我們的每一層次之間的耦合度當(dāng)前流程的解決方案IOC框架,以及業(yè)務(wù)解耦A(yù)OP。這里要解決的是SOA框架WCF和IOC框架的結(jié)合。
創(chuàng)新互聯(lián)是一家專業(yè)提供疊彩企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設(shè)、H5建站、小程序制作等業(yè)務(wù)。10年已為疊彩眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。WCF框架是一個極易擴(kuò)展的框架,提供了非常多的擴(kuò)展點(diǎn)(InstanceProvider,MessageInspector,CallContextInitializer,MessageFilter,MessageFormatter,ParameterInspector等等)。在這里IOC是作為一個容器組裝創(chuàng)建的框架,我們需要的是對服務(wù)對象的創(chuàng)建,所以我們選擇的當(dāng)然是InstanceProvider擴(kuò)展點(diǎn)。
多說一句廢話,正如前篇利用Attribute簡化Unity框架IOC注入和 AOP之PostSharp7-解決IOC 不能直接new問題,簡化IOC開發(fā)和IOC對象LazyLoad中所說,我是一個固執(zhí)的人,個人希望注入支持自定義配置文件,不喜歡把配置信息全部寫在一個web.config/app.config中,也不喜歡el的寫在同一個外部配置文件中,傾向于每個模塊在一個不同的配置文件,并在模塊中在區(qū)分container容器,所以特別寫了每個單獨(dú)配置文件的延時加載,緩存。
下面就是對InstanceProvider的擴(kuò)展:
- View Code
- public class ELUnityInstanceProvider : IInstanceProvider
- {
- private Type contractType;
- private string container;
- private string configFile;
- private string name;
- private static object lockObj = new object();
- private static Dictionary<string, UnityConfigurationSection> sectionDict = new Dictionary<string, UnityConfigurationSection>();
- public ELUnityInstanceProvider(Type contractType, string container, string configFile, string name)
- {
- this.name = name;
- this.configFile = configFile;
- this.contractType = contractType;
- this.container = container;
- }
- #region IInstanceProvider 成員
- public object GetInstance(System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
- {
- Microsoft.Practices.Unity.Configuration.UnityConfigurationSection unitySection = GetUnityConfigurationSection();
- if (unitySection != null)
- {
- var container = new Microsoft.Practices.Unity.UnityContainer().LoadConfiguration(unitySection, string.IsNullOrEmpty(this.container) ? unitySection.Containers.Default.Name : this.container);
- var obj = string.IsNullOrEmpty(this.name) ? container.Resolve(this.contractType) : container.Resolve(this.contractType, this.name);
- var piabAtttr = obj.GetType().GetCustomAttributes(typeof(ELPolicyinjectionAttribute), false) as ELPolicyinjectionAttribute[];
- if (piabAtttr.Length > 0)
- {
- obj = Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Wrap(this.contractType, obj);
- }
- return obj;
- }
- return null;
- }
- private Microsoft.Practices.Unity.Configuration.UnityConfigurationSection GetUnityConfigurationSection()
- {
- if (!string.IsNullOrEmpty(this.configFile))
- {
- if (!sectionDict.ContainsKey(this.configFile))
- {
- lock (lockObj)
- {
- if (!sectionDict.ContainsKey(this.configFile))
- {
- Microsoft.Practices.Unity.Configuration.UnityConfigurationSection unitySection = null;
- var fileMap = new System.Configuration.ExeConfigurationFileMap { ExeConfigFilename = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, this.configFile) };
- System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, System.Configuration.ConfigurationUserLevel.None);
- unitySection = configuration == null ? null : configuration.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName) as Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;
- if (unitySection == null)
- return null;
- sectionDict.Add(this.configFile, unitySection);
- }
- }
- }
- return sectionDict[this.configFile];
- }
- return System.Configuration.ConfigurationManager.GetSection(Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.SectionName) as Microsoft.Practices.Unity.Configuration.UnityConfigurationSection;
- }
- public object GetInstance(System.ServiceModel.InstanceContext instanceContext)
- {
- return this.GetInstance(instanceContext, null);
- }
- public void ReleaseInstance(System.ServiceModel.InstanceContext instanceContext, object instance)
- {
- IDisposable disposable = instance as IDisposable;
- if (disposable != null)
- {
- disposable.Dispose();
- }
- instance = null;
- }
- #endregion
- }
- 復(fù)制代碼
下面我們需要已Attribute方式貼在Contract上:
- View Code
- public class ELUnityBehaviorAttribute : Attribute, IContractBehavior
- {
- public string Container
- {
- get;
- set;
- }
- public string ConfigFile
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- #region IContractBehavior 成員
- public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
- {
- }
- public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
- {
- }
- public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.DispatchRuntime dispatchRuntime)
- {
- dispatchRuntime.InstanceProvider = new ELUnityInstanceProvider(contractDescription.ContractType, this.Container, this.ConfigFile, this.Name);
- }
- public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
- {
- }
- #endregion
- }
- 復(fù)制代碼
對于wcf同時我們也該支持配置文件擴(kuò)展:
- View Code
- public class ELUnityBehaviorElement : BehaviorExtensionElement
- {
- [ConfigurationProperty("Container", IsRequired = false, DefaultValue = "")]
- public string Container
- {
- get;
- set;
- }
- [ConfigurationProperty("ConfigFile", IsRequired = false, DefaultValue = "")]
- public string ConfigFile
- {
- get;
- set;
- }
- [ConfigurationProperty("Name", IsRequired = false, DefaultValue = "")]
- public string Name
- {
- get;
- set;
- }
- protected override object CreateBehavior()
- {
- return new ELUnityBehavior(this.Container, this.ConfigFile, this.Name);
- }
- public override Type BehaviorType
- {
- get { return typeof(ELUnityBehavior); }
- }
- }
- public class ELUnityBehavior : IEndpointBehavior
- {
- public string Container
- {
- get;
- set;
- }
- public string ConfigFile
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public ELUnityBehavior(string container, string configFile, string name)
- {
- this.Name = name;
- this.ConfigFile = configFile;
- this.Container = container;
- }
- #region IEndpointBehavior 成員
- public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
- {
- }
- public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
- {
- }
- public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
- {
- endpointDispatcher.DispatchRuntime.InstanceProvider = new ELUnityInstanceProvider(endpoint.Contract.ContractType, this.Container, this.ConfigFile, this.Name);
- }
- public void Validate(ServiceEndpoint endpoint)
- {
- }
- #endregion
- }
- 復(fù)制代碼
目前我們已經(jīng)簡單實(shí)現(xiàn)了:看看測試:
- View Code
- Contract:
- [ServiceContract()]
- [Green.WCFExtensions.ELUnityBehavior(Container = "test", ConfigFile = "App1.config")]
- public interface IHelloService
- {
- [OperationContract]
- string Say(string name);
- }
- Service:
- public class HelloService1 : IHelloService
- {
- [Microsoft.Practices.Unity.Dependency("proxy")]
- public IHelloService Service
- {
- get;
- set;
- }
- #region IHelloService 成員
- public string Say(string name)
- {
- return Service.Say(name);
- }
- #endregion
- }
- public class HelloServiceProxy : IHelloService
- {
- #region IHelloService 成員
- public string Say(string name)
- {
- return string.Format("Hello:{0}", name);
- }
- #endregion
- }
- 復(fù)制代碼
配置app1.config:
<register type="WcfService.IHelloService,WcfService" mapTo="WcfService.HelloServiceProxy,WcfService" name="proxy"> </register>創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
新聞標(biāo)題:WCF利用企業(yè)庫Unity框架的IOC層次解耦-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://jinyejixie.com/article26/cciojg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計、做網(wǎng)站、標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計公司、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容