zoukankan      html  css  js  c++  java
  • MEF Parts Sample

    
    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) =>
                                                            {
                                                            }
                                                        );
        }
    }
    
    
  • 相关阅读:
    使用ToolRunner运行Hadoop程序基本原理分析 分类: A1_HADOOP 2014-08-22 11:03 3462人阅读 评论(1) 收藏
    Hadoop入门经典:WordCount 分类: A1_HADOOP 2014-08-20 14:43 2514人阅读 评论(0) 收藏
    Hadoop配置文件 分类: A1_HADOOP 2014-08-19 12:48 1157人阅读 评论(1) 收藏
    【Nutch2.2.1基础教程之3】Nutch2.2.1配置文件 分类: H3_NUTCH 2014-08-18 16:33 1376人阅读 评论(0) 收藏
    8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏
    Hadoop基本原理之一:MapReduce 分类: A1_HADOOP 2014-08-17 19:26 1113人阅读 评论(0) 收藏
    Hadoop1.2.1伪分布模式安装指南 分类: A1_HADOOP 2014-08-17 10:52 1346人阅读 评论(0) 收藏
    【Nutch2.2.1基础教程之6】Nutch2.2.1抓取流程 分类: H3_NUTCH 2014-08-15 21:39 2530人阅读 评论(1) 收藏
    【Nutch2.2.1基础教程之1】nutch相关异常 分类: H3_NUTCH 2014-08-08 21:46 1549人阅读 评论(2) 收藏
    javascript的全局变量 分类: C1_HTML/JS/JQUERY 2014-08-07 11:03 562人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/3307674.html
Copyright © 2011-2022 走看看