Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Configurable Aspects for MEF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
6 Sep 2011CPOL9 min read 39.5K   285   18  
Add AOP capabilities to MEF by configuration using Dynamic Decorator.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;

namespace DynamicDecoratorAOP.Configuration
{
    public class AspectObjectConfigElement : ConfigurationElement
    {
        public AspectObjectConfigElement(String newName, String newType, String newInterface, String newMethods, String newPreDecoration, String newPostDecoration)
        {
            Name = newName;
            TargetType = newType;
            Interface = newInterface;
            Methods = newMethods;
            PreDecoration = newPreDecoration;
            PostDecoration = newPostDecoration;
        }

        public AspectObjectConfigElement() {}

        public AspectObjectConfigElement(String elementName)
        {
            Name = elementName;
        }

        [ConfigurationProperty("name", DefaultValue = "0", IsRequired = true, IsKey = true)]
        public string Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }

        [ConfigurationProperty("type", DefaultValue = "object", IsRequired = true, IsKey = false)]
        public string TargetType
        {
            get
            {
                return (string)this["type"];
            }
            set
            {
                this["type"] = value;
            }
        }

        [ConfigurationProperty("interface", DefaultValue = "object", IsRequired = true, IsKey = false)]
        public string Interface
        {
            get
            {
                return (string)this["interface"];
            }
            set
            {
                this["interface"] = value;
            }
        }

        [ConfigurationProperty("methods", DefaultValue = null, IsRequired = false)]
        public string Methods
        {
            get
            {
                return (string)this["methods"];
            }
            set
            {
                this["methods"] = value;
            }
        }

        [ConfigurationProperty("predecoration", DefaultValue = null, IsRequired = false)]
        public string PreDecoration
        {
            get
            {
                return (string)this["predecoration"];
            }
            set
            {
                this["predecoration"] = value;
            }
        }

        [ConfigurationProperty("postdecoration", DefaultValue = null, IsRequired = false)]
        public string PostDecoration
        {
            get
            {
                return (string)this["postdecoration"];
            }
            set
            {
                this["postdecoration"] = value;
            }
        }

        protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
        {
            base.DeserializeElement(reader, serializeCollectionKey);
            // You can your custom processing code here.
        }


        protected override bool SerializeElement(System.Xml.XmlWriter writer, bool serializeCollectionKey)
        {
            bool ret = base.SerializeElement(writer, serializeCollectionKey);
            // You can enter your custom processing code here.
            return ret;
        }


        protected override bool IsModified()
        {
            bool ret = base.IsModified();
            // You can enter your custom processing code here.
            return ret;
        }
    }

    public class AspectObjectsCollection : ConfigurationElementCollection
    {
        public AspectObjectsCollection()
        {
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new AspectObjectConfigElement();
        }


        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return new AspectObjectConfigElement(elementName);
        }


        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((AspectObjectConfigElement)element).Name;
        }


        public new string AddElementName
        {
            get { return base.AddElementName; }
            set { base.AddElementName = value; }
        }

        public new string ClearElementName
        {
            get { return base.ClearElementName; }
            set { base.ClearElementName = value; }
        }

        public new string RemoveElementName
        {
            get { return base.RemoveElementName; }
        }

        public new int Count
        {
            get { return base.Count; }
        }


        public AspectObjectConfigElement this[int index]
        {
            get
            {
                return (AspectObjectConfigElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public AspectObjectConfigElement this[string Name]
        {
            get
            {
                return (AspectObjectConfigElement)BaseGet(Name);
            }
        }

        public int IndexOf(AspectObjectConfigElement aspectObj)
        {
            return BaseIndexOf(aspectObj);
        }

        public void Add(AspectObjectConfigElement aspectObj)
        {
            BaseAdd(aspectObj);
            // Add custom code here.
        }

        protected override void
            BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
            // Add custom code here.
        }

        public void Remove(AspectObjectConfigElement aspectObj)
        {
            if (BaseIndexOf(aspectObj) >= 0)
                BaseRemove(aspectObj.Name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
            // Add custom code here.
        }
    }

    public class DynamicDecoratorSection : ConfigurationSection
    {
        [ConfigurationProperty("objectTemplates", IsDefaultCollection = false)]
        public AspectObjectsCollection objectTemplates
        {
            get
            {
                AspectObjectsCollection objectTemplatesCollection = (AspectObjectsCollection)base["objectTemplates"];
                return objectTemplatesCollection;
            }
        }

        //protected override void DeserializeSection(System.Xml.XmlReader reader)
        //{
        //    base.DeserializeSection(reader);
        //    // You can add custom processing code here.
        //}

        //protected override string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
        //{
        //    string s = base.SerializeSection(parentElement, name, saveMode);
        //    // You can add custom processing code here.
        //    return s;
        //}

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Object-oriented (OO) is about "classes" not "objects". But I truly believe that "objects" deserve more our attentions. If you agree, read more on... Dynamic Object Programming (DOP), Component-Based Object Extender (CBO Extender), AOP Container and Dynamic Decorator Pattern.

Mobile development is not just another type of front end. The real challenge is actually in the back end: How to present meaningful information in time to mobile users with exponentially increased data flooding around? Here is my first mobile solution: SmartBars - Barcode Reader, Price Comparison and Coupons.

Gary lives in southeast Michigan. My first programming language is FORTRAN. For the last a few years, I have primarily focused on .NET technologies with Mobile Development as my newest interest.

Comments and Discussions