34. The Good: AOP solves problems Public Function ReadEmployeeSalary( id as Integer ) as Single Trace.WriteLine( String.Format( "Reading employee # {0}'s salary", id ) ) If System.Threading.Thread.CurrentPrincipal.IsInRole( "manager" ) Then Try ' Code that reads employee's salary Catch e as FatalException Logging.LogFatalException( e, "ReadEmployeeSalary" ) End Try End If End Sub
35.
36. Aspects can implement policies Public Function ReadEmployeeSalary( id as Integer ) as Single Trace.WriteLine( String.Format( "Reading employee # {0}'s salary", id ) ) If System.Threading.Thread.CurrentPrincipal.IsInRole( "manager" ) Then Try ' Code that reads employee's salary Catch e as FatalException Logging.LogFatalException( e, "ReadEmployeeSalary" ) End Try End If End Sub 1 2 3
37. Aspects can implement policies public class SecurityPolicy { public bool AccessCheck() { return System.Threading.Thread.CurrentPrincipal.IsInRole( "manager" ); } } public class TracingPolicy { public void Before( CallContext c ) { Trace.WriteLine( String.Format( "Called: {0}", c.MethodName ) ); } } public class ExceptionPolicy { public void Thrown( FatalException e ) { Logger.LogFatalException( e ); } } 1 2 3
38. Weaving aspects before methods public class TracingPolicy { public void Before( CallContext c ) { Trace.WriteLine( String.Format( "Called: {0}", c.MethodName ) ); } } <weave> <aspect target="public *.*::*( * )" type="Before" method="TracingPolicy.Before( CallContext )"/> </weave>
39. Weaving aspects around methods public class SecurityPolicy { public bool AccessCheck() { return System.Threading.Thread.CurrentPrincipal.IsInRole( "manager" ); } } <weave> <aspect target=" [ interface:ManagerOnly == true ] public *.*::*( * )" type="Around" method="SecurityPolicy.AccessCheck()"/> </weave> [ ManagerOnly ] public interface ISensitiveEmployeeData { float ReadEmployeeSalary( int id ); ... }
40. Weaving aspects around methods public class ExceptionPolicy { public void Thrown( FatalException e ) { Logger.LogFatalException( e ); } } <weave> <aspect target="public *.*::*( * )" type="Thrown( FatalException )" method="ExceptionPolicy.Thrown( FatalException )"/> </weave>