Created
February 24, 2013 16:30
-
-
Save dzharii/5024436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using Castle.DynamicProxy; | |
| using Humanizer; | |
| namespace TestAOPDemo | |
| { | |
| public class Program | |
| { | |
| public class MyProxy<TClass> where TClass : class | |
| { | |
| private TClass _object; | |
| public TClass Object | |
| { | |
| get { return _object ?? (_object = CreateNewClass()); } | |
| } | |
| private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator(); | |
| private TClass CreateNewClass() | |
| { | |
| return ProxyGenerator.CreateClassProxy<TClass>(new ClassInterceptor()); | |
| } | |
| } | |
| public class ClassInterceptor : IInterceptor | |
| { | |
| public void Intercept(IInvocation invocation) | |
| { | |
| string readableMethodName = invocation.Method.Name.Humanize(); | |
| Console.ForegroundColor = ConsoleColor.Green; | |
| Console.Write("[Started] "); | |
| Console.ResetColor(); | |
| Console.WriteLine("Step: " + readableMethodName); | |
| invocation.Proceed(); | |
| Console.ForegroundColor = ConsoleColor.Yellow; | |
| Console.Write("[Finished] "); | |
| Console.ResetColor(); | |
| Console.WriteLine("Step: " + readableMethodName); | |
| Console.ForegroundColor = ConsoleColor.Magenta; | |
| Console.WriteLine(new String('~', 40)); | |
| Console.ResetColor(); | |
| } | |
| } | |
| public abstract class Animal | |
| { | |
| public virtual void HappyBirthday() | |
| { | |
| Console.WriteLine("\tNew Animal was Created"); | |
| } | |
| } | |
| public class Cow : Animal | |
| { | |
| public virtual void SayMoo() | |
| { | |
| Console.WriteLine("\tCow: Mooooo!"); | |
| } | |
| } | |
| public class Tester : Animal | |
| { | |
| public virtual void Assert_that_each_tester_should_read_DZis_is_a_test_Blog() | |
| { | |
| Console.WriteLine("\tTester: That's true! That's true!: http://blog.zhariy.com"); | |
| } | |
| } | |
| public static class Steps | |
| { | |
| public static Cow Cow { get { return new MyProxy<Cow>().Object; } } | |
| public static Tester Tester { get { return new MyProxy<Tester>().Object; } } | |
| } | |
| static void Main(string[] args) | |
| { | |
| Steps.Cow.HappyBirthday(); | |
| Steps.Cow.SayMoo(); | |
| var tester = Steps.Tester; | |
| tester.HappyBirthday(); | |
| tester.Assert_that_each_tester_should_read_DZis_is_a_test_Blog(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment