{"id":12427,"date":"2015-12-15T14:48:00","date_gmt":"2015-12-15T14:48:00","guid":{"rendered":"https:\/\/viewmyprojects.com\/winwirewp\/?p=12427"},"modified":"2023-11-30T06:39:30","modified_gmt":"2023-11-30T06:39:30","slug":"aop-aspect-oriented-programming","status":"publish","type":"post","link":"https:\/\/viewmyprojects.com\/winwirewp\/blog\/aop-aspect-oriented-programming\/","title":{"rendered":"AOP: Aspect Oriented Programming"},"content":{"rendered":"\n<p>Aspect Oriented Programming is a methodology to separate cross cut code across different modules in a software system. All the cross cut code is moved to a separate module, thus increasing more modularity and bringing in ease of maintenance.<\/p>\n\n\n\n<p class=\"blog-detail-list\"><strong>AOP has a number of benefits:<br><\/strong><br>\u2022 Improves the performance of the application<br>\u2022 Manages Cross Cutting Concerns viz. logging, Execution time spent etc.<br>\u2022 Adheres to DRY (Don\u2019t Repeat Yourself)<\/p>\n\n\n\n<p>For example, for logging the code has to be written in each method, like method execution starts, ends and exception etc. This is nothing but repetition of code in all the methods.<br>Public void Submit ()<br>{<br>Try<br>{<br>Logger.Info(\u201cSubmit method starts\u201d);<br>}<br>Catch<br>{<br>Logger.Info(\u201cNew Exception\u201d);<br>}<br>Finally<br>{<br>Logger.Info(\u201cSubmit method completed\u201d);<br>}<br>}<\/p>\n\n\n\n<p>AOP helps in avoiding this repetition.<\/p>\n\n\n\n<p><strong>Types of AOP:<\/strong><\/p>\n\n\n\n<p>1<strong>.&nbsp;<\/strong>Interceptors<br>2. IL Code Weaving<\/p>\n\n\n\n<p><strong>Interceptors:<\/strong><\/p>\n\n\n\n<p>o Intercepts calls to class methods\\properties<br>o Usually involves an Inversion of Control (IoC) Container<br>o No Post compilation changes to assemblies<\/p>\n\n\n\n<p><strong>IL Code Weaving:<\/strong><br>o Runs after application compilation<br>o Post Process assemblies<\/p>\n\n\n\n<p><strong>Aspect Hook Locations:<\/strong><\/p>\n\n\n\n<p class=\"blog-detail-list\">\u2022 OnStart: Executed immediately prior to method call<br>\u2022 OnExit: Executed immediately after the method call runs<br>\u2022 OnSuccess: Executed immediately after the method call runs without throwing an exception<br>\u2022 OnError: Executed when a method throws exception<\/p>\n\n\n\n<p>In this document, Let us see how the aspect-oriented programming is done using IL Code Weaving.<\/p>\n\n\n\n<p>There are a few tools which allow youto perform IL Code Weaving.<br>1. Post Sharp<br>2. LOOM.NET<br>3. Wicca<\/p>\n\n\n\n<p><strong>Below implementation is done using Post Sharp.<\/strong><\/p>\n\n\n\n<p>Creating an Aspect:<br>\u2013 Implement a Base Class<br>\u2013 Attach the aspect<\/p>\n\n\n\n<p>1. Create a Console Application for demonstrating the AOP using IL<\/p>\n\n\n\n<p><strong>Code Weaving.<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"202\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/aop1.webp\" alt=\"\" class=\"wp-image-18691\"\/><\/figure><\/div>\n\n\n<p>Output when the above code is executed:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"32\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/aop2.webp\" alt=\"\" class=\"wp-image-18693\"\/><\/figure><\/div>\n\n\n<p>2. Create another class called \u2018LoggingAspect\u2019 for logging the execution of the application. Add Post Sharp reference for this.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"641\" height=\"552\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code-2.webp\" alt=\"\" class=\"wp-image-18694\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code-2.webp 641w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code-2-300x258.webp 300w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/figure><\/div>\n\n\n<p>Inherit this class from the \u2018OnMethodBoundaryAspect\u2019 and add the attribute \u2018Serializable\u2019 to it.<br>3. Below is the output when the code is executed now. There\u2019s no change in the output. Still AOP is not working as expected.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"674\" height=\"74\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code3-1.webp\" alt=\"\" class=\"wp-image-18695\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code3-1.webp 674w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code3-1-300x33.webp 300w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/figure><\/div>\n\n\n<p>4. For the AOP to work as expected, the LoggingAspect needs to be attached to the underlying code.<\/p>\n\n\n\n<p>5. Go to the MyType class and add the \u2018LoggingAspect\u2019 as an attribute to the method that needs to use the Aspect as shown below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"857\" height=\"269\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code4.webp\" alt=\"\" class=\"wp-image-18696\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code4.webp 857w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code4-300x94.webp 300w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code4-768x241.webp 768w\" sizes=\"auto, (max-width: 857px) 100vw, 857px\" \/><\/figure><\/div>\n\n\n<p>6. Run the application and observer the output. Logging is applied to the method.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"674\" height=\"79\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code5.webp\" alt=\"\" class=\"wp-image-18697\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code5.webp 674w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code5-300x35.webp 300w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/figure><\/div>\n\n\n<p>7. In case, of any exception, the onException has to be logged. Add some code which throws an exception in the MyType class as below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"824\" height=\"292\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code6.webp\" alt=\"\" class=\"wp-image-18698\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code6.webp 824w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code6-300x106.webp 300w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code6-768x272.webp 768w\" sizes=\"auto, (max-width: 824px) 100vw, 824px\" \/><\/figure><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"675\" height=\"91\" src=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code7.webp\" alt=\"\" class=\"wp-image-18699\" srcset=\"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code7.webp 675w, https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/code7-300x40.webp 300w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/figure><\/div>\n\n\n<p>The setting up of the aspect as an attribute can be done<br>\u2013 on a set of methods, by using the \u2018LoggingAspect\u2019 attribute on the required methods as shown above.<br>\u2013 on the whole application with the convention based approach. To apply the aspect on all the methods, update the assembly reference in the AssemblyInfo.cs of the Application as shown below<\/p>\n\n\n\n<p>[assembly: LoggingAspect(AttributeTargetTypes = \u201cAOP.*\u201d)]<\/p>\n\n\n\n<p>To attach the aspect only to the methods and not to the properties and constructors, we need to add the following attribute to the LoggingAspect.<br>[MulticastAttributeUsage (MulticastTargets.Method, TargetMemberAttributes=MulticastAttributes.Instance)]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aspect Oriented Programming is a methodology to separate cross cut code across different modules in a software system. All the cross cut code is moved to a separate module, thus increasing more modularity and bringing in ease of maintenance. AOP has a number of benefits:\u2022 Improves the performance of the application\u2022 Manages Cross Cutting Concerns&hellip; <a class=\"more-link\" href=\"https:\/\/viewmyprojects.com\/winwirewp\/blog\/aop-aspect-oriented-programming\/\">Continue reading <span class=\"screen-reader-text\">AOP: Aspect Oriented Programming<\/span><\/a><\/p>\n","protected":false},"author":31,"featured_media":16813,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-12427","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","entry"],"acf":[],"featured_image_src":"https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic.webp","author_info":{"display_name":"Sree Navya","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/sreenavya\/"},"views":3620,"uagb_featured_image_src":{"full":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic.webp",800,440,false],"thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic-150x150.webp",150,150,true],"medium":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic-300x165.webp",300,165,true],"medium_large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic-768x422.webp",750,412,true],"large":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic.webp",750,413,false],"1536x1536":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic.webp",800,440,false],"2048x2048":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic.webp",800,440,false],"post-thumbnail":["https:\/\/viewmyprojects.com\/winwirewp\/wp-content\/uploads\/2023\/11\/AOP-Aspect-Oriented-Programming-graphic.webp",800,440,false]},"uagb_author_info":{"display_name":"Sree Navya","author_link":"https:\/\/viewmyprojects.com\/winwirewp\/author\/sreenavya\/"},"uagb_comment_info":0,"uagb_excerpt":"Aspect Oriented Programming is a methodology to separate cross cut code across different modules in a software system. All the cross cut code is moved to a separate module, thus increasing more modularity and bringing in ease of maintenance. AOP has a number of benefits:\u2022 Improves the performance of the application\u2022 Manages Cross Cutting Concerns&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/comments?post=12427"}],"version-history":[{"count":2,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12427\/revisions"}],"predecessor-version":[{"id":18700,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/posts\/12427\/revisions\/18700"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media\/16813"}],"wp:attachment":[{"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/media?parent=12427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/categories?post=12427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/viewmyprojects.com\/winwirewp\/wp-json\/wp\/v2\/tags?post=12427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}