Click here to Skip to main content
15,885,546 members
Articles / DevOps / Load Testing

Aspect Oriented Programming: learn step by step and roll your own implementation!

Rate me:
Please Sign up or sign in to vote.
4.98/5 (129 votes)
11 Sep 2013CPOL21 min read 242.8K   2K   240  
A journey into AOP land with concerns, pointcuts, joinpoints, advices, aspects, interceptors, proxies, targets, mix-ins, composites...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyAOP;

namespace Example2
{
    public interface IActor
    {
        string Name { get; set; }
        void Act();
    }

    public class Actor : IActor
    {
        public string Name { get; set; }

        public void Act()
        {
            Console.WriteLine("My name is '{0}'. I am such a good actor!", Name);
        }
    }

    public class TheConcern : IConcern<Actor>
    {
        public Actor This { get; set; }

        public string Name 
        {
            set
            {
                This.Name = value + "'. Hi, " + value + " you've been hacked";
            }
        }

        public void Act()
        {
            This.Act();
            Console.WriteLine("You think so...!");
        }
    }

    public class MethodAndPropertiesInterception : IExample
    {
        public void RegisterJoinPoints()
        {
            // Weave the Name property setter
            AOP.Registry.Join
                (
                    typeof(Actor).GetProperty("Name").GetSetMethod(),
                    typeof(TheConcern).GetProperty("Name").GetSetMethod()
                );

            // Weave the Act method
            AOP.Registry.Join
                (
                    typeof(Actor).GetMethod("Act"),
                    typeof(TheConcern).GetMethod("Act")
                );
        }

        public void RunExample()
        {
            Console.WriteLine("Behavior without interception");

            var actor = new Actor();
            actor.Name = "the Dude";
            actor.Act();
            Console.WriteLine();

            Console.WriteLine("Behavior WITH interception");
            var actor1 = (IActor)AOP.Factory.Create<Actor>();
            actor1.Name = "the Dude";
            actor1.Act();
            Console.WriteLine();
        }

        public string About
        {
            get
            {
                return  "Demo 2: Demonstrate interception of instance method and properties";
            }
        }
    }
}

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
Architect
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions