NOTE : Project has a moved to GitHub : https://github.com/ziyasal/AOPify

Fluent Style AOP demostration

Nuget package available for download https://nuget.org/packages/AOPify


AOPify provides basic AOP features with fluent style.

List of supported methods
  • Until
  • Delay
  • While
  • WhenTrue
  • Log
  • Before
  • After
  • HowLong
  • Catch
  • CatchAndThrow
  • ProcessAsync
  • Run
  • Return
  • RegisterLogger
List of supported attributes with process modes and processors
(sample usage exist in source code)
  • PreProcess : with options
    • PreProcessMode.OnBefore,
    • PreProcessMode.WithInputParameters
  • PostProcess : with options
    • PostProcessMode.OnAfter,
    • PostProcessMode.WithReturnType,
    • PostProcessMode.OnError,
    • PostProcessMode.HowLong
  • Sample Logger for executiong operations

public class ConsoleLogger : ILogger
    {
        public void Log(string format, params object[] args)
        {
            Console.WriteLine(format.FormatWith(args));
        }

        public void Error(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

        public void Error(Exception exception, string message)
        {
            Console.WriteLine("Ex Msg :{0}, Custom message :{1}".FormatWith(exception.Message, message));
        }

        public void Info(string format, params object[] args)
        {
            Log(format, args);
        }
    }
  • Sample Usage
class Program
    {
        static void Main()
        {
            //Before and After
            ConsoleLogger consoleLogger = new ConsoleLogger();
            AOPify.Let
                .Before(() => Console.WriteLine("Before"))
                .After(() => Console.WriteLine("After"))
                .Run(() => Console.WriteLine("Actual"));

           Console.WriteLine("-------------------------------------------------------");
            
            //Catch
            AOPify.Let.Catch(ex => Console.WriteLine(ex.Message)).Run(() =>
            {
                Console.WriteLine("Run with Error");
                throw new Exception("Exception");
            });

            Console.WriteLine("-------------------------------------------------------");

            //Register logger with logger instance
            AOPify.Let
                .RegisterLogger(Log.It.For(typeof(Program)).Use(consoleLogger))
                .Log("Before Log {0}".FormatWith(MethodBase.GetCurrentMethod().Name), "After Log {0}".FormatWith(MethodBase.GetCurrentMethod().Name))
                .Run(() => Console.WriteLine("Run executed"));

             Console.WriteLine("-------------------------------------------------------");

            //Register logger with Generic syntax
            AOPify.Let
                .RegisterLogger(Log.It.For(typeof(Program)).Use<ConsoleLogger>())
                .Log(MethodBase.GetCurrentMethod())
                .Run(() => Console.WriteLine("Run executed"));

            Console.WriteLine("-------------------------------------------------------");

            //HowLong
            AOPify.Let
               .RegisterLogger(Log.It.For(typeof(Program)).Use<ConsoleLogger>())
               .Log(MethodBase.GetCurrentMethod())
               .HowLong()
               .Run(() => Console.WriteLine("Run executed with How Long"));

            Console.WriteLine("-------------------------------------------------------");

            //Other Method test
            MyMethod("Test", 5, 8);

            Console.WriteLine("-------------------------------------------------------");

            Console.Read();
        }

        private static void MyMethod(string testStr, int count, int orderNo)
        {
            AOPify.Let
             .RegisterLogger(Log.It.For(typeof(Program)).Use<ConsoleLogger>())
             .Log(MethodBase.GetCurrentMethod())
             .Run(() => Console.WriteLine("Run Executed"));
        }
    }

Last edited Dec 31, 2012 at 3:48 PM by ziyasal, version 16