namespace Microshaoft.MEF.Contracts { using System; public delegate void ExceptionEventHandler<TSender>(TSender sender, Exception exception); } namespace Microshaoft.MEF.Contracts { using System; public interface IMefChainedProcessorPart<TContainer, TPartKey, TResult, TParameter> { IMefChainedProcessorPart<TContainer, TPartKey, TResult, TParameter> Instance { get; } int Priority { get; } TPartKey Key { get; } void OnOnceProcessAction(params TParameter[] parameters); TResult OnOnceProcessFunc(params TParameter[] parameters); void OnChainedOnceProcessAction(out ChainedProcessNextStep next, params TParameter[] parameters); TResult OnChainedOnceProcessFunc(out ChainedProcessNextStep next, params TParameter[] parameters); void OnChainedOnceAsyncQueueProcessAction(out ChainedProcessNextStep next, params TParameter[] parameters); bool OnChainedOnceAsyncQueueProcessFunc(out ChainedProcessNextStep next, params TParameter[] parameters); event ExceptionEventHandler<IMefChainedProcessorPart<TContainer, TPartKey ,TResult, TParameter>> OnCaughtExceptionInContainer; string GetRuntimeTypeFullName(); Type GetRuntimeType(); } public enum ChainedProcessNextStep { Continue , Break } } namespace Microshaoft.MEF.Contracts { using System; using System.Xml; public interface IMefPartsCompositionContainer<TPart, TPartKey, TResult, TInvokeParameter> { TPart[] Parts { get; } void ImportManyExports(string path); void ChainedInvokeAllPartsProcessAction(params TInvokeParameter[] parameters); TResult ChainedInvokeAllPartsProcessFunc(params TInvokeParameter[] parameters); TResult InvokeOnePartProcessFunc(TPartKey PartKey, params TInvokeParameter[] parameters); void InvokeOnePartProcessAction(TPartKey PartKey, params TInvokeParameter[] parameters); } } //============================================================================================================================= namespace Microshaoft.MEF.CompositionContainers { using Microshaoft; using Microshaoft.MEF.Contracts; using System; using System.Collections.Concurrent; using System.ComponentModel.Composition; using System.Linq; public class XmlMessageProcessorsCompositionContainer : IMefPartsCompositionContainer < IMefChainedProcessorPart < XmlMessageProcessorsCompositionContainer , string , string , string > , string , string , string > { [ImportMany(typeof(IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer,string, string, string>))] public IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer,string, string, string>[] Parts { get; private set; } ConcurrentDictionary<string, IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer,string, string, string>> _dictionary; public void ImportManyExports(string path) { MEFHelper.ImportManyExportsComposeParts<XmlMessageProcessorsCompositionContainer> ( path , this ); var result = Parts.OrderBy(x => x.Priority); if (_dictionary == null) { _dictionary = new ConcurrentDictionary<string, IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string>>(); } result.ToList().ForEach ( x => { _dictionary[x.Key] = x; } ); } public void ChainedInvokeAllPartsProcessAction(params string[] parameters) { throw new NotImplementedException(); } public string ChainedInvokeAllPartsProcessFunc(params string[] parameters) { throw new NotImplementedException(); } public string InvokeOnePartProcessFunc(string PartKey, params string[] parameters) { string r = string.Empty; IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer,string, string, string> part; if (_dictionary.TryGetValue(PartKey, out part)) { r = part.OnOnceProcessFunc(parameters[0]); } return r; } public void InvokeOnePartProcessAction(string PartKey, params string[] parameters) { throw new NotImplementedException(); } } } namespace Microshaoft.MEF.CompositionContainersManagers { using Microshaoft.MEF.CompositionContainers; public static class CompositionContainersManager { private static XmlMessageProcessorsCompositionContainer _xmlMessageProcessorsCompositionContainer = new XmlMessageProcessorsCompositionContainer(); public static XmlMessageProcessorsCompositionContainer xmlMessageProcessorsCompositionContainer { get { return CompositionContainersManager._xmlMessageProcessorsCompositionContainer; } set { CompositionContainersManager._xmlMessageProcessorsCompositionContainer = value; } } } } namespace Microshaoft { using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; public static class MEFHelper { public static void ImportManyExportsComposeParts<T>(string path, T attributedPart) { var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new DirectoryCatalog(path)); var container = new CompositionContainer(catalog); container.ComposeParts(attributedPart); } } } //===================================================================================================================== //可扩展部件样例 namespace Microshaoft.MEF.Parts { using Microshaoft.MEF.Contracts; using Microshaoft.MEF.CompositionContainers; using System; using System.ComponentModel.Composition; [Export(typeof(IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string>))] public class SampleXmlMessageProcessorPart : IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string> { public IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string> Instance { get { throw new NotImplementedException(); } } public int Priority { get { return 100; } } public string Key { get { return "SampleXmlMessageProcessorPart"; } } public void OnOnceProcessAction(params string[] parameters) { throw new NotImplementedException(); } public string OnOnceProcessFunc(params string[] parameters) { return ""; } public void OnChainedOnceProcessAction(out ChainedProcessNextStep next, params string[] parameters) { throw new NotImplementedException(); } public string OnChainedOnceProcessFunc(out ChainedProcessNextStep next, params string[] parameters) { throw new NotImplementedException(); } public void OnChainedOnceAsyncQueueProcessAction(out ChainedProcessNextStep next, params string[] parameters) { throw new NotImplementedException(); } public bool OnChainedOnceAsyncQueueProcessFunc(out ChainedProcessNextStep next, params string[] parameters) { throw new NotImplementedException(); } public string GetRuntimeTypeFullName() { throw new NotImplementedException(); } public Type GetRuntimeType() { throw new NotImplementedException(); } void SampleXmlMessageProcessorPart_OnCaughtExceptionInContainer(IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string> sender, Exception exception) { throw new NotImplementedException(); } public event ExceptionEventHandler<IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string>> OnCaughtExceptionInContainer = ( (x, y) => { } ); } } |