Aspect-Oriented Programming with Dependency Injection Mark Seemann @ploeh
Cross-Cutting Concerns Security Security Auditing Auditing Logging Logging Caching Caching Metering Metering Stability patterns Stability patterns Transactions Transactions
Objective • Single Responsibility Principle • Open/Closed Principle SOLID • Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle DRY •Don’t Repeat Yourself
Staying SOLID • Single Responsibility Principle • Open/Closed Principle SOLID • Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle DRY •Don’t Repeat Yourself
Prerequisite Program to an interface, not an implementation - Design Patterns, 1995
Conjecture • It’s always possible to apply a Decorator to an interface
Using Decorators to add aspects Client IFoo ConcreteFoo
Using Decorators to add aspects IFoo AuditingFoo Client IFoo ConcreteFoo
Using Decorators to add aspects IFoo SecureFoo IFoo AuditingFoo Client IFoo ConcreteFoo
Staying SOLID with Decorators DEMO
Staying SOLID • Single Responsibility Principle • Open/Closed Principle SOLID • Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle DRY •Don’t Repeat Yourself
Staying DRY • Single Responsibility Principle • Open/Closed Principle SOLID • Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle DRY •Don’t Repeat Yourself
Dynamic interception From here Commonly on, dynamic supplied by interception a DI is required Container
DI Containers with Interception Castle Unity Windsor Spring.NET LinFu
Using a DI Container DEMO
Interception Xyz
Interception IFoo XyzFoo Xyz Xyz
Interception IFoo XyzFoo Xyz IBar Xyz XyzBar Xyz
Interception IFoo XyzFoo Xyz IBar Xyz XyzBar Xyz IBaz XyzBaz Xyz
Adding interceptors DEMO
Pointcut IFoo ConcreteFoo Xyz IBar Abc ConcreteBar IBaz Æøå ConcreteBaz
Pointcut IFoo ConcreteFoo Xyz IBar Match Abc ConcreteBar IBaz Æøå ConcreteBaz
Adding a pointcut DEMO
Objective • Single Responsibility Principle • Open/Closed Principle SOLID • Liskov Substitution Principle • Interface Segregation Principle • Dependency Inversion Principle DRY •Don’t Repeat Yourself
Staying Staying DRY SOLID only requires requires dynamic loose interception coupling
http://blog.ploeh.dk/ @ploeh MARK SEEMANN
Recommend
More recommend