Click here to Skip to main content
15,891,431 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 245.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 Example1
{
    public interface IActor
    {
        string Name { get; }
    }

    public class Actor : IActor
    {
        public string Name { get; set; }
        public Actor(string Name)
        {
            Console.WriteLine("Real construction for {0}", Name);
            this.Name = Name;
        }
    }

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

        public TheConcern(string Name)
        {
            if (String.IsNullOrEmpty(Name))
            {
                Name = "New unknown";
                Console.WriteLine("The provided Name argument is invalid, so let's change it!");
            }
            This = new Actor(Name); // real construction
        }
    }

    public class ConstructorInterceptionExample : IExample
    {
        public void RegisterJoinPoints()
        {
            // Get a reference to constructor of both class and join them
            AOP.Registry.Join
                (
                    typeof(Actor).GetConstructors().First(),
                    typeof(TheConcern).GetConstructors().First()
                );
        }

        public void RunExample()
        {
            Console.WriteLine("Behaviour without interception and a provided Name ('Mickey Rourke')");
            var actor = new Actor("Mickey Rourke");
            Console.WriteLine("Actor {0} is mounted", actor.Name);
            Console.WriteLine();

            Console.WriteLine("Behavior without interception and no provided Name");
            var actor2 = new Actor("");
            Console.WriteLine("Actor {0} is mounted", actor2.Name);
            Console.WriteLine();

            Console.WriteLine("Behaviour WITH interception and a provided Name ('Mickey Rourke')");
            var actor3 = (IActor)AOP.Factory.Create<Actor>("Mickey Rourke");
            Console.WriteLine("Actor {0} is mounted", actor3.Name);
            Console.WriteLine();
            Console.WriteLine("Behavior WITH interception and no provided Name");
            var actor4 = (IActor)AOP.Factory.Create<Actor>("");
            Console.WriteLine("Actor {0} is mounted", actor4.Name);
        }

        public string About
        {
            get
            {
                return
                     "Demo 1: Demonstrate interception of a Constructor" + Environment.NewLine +
                     "The concern is modifying the parameter of the constructor to the value 'New Unknown' in case it is an empty string";

            }
        }
    }
}

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