+ All Categories
Home > Documents > AOP via Policy Injection & Unity Application Blocks

AOP via Policy Injection & Unity Application Blocks

Date post: 23-Feb-2016
Category:
Upload: tuwa
View: 49 times
Download: 0 times
Share this document with a friend
Description:
AOP via Policy Injection & Unity Application Blocks. Dmitri Nesteruk [email protected] http://activemesa.com http://spbalt.net. Agenda. AOP and Policy Injection AOP overview The world of proxies Policy Injection AB Unity interception Interceptor extension Custom attributes. - PowerPoint PPT Presentation
24
AOP via Policy Injection & Unity Application Blocks Dmitri Nesteruk [email protected] http://activemesa.com http://spbalt.net
Transcript
Page 1: AOP via Policy Injection  &  Unity Application Blocks

AOP via Policy Injection & Unity Application BlocksDmitri [email protected]://activemesa.com http://spbalt.net

Page 2: AOP via Policy Injection  &  Unity Application Blocks

AgendaAOP and Policy Injection

AOP overviewThe world of proxiesPolicy Injection AB

Unity interceptionInterceptor extensionCustom attributes

Page 3: AOP via Policy Injection  &  Unity Application Blocks

AOP and Policy InjectionPart I

Page 4: AOP via Policy Injection  &  Unity Application Blocks

Aspect-Oriented Programming

Separation of functional and non-functional requirements

Without mixing code

Page 5: AOP via Policy Injection  &  Unity Application Blocks

Ways of adding AOPPost-build assembly modification

PostSharpMetaprogramming

BooNemerle

ProxiesSpring.netCastlePolicy Injection AB

compilerextension

Page 6: AOP via Policy Injection  &  Unity Application Blocks

ProxyA proxy is a substitute object

It has both the original functionalityAnd 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

Page 7: AOP via Policy Injection  &  Unity Application Blocks

What is PIAB?Part of Enterprise Libraryhttp://entlib.codeplex.comAllows you to intercept calls to an object

MethodProperty Get/Set

Lets you set up a handler pipeline

Page 8: AOP via Policy Injection  &  Unity Application Blocks

Is PIAB an AOP framework?[…] 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, MSDNhttp://msdn.microsoft.com/en-us/library/cc511729.aspx

Page 9: AOP via Policy Injection  &  Unity Application Blocks

How does it work?By default, PIAB uses transparent proxies

Used in remotingTo create a proxy for your class, it must

Either implement a well-known interface; orDerive from MarshalByRefObject

PIAB uses a factory to create objectsJust like Unity!

Page 10: AOP via Policy Injection  &  Unity Application Blocks

Scenariopublic interface IPerson{ int Age { get; set; } string FirstName { get; set; } string LastName { get; set; } string GetInfo();}

public class Person : IPerson{ public Person(int age, string firstName, string lastName) { Age = age; FirstName = firstName; LastName = lastName; } public string GetInfo() { return FirstName + " " + LastName + " is " + Age + " years old."; }}

Page 11: AOP via Policy Injection  &  Unity Application Blocks

Object creation (no aspects yet!)

Concrete typesOnly if object derived from MarshalByRefObjectvar p = PolicyInjection.Create<Person>(…);Will fail otherwise

Interface typesvar p = PolicyInjection.Create<Person, IPerson>( 27, "Dmitri", "Nesteruk");

Can apply to existing objectPerson p = new Person(27, "Dmitri", "Nesteruk");IPerson wp = PolicyInjection.Wrap<IPerson>(p);

Page 12: AOP via Policy Injection  &  Unity Application Blocks

Applying policiesA policy needs to specify

Where it needs to be applied (matching rules)What needs to be done (handler)

EntLib provides several ‘stock’ handlers

ExampleIn each property assignmentCheck that the value is not null

And throw ArgumentException if it is

Typically edited in designer

Page 13: AOP via Policy Injection  &  Unity Application Blocks

Stock handlersPIAB comes pre-packaged with several ‘stock’ handlers

These handlers integrate with other application blocks

Can always write your own

Including a designer

AuthorizationCachingCustomException handling

LoggingPerformanceValidation

Page 14: AOP via Policy Injection  &  Unity Application Blocks

Custom validation policyDemo

Page 15: AOP via Policy Injection  &  Unity Application Blocks

