AOP via PIAB and Unity

54 %
46 %
Information about AOP via PIAB and Unity
Technology

Published on October 9, 2009

Author: DmitriNesteruk

Source: slideshare.net

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 extension Custom attributes

Part I

Separation of functional and non-functional requirements Without mixing code →

Post-build assembly modification PostSharp compiler Metaprogramming extension Boo Nemerle Proxies Spring.net Castle Policy Injection AB

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

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

[…] 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

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!

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."; } }

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);

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

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

Custom validation policy

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 

Part II

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

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 

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

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());

Unity Interception

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 

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

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

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

AOP via the Policy Injection Application Block

AOP via Policy Injection & Unity Application Blocks. Dmitri Nesteruk. ... Итак,PIAB и Unity оба выдают объекты по ...
Read more

PPT - AOP via Policy Injection & Unity Application Blocks ...

AOP via Policy Injection & Unity Application ... AOP and Policy Injection AOP overview The world of proxies Policy Injection AB Unity interception ...
Read more

AOP via Policy Injection & Unity Application Blocks ...

AOP via Policy Injection & Unity ... Это расширение фактически делает то же самое что и PIAB. Unity ...
Read more

c# - Unity ICallHandler vs. IInterceptionBehavior - Stack ...

Unity ICallHandler vs. IInterceptionBehavior. ... that for a lot of people the PIAB ... injection unity-container aop unity-interception or ...
Read more

Dmitri Nesteruk dmitri@activemesa - IT-Event.Ru

Dmitri Nesteruk. dmitri@activemesa ... Unity is positioned to supersede PIAB for AOP ... AOP via the Policy Injection Application Block Author:
Read more

IoC and the Unity Application Block - The ASP.NET Site

IoC and the Unity Application Block Once Again; ... AOP in Unity??? ... Martijn Veken has built an extension to use the PIAB and you can read more about it ...
Read more

IDataErrorInfo with Validation Application Block and Unity ...

This is the second article in a planned series of articles concerning AOP with Unity ... MVVM, PIAB, Unity, ... validation via property ...
Read more

Rating of Open Source AOP Frameworks in .NET - CodeProject

... lots of open source AOP ... You can reach him via e ... I have an AOP talk comparing different AOP frameworks (Spring.NET, Castle, PIAB, ...
Read more

Policy Injection Application Block - Home

PIAB can be used to change the behavior of your custom objects, ... AOP) fashion. Code Plex ... How do I use PIAB? Official releases are available via Nuget.
Read more