fluentAOP QuickStart

  1. Advices
    1. OnBefore Advice
    2. OnAfter Advice
    3. OnCatch and OnFinally Advices
    4. OnInvoke Advice
    5. OnReturn Advice
  2. Implementing additional interfaces
  3. Intercepting multiple calls with a single advice
  4. Intercepting all calls with a single advice
  5. Differentiating between overloaded methods
  6. Stacking several methods on a single fluent instruction
  7. Utilizing predicates to select multiple methods
  8. Intercepting Properties I: Setters
  9. Intercepting Properties II: Getters
  10. Nested advices
  11. Non-target proxies (pending)
  12. Method wrappers (pending)
  13. Intercepting virtual members (pending)
  14. Changing the CIL provider (pending)


1. Advices

1.1 OnBefore Advice

It always runs before the target method execution.

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.Go())
	.OnBefore(()=> Console.WriteLine("1,2,3..."))
	.Save();

foo.Go();

// Console Output:
// 1,2,3...
// Go!


1.2 OnAfter Advice

It runs after the target method execution, but only if it does not throw.

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.Go())
	.OnAfter(() => Console.WriteLine("--Excecution Succeded--"))
	.Save();

foo.Go();

// Console Output:
// Go!
// --Excecution Succeded--


1.3 OnCatch and OnFinally Advices

OnCatch advice runs after the target method execution, but only if it throws.
OnFinally advice runs after the target method execution. But OnAfter and OnCatch advices always run first if they exist.

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.Throw())
		.OnCatch(ex => { throw new InvalidOperationException("--Some Error Message Here--", ex); })
		.OnFinally(() => Console.Write("--Dispose Resources Here--"))
	.Save();

foo.Throw();

//  Console Output:
//  Example_of_how_to_apply_OnCatch_and_OnFinally_advices has failed:
//  --Some Error Message Here--
//  System.InvalidOperationException...
//  --Dispose Resources Here--


1.4 OnInvoke Advice

It runs instead of the target method execution.

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.Throw())
	.OnInvoke(mi => Console.Write("--Replace Method Execution Here--")) // mi: MethodInvocation
	.Save();

foo.Throw();

// Notes: Throw() method does not thow anymore!!
// Console Output:
// --Replace Method Execution Here--


1.5 OnReturn Advice

It runs right before the proxy method execution is over. OnFinally advice runs first if it exist.

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.Null())
	.OnReturn((mi, r) => r ?? "--Replace Any Null Value Here--") //r: ReturnedValue
	.Save();

Console.WriteLine(foo.Null());

// Console Output:
// --Replace Any Null Value Here--


2. Implementing additional interfaces

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.Implement<IDisposable>()
	.InterceptMethod(f => f.Go())
	.OnBefore(() => Console.WriteLine("--OnBefore--"))
	.Save();


3. Intercepting multiple calls with a single advice

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethods(
		f => f.Go(),
		f => f.Throw(),
		f => f.Null())
	.OnBefore(() => Console.WriteLine("--OnBefore--"))
	.OnAfter(() => Console.WriteLine("--OnAfter--"))
	.Save();     


4. Intercepting all calls with a single advice

            var foo = new Proxy<IFoo>()
                .Target(new Foo())
                .InterceptAll()
                .OnBefore(() => Console.WriteLine("--OnBefore--"))
                .OnAfter(() => Console.WriteLine("--OnAfter--"))
                .Save();

            foo.Go(); // Intercepted
            foo.Name = "foo"; // Intercepted
            var name = foo.Name; // Intercepted


5. Differentiating between overloaded methods

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.Overloaded(It.Any<string>()))
	.OnBefore(mi => mi.Arguments[0] = "--Intercepted--") // Replaces arg0
	.Save();

foo.Overloaded(" Hi! ");

// Notes: method was intercepted
// Console Output:
// --Intercepted--

foo.Overloaded( 123 );

// Notes: method was not intercepted
// Console Output:
// 123


6. Stacking several methods on a single fluent instruction

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.Go())
		// Go() Advices:
		.OnBefore(() => Console.WriteLine("1,2,3...")) 
		.OnAfter(() => Console.WriteLine("--Excecution Succeded--"))
	.InterceptMethod(f => f.Throw())
		// Throw() Advices:
		.OnCatch(ex => { throw new InvalidOperationException("--Some Error Message Here--", ex); })
		.OnFinally(() => Console.Write("--Dispose Resources Here--"))
	.InterceptMethod(f => f.Null())
		// Null() Advices:
		.OnReturn((mi, r) => r ?? "--Replace Any Null Value Here--")
	.Save();

// fluentAOP Convention:
// Methods must be followed by one or more advices
// Advices only intercept the closest previously defined method


7. Utilizing predicates to select multiple methods

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptWhere(method => method.Name.EndsWith("loaded")) // Intercepts any method that ends with 'loaded'
	.OnBefore(() => Console.WriteLine("--OnBefore--"))
	.Save();

foo.Overloaded(" Hi! "); // Intercepted
foo.Go(); // Not Intercepted


8. Intercepting Properties I: Setters

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptSetter(f => f.Name)
	.OnBefore(() => Console.WriteLine("--Setting Property--"))
	.Save();

foo.Name = "foo"; // Intercepted
var name = foo.Name; // Not Intercepted


9. Intercepting Properties II: Getters

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptGetter(f => f.Name)
	.OnBefore(() => Console.WriteLine("--Getting Property--"))
	.Save();

foo.Name = "foo"; // Not Intercepted
var name = foo.Name; // Intercepted


10. Nested advices

var foo = new Proxy<IFoo>()
	.Target(new Foo())
	.InterceptMethod(f => f.Go())
	.With(m => 
	{                
		m.OnBefore(()=> Console.WriteLine("--OnBefore--"));
		m.OnAfter(()=> Console.WriteLine("--OnAfter--"));

	}).Save();

foo.Go();

// Console Output:
// --OnBefore--
// Go!
// --OnAfter--


Target Class
The examples shown in this page are based on the following target class definition:

public class Foo : IFoo
{
	public string Name { get; set; }

	public void Go()
	{
		Console.WriteLine("Go!");
	}

	public void Throw()
	{
		throw new Exception();
	}

	public string Null()
	{
		return null;
	}

	public void Overloaded(int i) 
	{
		Console.WriteLine(i);
	}
	
	public void Overloaded(string s) 
	{
		Console.WriteLine(s);
	}
}

public interface IFoo
{
	string Name { get; set; }
	void Go();
	void Throw();
	string Null();
	void Overloaded(int i);
	void Overloaded(string s);
}

Non-target proxies (pending)

Method wrappers (pending)

Intercepting virtual members (pending)

Changing the CIL provider (pending)

Last edited Jan 19, 2011 at 6:22 AM by pablomx, version 6

Comments

No comments yet.