Custom handler creationImplement ICallHandlerImplement Invoke() to

Do your own pre/postprocessingTo invoke next item in chain, usereturn getNext()(input, getNext);

Make sure to tag handler class with[ConfigurationElementType( typeof(CustomCallHandlerData))]Create your custom policy

Warning: sign the assembly; orEdit config manually

ICallHandler is defined in Unity

AB

Page 16: AOP via Policy Injection  &  Unity Application Blocks

Unity InterceptionPart II

Page 17: AOP via Policy Injection  &  Unity Application Blocks

Unity Object ResolutionUnity 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 runtimeThis type can use the functionality of Person and enhance it

Page 18: AOP via Policy Injection  &  Unity Application Blocks

Unity & PIABBoth Unity and PIAB give out objects

Unity resolves dependenciesPIAB creates proxies

Synergy? Absolutely.In Unity, we can decide whether or not a proxy needs to be createdThen we can selectively apply PIABAnd yield an object conforming to an interface

Interoperation between Unity and PIAB is possible, but…

There is an alternative

Page 19: AOP via Policy Injection  &  Unity Application Blocks

Unity Interception ExtensionSince v1.2, Unity has a…

Microsoft.Practices.Unity.InterceptionExtension

Works just like PIAB, butEasy to configure in codeIntegrates with Unity container

ArtefactsCall handler (as per PIAB)Handler attribute – used to decorate elements it affectsUnity extension configuration

Page 20: AOP via Policy Injection  &  Unity Application Blocks

Interceptor implementationDerive from HandlerAttribute

Override CreateHandler()

Make a handler that implements ICallHandler

Define Invoke()Use getNext().Invoke() to call decorated methodAdd code around it as necessary

Add and configure interceptionuc.AddNewExtension<Interception>();uc.Configure<Interception>() .SetInterceptorFor<IPerson>( new TransparentProxyInterceptor());

Page 21: AOP via Policy Injection  &  Unity Application Blocks

Unity InterceptionDemo

Page 22: AOP via Policy Injection  &  Unity Application Blocks

ConclusionsPIAB is useful for ‘stock’ interceptionUnity is useful for general-purpose interceptionUnity is positioned to supersede PIAB for AOPBest for dynamic reconfiguration

Use precompilation for speed

Page 23: AOP via Policy Injection  &  Unity Application Blocks

LinksAOP 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

Page 24: AOP via Policy Injection  &  Unity Application Blocks

albvobqllkutmywmwreerqqmqiqdnwgsswda

vpdvnuxjlsytubnbycthrryuvastadtnzffx

gqhbjjpyowsigiciigsofnfrjfdcfgmkwuyj

vbvemitijxuvwsjuguylbocmykubwswnpxio

kaycqosunkscgxjxjeqphjiacmjzhckvxkak

fecruuxqppkofwgxbvjmbeleewkscvnnogcz

wwfwinehjqlhxuvjomhgsxajzbdnuascnaji

xwinwzjdspnwipcndsrmjhzqjgbwjmezkjva

zouqwdcjcfoxwthvsrhomjynauppuphznsjr

mbzowkinthlikzwmzmfxchomwxbsmxucjxos

hxuetpuxeovlhaypffvaxzxlzulclnqgegmx

ykkkqjnhpijwipddaxzszempclimsugeizom

qprpdwmyqtovmpTHEyENDvzdczxmgqqrhnbjibqipxnhwidohmawcxmghcyriknpndmcxlze

hhsclfsylkjsptdqebkvuxkmkzpgkenafhhr

oxvwkujutneuqfadnedyyyfczcapxyfbrwey

ofavfhzryanzuqrognfpxljyluanrdorvkmf

jynhpcctkxytbfjrnxgczhspceiqgxkpfgrn

lyiiftisbifckcheslwysupdvxbrlqlkizdz

wsawriiumnixrcjndhnwgsfsilhabhlhxmvp

tegknoisgsxvbokecijybedrtpexvrcwuvds

doazttmuiuvubplwcpxnkkvaavbbseefdbfy

ivcjkrgrytjamfvhbfsbzlinaxclrlzivlcb

nudllguyrttuqtlyjlquhaouoptgvlqqrkrqyplzdxnqnqvtfbuhrynkfqithiuwinmloccc


Recommended