240 likes | 425 Views
AOP via Policy Injection & Unity Application Blocks. Dmitri Nesteruk dmitri@activemesa.com 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.
E N D
AOP via Policy Injection & Unity Application Blocks Dmitri Nesteruk dmitri@activemesa.com 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
Part I AOP and Policy Injection
Aspect-Oriented Programming • Separation of functional and non-functional requirements • Without mixing code →
Ways of adding AOP • Post-build assembly modification • PostSharp • Metaprogramming • Boo • Nemerle • Proxies • Spring.net • Castle • Policy Injection AB compilerextension
Proxy • 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
What is PIAB? • Part of Enterprise Libraryhttp://entlib.codeplex.com • Allows you to intercept calls to an object • Method • Property Get/Set • Lets you set up a handler pipeline
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
How does it work? • 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!
Scenario public 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."; } }
Object creation (no aspects yet!) • 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");IPersonwp = PolicyInjection.Wrap<IPerson>(p);
Applying policies • 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
Stock handlers • PIAB comes pre-packaged with several ‘stock’ handlers • These handlers integrate with other application blocks • Can always write your own • Including a designer
Demo Custom validation policy
Custom handler creation ICallHandler is defined in Unity AB • Implement ICallHandler • Implement Invoke() to • Do your own pre/postprocessing • To 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; or • Edit config manually
Part II Unity Interception
Unity Object Resolution • Unity is capable of giving us predefined object instances • IPerson p = uc.Resolve<IPerson>(); • IPersoncan 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 enhance it
Unity & PIAB • 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
Unity Interception Extension • 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
Interceptor implementation • 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());
Demo Unity Interception
Conclusions • 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
Links • AOP frameworks to try • PostSharphttp://postsharp.orgOur 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
albvobqllkutmywmwreerqqmqiqdnwgsswdavpdvnuxjlsytubnbycthrryuvastadtnzffxgqhbjjpyowsigiciigsofnfrjfdcfgmkwuyjvbvemitijxuvwsjuguylbocmykubwswnpxiokaycqosunkscgxjxjeqphjiacmjzhckvxkakfecruuxqppkofwgxbvjmbeleewkscvnnogczwwfwinehjqlhxuvjomhgsxajzbdnuascnajixwinwzjdspnwipcndsrmjhzqjgbwjmezkjvazouqwdcjcfoxwthvsrhomjynauppuphznsjrmbzowkinthlikzwmzmfxchomwxbsmxucjxoshxuetpuxeovlhaypffvaxzxlzulclnqgegmxykkkqjnhpijwipddaxzszempclimsugeizomqprpdwmyqtovmpTHEyENDvzdczxmgqqrhnbjibqipxnhwidohmawcxmghcyriknpndmcxlzehhsclfsylkjsptdqebkvuxkmkzpgkenafhhroxvwkujutneuqfadnedyyyfczcapxyfbrweyofavfhzryanzuqrognfpxljyluanrdorvkmfjynhpcctkxytbfjrnxgczhspceiqgxkpfgrnlyiiftisbifckcheslwysupdvxbrlqlkizdzwsawriiumnixrcjndhnwgsfsilhabhlhxmvptegknoisgsxvbokecijybedrtpexvrcwuvdsdoazttmuiuvubplwcpxnkkvaavbbseefdbfyivcjkrgrytjamfvhbfsbzlinaxclrlzivlcbnudllguyrttuqtlyjlquhaouoptgvlqqrkrqyplzdxnqnqvtfbuhrynkfqithiuwinmlocccalbvobqllkutmywmwreerqqmqiqdnwgsswdavpdvnuxjlsytubnbycthrryuvastadtnzffxgqhbjjpyowsigiciigsofnfrjfdcfgmkwuyjvbvemitijxuvwsjuguylbocmykubwswnpxiokaycqosunkscgxjxjeqphjiacmjzhckvxkakfecruuxqppkofwgxbvjmbeleewkscvnnogczwwfwinehjqlhxuvjomhgsxajzbdnuascnajixwinwzjdspnwipcndsrmjhzqjgbwjmezkjvazouqwdcjcfoxwthvsrhomjynauppuphznsjrmbzowkinthlikzwmzmfxchomwxbsmxucjxoshxuetpuxeovlhaypffvaxzxlzulclnqgegmxykkkqjnhpijwipddaxzszempclimsugeizomqprpdwmyqtovmpTHEyENDvzdczxmgqqrhnbjibqipxnhwidohmawcxmghcyriknpndmcxlzehhsclfsylkjsptdqebkvuxkmkzpgkenafhhroxvwkujutneuqfadnedyyyfczcapxyfbrweyofavfhzryanzuqrognfpxljyluanrdorvkmfjynhpcctkxytbfjrnxgczhspceiqgxkpfgrnlyiiftisbifckcheslwysupdvxbrlqlkizdzwsawriiumnixrcjndhnwgsfsilhabhlhxmvptegknoisgsxvbokecijybedrtpexvrcwuvdsdoazttmuiuvubplwcpxnkkvaavbbseefdbfyivcjkrgrytjamfvhbfsbzlinaxclrlzivlcbnudllguyrttuqtlyjlquhaouoptgvlqqrkrqyplzdxnqnqvtfbuhrynkfqithiuwinmloccc