1 / 24

AOP via Policy Injection & Unity Application Blocks

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.

tuwa
Download Presentation

AOP via Policy Injection & Unity Application Blocks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AOP via Policy Injection & Unity Application Blocks Dmitri Nesteruk dmitri@activemesa.com http://activemesa.com http://spbalt.net

  2. Agenda • AOP and Policy Injection • AOP overview • The world of proxies • Policy Injection AB • Unity interception • Interceptor extension • Custom attributes

  3. Part I AOP and Policy Injection

  4. Aspect-Oriented Programming • Separation of functional and non-functional requirements • Without mixing code →

  5. Ways of adding AOP • Post-build assembly modification • PostSharp • Metaprogramming • Boo • Nemerle • Proxies • Spring.net • Castle • Policy Injection AB compilerextension

  6. 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

  7. 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

  8. 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

  9. 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!

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

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

  12. 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

  13. 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

  14. Demo Custom validation policy

  15. 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 

  16. Part II Unity Interception

  17. 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

  18. 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 

  19. 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

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

  21. Demo Unity Interception

  22. 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 

  23. 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

  24. albvobqllkutmywmwreerqqmqiqdnwgsswdavpdvnuxjlsytubnbycthrryuvastadtnzffxgqhbjjpyowsigiciigsofnfrjfdcfgmkwuyjvbvemitijxuvwsjuguylbocmykubwswnpxiokaycqosunkscgxjxjeqphjiacmjzhckvxkakfecruuxqppkofwgxbvjmbeleewkscvnnogczwwfwinehjqlhxuvjomhgsxajzbdnuascnajixwinwzjdspnwipcndsrmjhzqjgbwjmezkjvazouqwdcjcfoxwthvsrhomjynauppuphznsjrmbzowkinthlikzwmzmfxchomwxbsmxucjxoshxuetpuxeovlhaypffvaxzxlzulclnqgegmxykkkqjnhpijwipddaxzszempclimsugeizomqprpdwmyqtovmpTHEyENDvzdczxmgqqrhnbjibqipxnhwidohmawcxmghcyriknpndmcxlzehhsclfsylkjsptdqebkvuxkmkzpgkenafhhroxvwkujutneuqfadnedyyyfczcapxyfbrweyofavfhzryanzuqrognfpxljyluanrdorvkmfjynhpcctkxytbfjrnxgczhspceiqgxkpfgrnlyiiftisbifckcheslwysupdvxbrlqlkizdzwsawriiumnixrcjndhnwgsfsilhabhlhxmvptegknoisgsxvbokecijybedrtpexvrcwuvdsdoazttmuiuvubplwcpxnkkvaavbbseefdbfyivcjkrgrytjamfvhbfsbzlinaxclrlzivlcbnudllguyrttuqtlyjlquhaouoptgvlqqrkrqyplzdxnqnqvtfbuhrynkfqithiuwinmlocccalbvobqllkutmywmwreerqqmqiqdnwgsswdavpdvnuxjlsytubnbycthrryuvastadtnzffxgqhbjjpyowsigiciigsofnfrjfdcfgmkwuyjvbvemitijxuvwsjuguylbocmykubwswnpxiokaycqosunkscgxjxjeqphjiacmjzhckvxkakfecruuxqppkofwgxbvjmbeleewkscvnnogczwwfwinehjqlhxuvjomhgsxajzbdnuascnajixwinwzjdspnwipcndsrmjhzqjgbwjmezkjvazouqwdcjcfoxwthvsrhomjynauppuphznsjrmbzowkinthlikzwmzmfxchomwxbsmxucjxoshxuetpuxeovlhaypffvaxzxlzulclnqgegmxykkkqjnhpijwipddaxzszempclimsugeizomqprpdwmyqtovmpTHEyENDvzdczxmgqqrhnbjibqipxnhwidohmawcxmghcyriknpndmcxlzehhsclfsylkjsptdqebkvuxkmkzpgkenafhhroxvwkujutneuqfadnedyyyfczcapxyfbrweyofavfhzryanzuqrognfpxljyluanrdorvkmfjynhpcctkxytbfjrnxgczhspceiqgxkpfgrnlyiiftisbifckcheslwysupdvxbrlqlkizdzwsawriiumnixrcjndhnwgsfsilhabhlhxmvptegknoisgsxvbokecijybedrtpexvrcwuvdsdoazttmuiuvubplwcpxnkkvaavbbseefdbfyivcjkrgrytjamfvhbfsbzlinaxclrlzivlcbnudllguyrttuqtlyjlquhaouoptgvlqqrkrqyplzdxnqnqvtfbuhrynkfqithiuwinmloccc

More Related