Click here to Skip to main content
15,896,348 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.8K   285   18  
Add AOP capabilities to MEF by configuration using Dynamic Decorator.
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

using ThirdPartyHR;
using DynamicDecoratorAOP.Mef;
using System.ComponentModel.Composition.Primitives;
using System.ComponentModel;
using NCT;
using DynamicDecoratorAOP;
using System.Threading;
using System.Security.Principal;
using DynamicDecoratorAOP.Concerns;

namespace ConsoleUtil
{
    class LocalConcerns
    {
        static LocalConcerns()
        {
            ConcernsContainer.runtimeAspects.Add("ConsoleUtil.LocalConcerns.NotifyChange", new Decoration(LocalConcerns.NotifyChange, null));
            ConcernsContainer.runtimeAspects.Add("ConsoleUtil.LocalConcerns.SecurityCheck", new Decoration(LocalConcerns.SecurityCheck, null));
        }

        public static void NotifyChange(AspectContext ctx, object[] parameters)
        {
            ((Employee)ctx.Target).NotifyPropertyChanged(ctx.CallCtx.MethodName);
        }

        public static void SecurityCheck(AspectContext ctx, object[] parameters)
        {
            Exception exInner = null;

            try
            {
                if (parameters != null && parameters[0] is WindowsPrincipal && ((WindowsPrincipal)parameters[0]).IsInRole("BUILTIN\\" + "Administrators"))
                {
                    return;
                }
            }
            catch ( Exception ex)
            {
                exInner = ex;
            }

            throw new Exception("No right to call!", exInner);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AggregateCatalog catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.LoadFrom("Employee.dll")));

            Employee target = null;
            IEmployee emp = null;
            var container = new AOPCompositeContainer(catalog);

            emp = container.GetExportProxy<Employee, IEmployee>();
            emp.EmployeeID = 1;
            emp.FirstName = "John";
            emp.LastName = "Smith";
            emp.DateOfBirth = new DateTime(1990, 4, 1);
            emp.DepartmentID = 1;
            emp.DetailsByLevel(2);

            IEmployee emp1 = container.GetExportProxy<Employee, IEmployee>("set_EmployeeID");

            try
            {
                //Commenting out this line will throw out an exception
                //Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                Decoration dec = ConcernsContainer.runtimeAspects["ConsoleUtil.LocalConcerns.SecurityCheck"];
                dec.Parameters = new object[] { Thread.CurrentPrincipal };

                target = container.GetTarget<Employee>();
                target.PropertyChanged += new PropertyChangedEventHandler(PropertyChanged_Listener);
                emp1.EmployeeID = 2;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            container.Dispose(); 
            Console.ReadLine();
        }

        static void PropertyChanged_Listener(object sender, PropertyChangedEventArgs e)
        {
            Console.WriteLine(e.PropertyName.ToString() + " has changed.");
        }
    }
}

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