Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Dmitri Nesteruk
dmitri@activemesa.com
http://activemesa.com
http://spbalt.net
AOP and Policy Injection
   AOP overview
   The world of proxies
   Policy Injection AB
Unity interception
   Interceptor ...
Part I
Separation of functional and non-functional
requirements
   Without mixing code




                   →
Post-build assembly modification
   PostSharp
                                   compiler
Metaprogramming                 ...
A proxy is a substitute object
   It has both the original functionality
   And the AOP-added features
Can take an existin...
Part of Enterprise Library
http://entlib.codeplex.com
Allows you to intercept calls to an object
   Method
   Property Get...
[…] However, the Policy Injection Application
Block is not an AOP framework
implementation for the following reasons:
   I...
By default, PIAB uses transparent proxies
   Used in remoting
To create a proxy for your class, it must
   Either implemen...
public interface IPerson   public class Person : IPerson
{                          {
  int Age {                  public ...
Concrete types
   Only if object derived from MarshalByRefObject
   var p = PolicyInjection.Create<Person>(…);
   Will fai...
A policy needs to specify
   Where it needs to be applied (matching rules)
   What needs to be done (handler)
     EntLib ...
PIAB comes pre-            Authorization
packaged with several
‘stock’ handlers           Caching

These handlers integrat...
Custom validation policy
Implement ICallHandler                ICallHandler is
                                    defined in Unity AB 
Implement ...
Part II
Unity is capable of giving us predefined object
instances
   IPerson p = uc.Resolve<IPerson>();
IPerson can resolve to a p...
Both Unity and PIAB give out objects
   Unity resolves dependencies
   PIAB creates proxies
Synergy? Absolutely.
   In Uni...
Since v1.2, Unity has a…
   Microsoft.Practices.Unity.InterceptionExtension
Works just like PIAB, but
   Easy to configure...
Derive from HandlerAttribute
   Override CreateHandler()

Make a handler that implements ICallHandler
   Define Invoke()
 ...
Unity Interception
PIAB is useful for ‘stock’ interception
Unity is useful for general-purpose
interception
Unity is positioned to supersede ...
AOP frameworks to try
  PostSharp http://postsharp.org
  Our webcast at http://www.techdays.ru/videos/1320.html
  Spring.n...
a   l   b   v   o   b   q   l   l   k   u   t   m   y   w   m   w   r   e   e   r   q   q   m   q   i   q   d   n   w   g ...
Upcoming SlideShare
Loading in …5
×

AOP via PIAB and Unity

2,421 views

Published on

Published in: Technology
  • Be the first to comment

AOP via PIAB and Unity

  1. 1. Dmitri Nesteruk dmitri@activemesa.com http://activemesa.com http://spbalt.net
  2. 2. AOP and Policy Injection AOP overview The world of proxies Policy Injection AB Unity interception Interceptor extension Custom attributes
  3. 3. Part I
  4. 4. Separation of functional and non-functional requirements Without mixing code →
  5. 5. Post-build assembly modification PostSharp compiler Metaprogramming extension Boo Nemerle Proxies Spring.net Castle Policy Injection AB
  6. 6. A proxy is a substitute object It has both the original functionality And the AOP-added features Can take an existing object and intercept property access, method calls, etc. Must (typically) use an interface That way, implementation can be transparently substituted
  7. 7. Part of Enterprise Library http://entlib.codeplex.com Allows you to intercept calls to an object Method Property Get/Set Lets you set up a handler pipeline
  8. 8. […] However, the Policy Injection Application Block is not an AOP framework implementation for the following reasons: It uses interception to enable only pre-processing handlers and post-processing handlers. It does not insert code into methods. It does not provide interception for class constructors. Policy Injection Application Block, MSDN http://msdn.microsoft.com/en-us/library/cc511729.aspx
  9. 9. By default, PIAB uses transparent proxies Used in remoting To create a proxy for your class, it must Either implement a well-known interface; or Derive from MarshalByRefObject PIAB uses a factory to create objects Just like Unity!
  10. 10. public interface IPerson public class Person : IPerson { { int Age { public Person(int age, get; set; string firstName, } string lastName) string FirstName { { get; set; Age = age; } FirstName = firstName; string LastName { LastName = lastName; get; set; } } string GetInfo(); public string GetInfo() } { return FirstName + " " + LastName + " is " + Age + " years old."; } }
  11. 11. Concrete types Only if object derived from MarshalByRefObject var p = PolicyInjection.Create<Person>(…); Will fail otherwise Interface types var p = PolicyInjection.Create<Person, IPerson>( 27, "Dmitri", "Nesteruk"); Can apply to existing object Person p = new Person(27, "Dmitri", "Nesteruk"); IPerson wp = PolicyInjection.Wrap<IPerson>(p);
  12. 12. A policy needs to specify Where it needs to be applied (matching rules) What needs to be done (handler) EntLib provides several ‘stock’ handlers Example In each property assignment Check that the value is not null And throw ArgumentException if it is Typically edited in designer
  13. 13. PIAB comes pre- Authorization packaged with several ‘stock’ handlers Caching These handlers integrate Custom with other application blocks Exception handling Logging Can always write your own Performance Including a designer Validation
  14. 14. Custom validation policy
  15. 15. Implement ICallHandler ICallHandler is defined in Unity AB  Implement Invoke() to Do your own pre/postprocessing To invoke next item in chain, use return getNext()(input, getNext); Make sure to tag handler class with [ConfigurationElementType( typeof(CustomCallHandlerData))] Create your custom policy Warning: sign the assembly; or Edit config manually 
  16. 16. Part II
  17. 17. Unity is capable of giving us predefined object instances IPerson p = uc.Resolve<IPerson>(); IPerson can resolve to a predefined type, but it doesn’t have to! It can resolve instead to a type constructed at runtime This type can use the functionality of Person and implement
  18. 18. Both Unity and PIAB give out objects Unity resolves dependencies PIAB creates proxies Synergy? Absolutely. In Unity, we can decide whether or not a proxy needs to be created Then we can selectively apply PIAB And yield an object conforming to an interface Interoperation between Unity and PIAB is possible, but… There is an alternative 
  19. 19. Since v1.2, Unity has a… Microsoft.Practices.Unity.InterceptionExtension Works just like PIAB, but Easy to configure in code Integrates with Unity container Artefacts Call handler (as per PIAB) Handler attribute – used to decorate elements it affects Unity extension configuration
  20. 20. Derive from HandlerAttribute Override CreateHandler() Make a handler that implements ICallHandler Define Invoke() Use getNext().Invoke() to call decorated method Add code around it as necessary Add and configure interception uc.AddNewExtension<Interception>(); uc.Configure<Interception>() .SetInterceptorFor<IPerson>( new TransparentProxyInterceptor());
  21. 21. Unity Interception
  22. 22. PIAB is useful for ‘stock’ interception Unity is useful for general-purpose interception Unity is positioned to supersede PIAB for AOP Best for dynamic reconfiguration Use precompilation for speed 
  23. 23. AOP frameworks to try PostSharp http://postsharp.org Our webcast at http://www.techdays.ru/videos/1320.html Spring.net http://www.springframework.net/ Castle http://www.castleproject.org Enterprise library http://entlib.codeplex.com
  24. 24. a l b v o b q l l k u t m y w m w r e e r q q m q i q d n w g s s w d a v p d v n u x j l s y t u b n b y c t h r r y u v a s t a d t n z f f x g q h b j j p y o w s i g i c i i g s o f n f r j f d c f g m k w u y j v b v e m i t i j x u v w s j u g u y l b o c m y k u b w s w n p x i o k a y c q o s u n k s c g x j x j e q p h j i a c m j z h c k v x k a k f e c r u u x q p p k o f w g x b v j m b e l e e w k s c v n n o g c z w w f w i n e h j q l h x u v j o m h g s x a j z b d n u a s c n a j i x w i n w z j d s p n w i p c n d s r m j h z q j g b w j m e z k j v a z o u q w d c j c f o x w t h v s r h o m j y n a u p p u p h z n s j r m b z o w k i n t h l i k z w m z m f x c h o m w x b s m x u c j x o s h x u e t p u x e o v l h a y p f f v a x z x l z u l c l n q g e g m x y k k k q j n h p i j w i p d d a x z s z e m p c l i m s u g e i z o m q p r p d w m y q t o v m p T H E y E N D v z d c z x m g q q r h n b j i b q i p x n h w i d o h m a w c x m g h c y r i k n p n d m c x l z e h h s c l f s y l k j s p t d q e b k v u x k m k z p g k e n a f h h r o x v w k u j u t n e u q f a d n e d y y y f c z c a p x y f b r w e y o f a v f h z r y a n z u q r o g n f p x l j y l u a n r d o r v k m f j y n h p c c t k x y t b f j r n x g c z h s p c e i q g x k p f g r n l y i i f t i s b i f c k c h e s l w y s u p d v x b r l q l k i z d z w s a w r i i u m n i x r c j n d h n w g s f s i l h a b h l h x m v p t e g k n o i s g s x v b o k e c i j y b e d r t p e x v r c w u v d s d o a z t t m u i u v u b p l w c p x n k k v a a v b b s e e f d b f y i v c j k r g r y t j a m f v h b f s b z l i n a x c l r l z i v l c b n u d l l g u y r t t u q t l y j l q u h a o u o p t g v l q q r k r q y p l z d x n q n q v t f b u h r y n k f q i t h i u w i n m l o c c c

×