<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicious.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Rob Reynolds - The Fervent Coder : IOC</title><link>http://devlicious.com/blogs/rob_reynolds/archive/tags/IOC/default.aspx</link><description>Tags: IOC</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>How To Use Windsor to Change a Deployed Application’s Behavior Without Touching a Single Line of Existing Code</title><link>http://devlicious.com/blogs/rob_reynolds/archive/2009/12/03/how-to-use-windsor-to-change-a-deployed-application-s-behavior-without-touching-a-single-line-of-existing-code.aspx</link><pubDate>Fri, 04 Dec 2009 04:56:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:54311</guid><dc:creator>Rob Reynolds</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/rob_reynolds/rsscomments.aspx?PostID=54311</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicious.com/blogs/rob_reynolds/commentapi.aspx?PostID=54311</wfw:comment><comments>http://devlicious.com/blogs/rob_reynolds/archive/2009/12/03/how-to-use-windsor-to-change-a-deployed-application-s-behavior-without-touching-a-single-line-of-existing-code.aspx#comments</comments><description>&lt;p&gt;Good patterns of development really make maintenance even easier. And since we are all doing maintenance after we write our first line of code, we really should keep maintenance concerns in mind at all times. When you do interfaced based development, it allows you to swap out behaviors without changing code that uses the interface. Now can you imagine taking that to the next level?&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Awhile back I was giving a presentation on Inversion of Control and I was asked if it were possible to set a new DLL next to an existing application in production and get that application to use the new DLL using IOC. The thought was that you wouldn&amp;rsquo;t have to worry about breaking existing code. I sat down later and learned that it&amp;rsquo;s not only possible, but actually very easy! It gives you a pretty powerful sword to wield should you ever need it.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;Implementing A New Behavior Without Changing a Line of Deployed Code&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Let&amp;rsquo;s say you are using Windsor Container for your IOC. So you have code in production and need to change some behavior. But you don&amp;rsquo;t have the ability to get to the source code, just the DLLs in production. You need to just make the current application just log a message instead of doing whatever the system did before. This system is a simple HR system called employee.system.&lt;/p&gt;
&lt;p&gt;1. We create a project called employee.system.additional and reference the code that has the interface we need to implement. &lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_30EAEE1D.png"&gt;&lt;img border="0" width="243" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_thumb_5F00_54C39FAA.png" alt="referencing the DLL with the interface" height="234" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="referencing the DLL with the interface" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;2. Then we create our implementation of the interface. This is a new implementation of ICommand.&amp;nbsp; It&amp;#39;s very simple, it just logs a message.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_5FA080F2.png"&gt;&lt;img border="0" width="643" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_thumb_5F00_7FF810E4.png" alt="Command showing a message" height="218" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="Command showing a message" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;3. After we are done, we build the new assembly and take that that DLL and place it in the same folder as the existing code.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_632E7602.png"&gt;&lt;img border="0" width="451" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_thumb_5F00_617DAA2E.png" alt="image" height="242" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;4. Then we crack open the configuration file of the deployed system and make some changes to allow us to use the new DLL&amp;#39;s code (the existing code doesn&amp;#39;t even know of this new assembly).&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_46D10E15.png"&gt;&lt;img border="0" width="644" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_thumb_5F00_12208C0D.png" alt="showing the windsor config change" height="204" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="showing the windsor config change" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;5. Then we run the application. &lt;/p&gt;
&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_2B78697A.png"&gt;&lt;img border="0" width="629" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/rob_5F00_reynolds/image_5F00_thumb_5F00_65873659.png" alt="displaying the new functionality" height="101" style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="displaying the new functionality" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The functionality of the application has now changed completely based on a new DLL and a configuration change. How many lines did we change in the current application? That&amp;rsquo;s right, zip. Zero. None. Big goose egg. &lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;Conclusion&lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Maintenance takes on a whole new meaning when you can do something like this! I&amp;rsquo;m not saying this is something you should do in a normal environment. But just knowing about it gives you options. It&amp;rsquo;s something that should be used in special situations like when you have access to a DLL but don&amp;rsquo;t have access to the source code.&amp;nbsp; This is something you can tuck away and pull out one day when you have a need for something like this.&amp;nbsp; Or similarly you could use something like &lt;a target="_blank" href="http://sebastien.lebreton.free.fr/reflexil/"&gt;Reflexil&lt;/a&gt; to just manipulate IL code and save modified assemblies to disk.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicious.com/aggbug.aspx?PostID=54311" width="1" height="1"&gt;</description><category domain="http://devlicious.com/blogs/rob_reynolds/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicious.com/blogs/rob_reynolds/archive/tags/IOC/default.aspx">IOC</category></item></channel></rss>