<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5616951549680673067</id><updated>2012-02-16T22:34:59.882-05:00</updated><category term='Patterns'/><category term='MVC'/><category term='ASP.Net'/><category term='.Net'/><title type='text'>Refactoring... Me</title><subtitle type='html'>Mike Meisinger's Blog about coding and software development</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mmeisinger.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5616951549680673067/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://mmeisinger.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Meisinger</name><uri>http://www.blogger.com/profile/15728383238154516952</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5616951549680673067.post-9088487126420797980</id><published>2008-03-25T21:16:00.024-04:00</published><updated>2008-03-27T12:10:51.808-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><title type='text'>Inversion of Control and Dependency Injection</title><content type='html'>&lt;p&gt;&lt;span style="font-family:arial;"&gt;MVC has not only brought about a change in how we think about designing and building web applications on the .Net framework but it has also reminded most of us about the Inversion of Control (IoC) and Dependency Injection patterns. While most of us have used these patterns before (without even knowing about it), I think that it is very important to understand what these patterns are, how to use them, and why they are important not only with MVC but in everyday "coding" life. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;IoC and DI are defined and described at length in an article written back in 2004 by &lt;a href="http://martinfowler.com/"&gt;Martin Fowler&lt;/a&gt; named &lt;a href="http://martinfowler.com/articles/injection.html"&gt;"Inversion of Control Containers and the Dependency Injection pattern"&lt;/a&gt; that I highly recommend everyone read. I am not going to go into great details about these patterns but... &lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-family:arial;"&gt;IoC means that an object gets other objects that it relies on through an outside source or framework&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:arial;"&gt;Dependency Injection means that an object gets other objects that it relies on through it's constructor or set properties &lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;It is safe to say that these are the same but just remember that there is a difference. I have also found that when people or articles have talked about IoC they have been really talking about Dependency Injection. So enough about that... what does it look like? &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;Let's start with a simple class which relies on a Data Access class (named "MyDataAccess") to connect to some database...&lt;/span&gt;&lt;/p&gt;&lt;span style="font-family:arial;font-size:85%;color:#333399;"&gt;&lt;pre&gt;public class MyObject&lt;br /&gt;{&lt;br /&gt;  private MyDataAccess dataAccess;&lt;br /&gt;  public MyObject()&lt;br /&gt;  {&lt;br /&gt;    this.dataAccess = new MyDataAccess();&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;Imagine now that the "MyDataAccess" implements an interface named "IDataAccess" and we change the code to reflect that... &lt;/span&gt;&lt;/p&gt;&lt;span style="font-family:arial;font-size:85%;color:#333399;"&gt;&lt;pre&gt;public class MyObject&lt;br /&gt;{&lt;br /&gt;  private IDataAccess dataAccess;&lt;br /&gt;  public MyObject()&lt;br /&gt;  {&lt;br /&gt;    this.dataAccess = (IDataAccess)new MyDataAccess();&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;But now if our example changes one more time... and this time we add yet another class which implements the "IDataAccess" interface, we would have to add more logic or code to determine which concrete class should be created...&lt;/p&gt;&lt;span style="font-family:arial;font-size:85%;color:#333399;"&gt;&lt;pre&gt;public class MyObject&lt;br /&gt;{&lt;br /&gt;  private IDataAccess dataAccess;&lt;br /&gt;  public MyObject()&lt;br /&gt;  {&lt;br /&gt;    int dataAccessType = (int)ConfigurationManager.AppSettings["daType"];&lt;br /&gt;    if (dataAccessType.Equals(1))&lt;br /&gt;    {&lt;br /&gt;      this.dataAccess = (IDataAccess)new MyDataAccess();&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;      this.dataAccess = (IDataAccess)new YourDataAccess();&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;Clearly this is going to be a problem each time we want add a new class which implements the "IDataAccess" interface. Now expand this out to several classes which depend upon an "IDataAccess" object and you start to see the overhead with maintaining that code. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;Enter the Dependency Injection pattern where we pass in an "IDataAccess" object as a parameter to the constructor. This then moves the responsibility (or control) of creating the "IDataAccess" object outside of our class or outside of it's scope. &lt;/span&gt;&lt;/p&gt;&lt;span style="font-family:arial;font-size:85%;color:#333399;"&gt;&lt;pre&gt;public class MyObject&lt;br /&gt;{&lt;br /&gt;  private IDataAccess dataAccess;&lt;br /&gt;  public MyObject(IDataAccess dataAccess)&lt;br /&gt;  {&lt;br /&gt;    this.dataAccess = dataAccess;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;So now any time someone creates an instance of this class they can clearly see that it needs or depends upon an "IDataAccess" object. We have also increased the test-ability of our class by being able to pass in stubs or "mock" objects. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:arial;"&gt;In my next post I will cover more of the benefits and show it works amd how it is used in Testing, MVC and LINQ frameworks. &lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmmeisinger.blogspot.com%2f2008%2f03%2finversion-of-control-and-dependency.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmmeisinger.blogspot.com%2f2008%2f03%2finversion-of-control-and-dependency.html" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5616951549680673067-9088487126420797980?l=mmeisinger.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmeisinger.blogspot.com/feeds/9088487126420797980/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5616951549680673067&amp;postID=9088487126420797980' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5616951549680673067/posts/default/9088487126420797980'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5616951549680673067/posts/default/9088487126420797980'/><link rel='alternate' type='text/html' href='http://mmeisinger.blogspot.com/2008/03/inversion-of-control-and-dependency.html' title='Inversion of Control and Dependency Injection'/><author><name>Meisinger</name><uri>http://www.blogger.com/profile/15728383238154516952</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5616951549680673067.post-4523397724893793806</id><published>2008-03-21T20:46:00.007-04:00</published><updated>2008-03-21T21:33:18.974-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='MVC'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.Net'/><title type='text'>ASP.Net MVC Source Code Available... our first test</title><content type='html'>&lt;span style="font-family:arial;"&gt;&lt;a href="http://weblogs.asp.net/scottgu/"&gt;Scott Guthrie&lt;/a&gt; recently posted an article about the &lt;a href="http://weblogs.asp.net/scottgu/archive/2008/03/21/asp-net-mvc-source-code-now-available.aspx"&gt;ASP.Net &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_0"&gt;MVC&lt;/span&gt; Source Code &lt;/a&gt;becoming &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_1"&gt;available&lt;/span&gt; from the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_2"&gt;CodePlex&lt;/span&gt; website.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;I think that this a huge step in the right direction. I would even go as far as saying that this "move" was intentional. If you take a minute and think about what this means... you will see what I am talking about.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;With the source code to the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_3"&gt;MVC&lt;/span&gt; library being made available... it is almost like we as developers are being tested. Will we take the source code and make it our own? Will we report back defects and enhancements back to Microsoft to improve this feature? Will we open it up and try to break it and find flaws in the underlying code?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;My point being that if we take the right approach as a community and as a team then not only will we (the end users of these features) become more &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_4"&gt;familiar&lt;/span&gt; and confident with these bits but our input and feedback will find a larger and more important audience in the long run.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;Is that to say that when I (a simple developer in the .Net world) find an issue with the way that they (the developers at Microsoft) implemented some piece of code expect it to be corrected? No. Is that to say that when I feel like there is a feature or method that can make my life easier it will be included in the final release? No.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;What it does mean is that I am given a chance to make the community, feature, and the platform as a whole better. Sure... I am only one voice. If my voice is included along with others... it becomes easier to hear. If I am given the chance to work with and validate the feature as a "real world developer" then when the feature or functionality is incorporated... &lt;strong&gt;we&lt;/strong&gt; are the ones to blame if something doesn't do what the rest of developers feel like it should.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;All and all... I feel like this is a tremendous opportunity that is being given. While this would not be considered "open source" by any stretch of the imagination, I (little old me... a simple developer) am being given the chance... to change... Microsoft and the .Net language.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;Take a minute and think about what that means to you...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;Do you want to be a part of the community or simply consume it?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmmeisinger.blogspot.com%2f2008%2f03%2fscott-guthrie-recently-posted-article.html"&gt;&lt;br /&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmmeisinger.blogspot.com%2f2008%2f03%2fscott-guthrie-recently-posted-article.html" border="0" /&gt;&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5616951549680673067-4523397724893793806?l=mmeisinger.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mmeisinger.blogspot.com/feeds/4523397724893793806/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5616951549680673067&amp;postID=4523397724893793806' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5616951549680673067/posts/default/4523397724893793806'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5616951549680673067/posts/default/4523397724893793806'/><link rel='alternate' type='text/html' href='http://mmeisinger.blogspot.com/2008/03/scott-guthrie-recently-posted-article.html' title='ASP.Net MVC Source Code Available... our first test'/><author><name>Meisinger</name><uri>http://www.blogger.com/profile/15728383238154516952</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
