tag:blogger.com,1999:blog-53227629840105753662024-03-13T07:00:34.833-07:00Nanduri'sKnowledge is power but attitude is everything. Join in my long journey of knowledge search, I promise I will share whatever I have seen, whatever I have learned and whatever I have experienced. This is continuous journey so post your questions and lets solve together.
Welcome to Nanduri’s Knowledge search.Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-5322762984010575366.post-18809800102328270712013-07-15T15:00:00.004-07:002015-01-09T04:48:20.347-08:00Start using Single Page Application Template Visual Studio 2012<br />
What do you need First ?<br />
<br />
<b>Step 0: Pre-Requisites. </b><br />
<br />
1. Proper VS012 installation will install MVC4 - If not install it from MS Download<br />
<br />
2. Download and install ASP.NET and Web Tools 2012.2<br />
<br />
Step 1:<br />
<br />Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-33350784777652631282012-12-17T04:18:00.004-08:002012-12-17T04:18:56.391-08:00Active Directory 123 <br />
<br />
<div style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px; padding: 10px;">
I've been dinking around in the <code style="border: 1px solid rgb(242, 242, 242); font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 1em; line-height: 1.5em; margin: 5px 0px; overflow: auto; padding: 0px;"><a href="http://msdn2.microsoft.com/en-us/system.directoryservices.aspx" style="background: inherit; color: #4f82cb; margin: 0px; padding: 0px; text-decoration: initial;">System.DirectoryServices</a></code> namespace lately trying to update user's in Active Directory. This particular namespace has 2 main component classes: <a href="http://msdn2.microsoft.com/en-us/system.directoryservices.directoryentry.aspx" style="background: inherit; color: #4f82cb; margin: 0px; padding: 0px; text-decoration: initial;"><code style="border: 1px solid rgb(242, 242, 242); font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 1em; line-height: 1.5em; margin: 5px 0px; overflow: auto; padding: 0px;">DirectoryEntry</code></a> and <a href="http://msdn2.microsoft.com/en-us/system.directoryservices.directorysearcher.aspx" style="background: inherit; color: #4f82cb; margin: 0px; padding: 0px; text-decoration: initial;"><code style="border: 1px solid rgb(242, 242, 242); font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 1em; line-height: 1.5em; margin: 5px 0px; overflow: auto; padding: 0px;">DirectorySearcher</code></a>. After a couple of days (hence no posting) I have successfully accomplished the tasks of querying for and updating users. I will share some basic functionality for looking up and verifying users in Active Directory to lay the foundation for those of you that are interested. It might be useful to read up on <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/lightweight_directory_access_protocol_ldap_api.asp" style="background: inherit; color: #4f82cb; margin: 0px; padding: 0px; text-decoration: initial;">LDAP</a> to get a good understanding of what it is and how it works with Active Directory.</div>
<div style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px; padding: 10px;">
<strong style="margin: 0px; padding: 0px;">Setting up the connection</strong><code style="border: 1px solid rgb(242, 242, 242); font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 1em; line-height: 1.5em; margin: 5px 0px; overflow: auto; padding: 0px;"><span style="color: blue; margin: 0px; padding: 0px;">public</span> <span style="color: blue; margin: 0px; padding: 0px;">static</span> <span style="color: teal; margin: 0px; padding: 0px;">DirectoryEntry</span> GetDirectoryEntry()<br style="margin: 0px; padding: 0px;" />{<br style="margin: 0px; padding: 0px;" /><span style="color: teal; margin: 0px; padding: 0px;"> DirectoryEntry</span> de = <span style="color: blue; margin: 0px; padding: 0px;">new</span> <span style="color: teal; margin: 0px; padding: 0px;">DirectoryEntry</span>();<br style="margin: 0px; padding: 0px;" /> de.Path = "<span style="color: maroon; margin: 0px; padding: 0px;">LDAP://OU=Domain,DC=YourDomain,DC=com"</span>;<br style="margin: 0px; padding: 0px;" /> de.AuthenticationType = <span style="color: teal; margin: 0px; padding: 0px;">AuthenticationTypes</span>.Secure;</code></div>
<code style="background-color: #fafafa; border: 1px solid rgb(242, 242, 242); color: #333333; font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 11px; line-height: 16.78333282470703px; margin: 5px 0px; overflow: auto; padding: 0px;"><div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;"> return</span> de;<br style="margin: 0px; padding: 0px;" />}</div>
</code><span style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px;"></span><div style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px; padding: 10px;">
<strong style="margin: 0px; padding: 0px;">Does a User Exist?</strong><br style="margin: 0px; padding: 0px;" />Before you update any user information it is probably a good idea to find out if they actually exist in the Active Directory.</div>
<code style="background-color: #fafafa; border: 1px solid rgb(242, 242, 242); color: #333333; font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 11px; line-height: 16.78333282470703px; margin: 5px 0px; overflow: auto; padding: 0px;"><div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;">public</span> <span style="color: blue; margin: 0px; padding: 0px;">bool</span> UserExists(<span style="color: blue; margin: 0px; padding: 0px;">string</span> username)<br style="margin: 0px; padding: 0px;" />{<br style="margin: 0px; padding: 0px;" /><span style="color: teal; margin: 0px; padding: 0px;"> DirectoryEntry</span> de = GetDirectoryEntry();<br style="margin: 0px; padding: 0px;" /><span style="color: teal; margin: 0px; padding: 0px;"> DirectorySearcher</span> deSearch = <span style="color: blue; margin: 0px; padding: 0px;">new</span> <span style="color: teal; margin: 0px; padding: 0px;">DirectorySearcher</span>();</div>
<div style="padding: 10px;">
deSearch.SearchRoot = de;<br style="margin: 0px; padding: 0px;" /> deSearch.Filter = <span style="color: maroon; margin: 0px; padding: 0px;">"(&(objectClass=user) (cn="</span> + username + <span style="color: maroon; margin: 0px; padding: 0px;">"))"</span>;</div>
<div style="padding: 10px;">
<span style="color: teal; margin: 0px; padding: 0px;"> SearchResultCollection</span> results = deSearch.FindAll();</div>
<div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;">return</span> results.Count > 0;<br style="margin: 0px; padding: 0px;" />}</div>
</code><code style="background-color: #fafafa; border: 1px solid rgb(242, 242, 242); color: #333333; font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 11px; line-height: 16.78333282470703px; margin: 5px 0px; overflow: auto; padding: 0px;"><span style="color: blue; margin: 0px; padding: 0px;"></span><div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;">private</span> <span style="color: teal; margin: 0px; padding: 0px;">String</span> FindName(<span style="color: teal; margin: 0px; padding: 0px;">String</span> userAccount)<br style="margin: 0px; padding: 0px;" />{<br style="margin: 0px; padding: 0px;" /><span style="color: teal; margin: 0px; padding: 0px;"> DirectoryEntry</span> entry = GetDirectoryEntry();<br style="margin: 0px; padding: 0px;" /><span style="color: teal; margin: 0px; padding: 0px;"> String</span> account = userAccount.Replace(<span style="color: maroon; margin: 0px; padding: 0px;">@"Domain\"</span>, <span style="color: maroon; margin: 0px; padding: 0px;">""</span>);</div>
<div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;"> try</span> {<br style="margin: 0px; padding: 0px;" /><span style="color: teal; margin: 0px; padding: 0px;"> DirectorySearcher</span> search = <span style="color: blue; margin: 0px; padding: 0px;">new</span> <span style="color: teal; margin: 0px; padding: 0px;">DirectorySearcher</span>(entry);<br style="margin: 0px; padding: 0px;" /> search.Filter = <span style="color: maroon; margin: 0px; padding: 0px;">"(SAMAccountName="</span> + account + <span style="color: maroon; margin: 0px; padding: 0px;">")"</span>;<br style="margin: 0px; padding: 0px;" /> search.PropertiesToLoad.Add(<span style="color: maroon; margin: 0px; padding: 0px;">"displayName"</span>);</div>
<div style="padding: 10px;">
<span style="color: teal; margin: 0px; padding: 0px;"> SearchResult</span> result = search.FindOne();</div>
<div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;"> if</span> (result != <span style="color: blue; margin: 0px; padding: 0px;">null</span>)<br style="margin: 0px; padding: 0px;" /> {<span style="color: blue; margin: 0px; padding: 0px;"><br style="margin: 0px; padding: 0px;" /> return</span> result.Properties[<span style="color: maroon; margin: 0px; padding: 0px;">"displayname"</span>][0].ToString();<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> else</span> {<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> return</span> <span style="color: maroon; margin: 0px; padding: 0px;">"Unknown User"</span>;<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> catch</span> (<span style="color: teal; margin: 0px; padding: 0px;">Exception</span> ex)<br style="margin: 0px; padding: 0px;" /> {<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> string</span> debug = ex.Message;<br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> return</span> <span style="color: maroon; margin: 0px; padding: 0px;">""</span>;<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" />}</div>
</code><span style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px;"></span><div style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px; padding: 10px;">
The form I created has 2 textboxes (Username & Password) and a submit button. When the button is clicked all the events are fired and if everything checks out the user is updated in Active Directory.</div>
<code style="background-color: #fafafa; border: 1px solid rgb(242, 242, 242); color: #333333; font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 11px; line-height: 16.78333282470703px; margin: 5px 0px; overflow: auto; padding: 0px;"><div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;">private</span> <span style="color: blue; margin: 0px; padding: 0px;">void</span> btnUpdate_Click(<span style="color: blue; margin: 0px; padding: 0px;">object</span> sender, <span style="color: teal; margin: 0px; padding: 0px;">EventArgs</span> e)<br style="margin: 0px; padding: 0px;" />{<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> if</span> (tbUser.Text != <span style="color: maroon; margin: 0px; padding: 0px;">""</span> && tbPass.Text != <span style="color: maroon; margin: 0px; padding: 0px;">""</span>)<br style="margin: 0px; padding: 0px;" /> {<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> string</span> username = tbUser.Text.ToString();<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> string</span> password = tbPass.Text.ToString();</div>
<div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;"> if</span> (UserExists(FindName(username))<br style="margin: 0px; padding: 0px;" /> {<br style="margin: 0px; padding: 0px;" /> ModifyUser(FindName(username), username, password);<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" />}</div>
</code><span style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px;"></span><div style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px; padding: 10px;">
<strong style="margin: 0px; padding: 0px;">Modify User Information</strong><code style="border: 1px solid rgb(242, 242, 242); font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 1em; line-height: 1.5em; margin: 5px 0px; overflow: auto; padding: 0px;"><span style="color: blue; margin: 0px; padding: 0px;">public</span> <span style="color: blue; margin: 0px; padding: 0px;">void</span> ModifyUser(<span style="color: blue; margin: 0px; padding: 0px;">string</span> userDisplayName, <span style="color: blue; margin: 0px; padding: 0px;">string</span> username, <span style="color: blue; margin: 0px; padding: 0px;">string</span>password)<br style="margin: 0px; padding: 0px;" />{<br style="margin: 0px; padding: 0px;" /><span style="color: teal; margin: 0px; padding: 0px;"> DirectoryEntry</span> de = GetDirectoryEntry();<br style="margin: 0px; padding: 0px;" /> de.Username = username;<br style="margin: 0px; padding: 0px;" /> de.Password = password;</code></div>
<code style="background-color: #fafafa; border: 1px solid rgb(242, 242, 242); color: #333333; font-family: 'Courier New', 'Lucida Console', 'courier new', monospace; font-size: 11px; line-height: 16.78333282470703px; margin: 5px 0px; overflow: auto; padding: 0px;"><div style="padding: 10px;">
<span style="color: teal; margin: 0px; padding: 0px;"> DirectorySearcher</span> ds = <span style="color: blue; margin: 0px; padding: 0px;">new</span> <span style="color: teal; margin: 0px; padding: 0px;">DirectorySearcher</span>(de);<br style="margin: 0px; padding: 0px;" /> ds.Filter = (<span style="color: maroon; margin: 0px; padding: 0px;">"(&(objectclass=user)(objectcategory=person)<br style="margin: 0px; padding: 0px;" /> (displayname="</span> + userDisplayName + <span style="color: maroon; margin: 0px; padding: 0px;">"))"</span>);</div>
<div style="padding: 10px;">
ds.SearchScope = <span style="color: teal; margin: 0px; padding: 0px;">SearchScope</span>.Subtree;</div>
<div style="padding: 10px;">
<span style="color: teal; margin: 0px; padding: 0px;"> SearchResult</span> results = ds.FindOne();</div>
<div style="padding: 10px;">
<span style="color: blue; margin: 0px; padding: 0px;"> if</span> (results != <span style="color: blue; margin: 0px; padding: 0px;">null</span>)<br style="margin: 0px; padding: 0px;" /> {<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> try</span> {<br style="margin: 0px; padding: 0px;" /><span style="color: teal; margin: 0px; padding: 0px;"> DirectoryEntry</span> updateEntry = results.GetDirectoryEntry();<br style="margin: 0px; padding: 0px;" /> updateEntry.Properties[<span style="color: maroon; margin: 0px; padding: 0px;">"department"</span>].Value = <span style="color: maroon; margin: 0px; padding: 0px;">"555"</span>;<br style="margin: 0px; padding: 0px;" /> updateEntry.CommitChanges();<br style="margin: 0px; padding: 0px;" /> updateEntry.Close();<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" /><span style="color: blue; margin: 0px; padding: 0px;"> catch</span> (<span style="color: teal; margin: 0px; padding: 0px;">Exception</span> ex)<br style="margin: 0px; padding: 0px;" /> {<br style="margin: 0px; padding: 0px;" /> tbError.Text = ex.ToString();<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" /> }<br style="margin: 0px; padding: 0px;" /><br style="margin: 0px; padding: 0px;" /> de.Close();<br style="margin: 0px; padding: 0px;" />}</div>
</code><span style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px;"></span><div style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px; padding: 10px;">
Good luck! With a little patience you will find that Active Directory is quite fun to work with. Also keep in mind that this is a very basic example. When you master this you can start pulling data from external data sources, formatting, look them up in Active Directory and then update accordingly!Reading </div>
<div style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px; padding: 10px;">
Sourced from : http://www.willasrari.com/blog/query-active-directory-users-using-c/000133.aspx</div>
<div style="background-color: #fafafa; color: #333333; font-family: Verdana, Tahoma, arial, sans-serif; font-size: 11px; line-height: 16.78333282470703px; padding: 10px;">
<br /></div>
Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-81206803276587027042012-01-12T03:38:00.000-08:002012-01-12T03:38:58.253-08:00How to setup PHP on windows<br />
Just follow this video and you are good to go<br />
<br />
<a href="http://www.webassist.com/support/documentation/how-tos/xampp_for_windows.php">http://www.webassist.com/support/documentation/how-tos/xampp_for_windows.php</a>
<br />
<br />Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-60991233521514885522012-01-03T11:45:00.000-08:002012-01-03T11:47:17.725-08:00What is dependency injection<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Published: <span id="ctl00_ArticleInfo1_pubdate">13 Nov 2009</span><br />
By: <span id="ctl00_ArticleInfo1_author"><a href="http://dotnetslackers.com/community/members/asiemer.aspx">Andrew Siemer</a></span> <span id="ctl00_ArticleInfo1_download"></span><br />
<span id="ctl00_ArticleInfo1_desc">In this article we will address the lack of testability that our current application has. We will do this by making our application conform to the dependency injection pattern. This will allow us to push up all of our dependencies which makes our code considerably more testable than it currently is.</span><br />
</span><div class="KonaBody" id="kb"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><br />
</span><br />
<table id="toc"><thead>
<tr> <th><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><br />
</span></th></tr>
</thead> <tbody>
<tr> <td><ul><li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s1-introduction"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">1 Introduction </span></a></li>
<li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s2-what-is-dependency-injection"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">2 What is dependency injection? </span></a></li>
<li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s3-code-that-doesnt-use-dependency-injection"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">3 Code that doesn't use dependency injection </span></a></li>
<li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s4-code-that-does-use-dependency-injection"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">4 Code that does use dependency injection </span></a></li>
<li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s5-refactoring-for-dependency-injection"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">5 Refactoring for dependency injection </span></a></li>
<ul><li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s6-what-needs-to-be-refactored"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">5.1 What needs to be refactored? </span></a></li>
<li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s7-lets-refactor-it"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">5.2 Lets refactor it! </span></a></li>
</ul><li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s8-writing-our-tests"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">6 Writing our tests </span></a></li>
<li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s9-analysis"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">7 Analysis </span></a></li>
<ul><li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s10-pros"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">7.1 Pros </span></a></li>
<li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s11-cons"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">7.2 Cons </span></a></li>
<li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s12-comparison-chart"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">7.3 Comparison Chart </span></a></li>
</ul><li><a href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#s13-summary"><span style="font-family: Verdana, sans-serif; font-size: xx-small;">8 Summary</span></a></li>
</ul></td></tr>
</tbody></table><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s1-introduction"></a> </span><br />
<h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Introduction </span></h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">In the last article we removed the dependency that our front end and middle tier code had on our data access layer. This provided us with an application that has no ties to the data access technology that is used to connect to the surrounding infrastructure. This made the application more flexible in that the data access code and repositories can be swapped out for something new. The codebase however is no more testable than it has been in all of its previous states.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">In this article we will address the lack of testability that our current application has. We will do this by making our application conform to the dependency injection pattern. This will allow us to push up all of our dependencies which makes our code considerably more testable than it currently is.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s2-what-is-dependency-injection"></a> </span><br />
<h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">What is dependency injection? </span></h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow">Dependency Injection</a> as defined by Wikipedia is "the process of supplying an external dependency to a software component. It is a specific form of <a href="http://en.wikipedia.org/wiki/Inversion_of_control" rel="nofollow">inversion of control</a> where the concern being inverted is the process of obtaining the needed dependency."</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">The statement above doesn't clearly describe what this means if your head is not already thinking in this realm. For that reason seeing some code that does not use dependency injection vs. seeing some code that does use this pattern tends to make things much easier to comprehend. </span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s3-code-that-doesnt-use-dependency-injection"></a> </span><br />
<h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Code that doesn't use dependency injection </span></h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Let's take a look at a quick mock up that implements a Product, ProductRepository, and a ProductService. This code will be called from a simple console application that outputs the products to the display. </span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><br />
</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 1: The model </span></h4><div class="syntaxhighlighter " id="highlighter_797282"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_797282" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_797282" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_797282" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">Product </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">int</code> <code class="plain">Id { </code><code class="keyword">get</code><code class="plain">; </code><code class="keyword">set</code><code class="plain">; } </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">string</code> <code class="plain">Name { </code><code class="keyword">get</code><code class="plain">; </code><code class="keyword">set</code><code class="plain">; } </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">string</code> <code class="plain">Description { </code><code class="keyword">get</code><code class="plain">; </code><code class="keyword">set</code><code class="plain">; } </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">double</code> <code class="plain">Price { </code><code class="keyword">get</code><code class="plain">; </code><code class="keyword">set</code><code class="plain">; } </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">ProductRepository </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">Product[] GetProducts() </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">List<Product> result = </code><code class="keyword">new</code> <code class="plain">List<Product>(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">13.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = 1; i <= 10; i++) </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">14.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">15.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 84px !important;"><code class="plain">result.Add(</code><code class="keyword">new</code> <code class="plain">Product() </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">16.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 189px !important;"><code class="plain">{Description = </code><code class="string">"Product Description "</code> <code class="plain">+ i, Id = i, Name = </code><code class="string">"Product "</code> <code class="plain">+ i, Price = i}); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">17.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">18.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">return</code> <code class="plain">result.ToArray(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">19.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">20.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">21.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">ProductService </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">22.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">23.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">Product[] GetProducts(</code><code class="keyword">int</code> <code class="plain">Page, </code><code class="keyword">int</code> <code class="plain">RecordsPerPage) </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">24.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">25.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Product[] result = </code><code class="keyword">new</code> <code class="plain">ProductRepository().GetProducts(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">26.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">return</code> <code class="plain">result.Skip((Page - 1) * RecordsPerPage).Take(RecordsPerPage).ToArray(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">27.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">28.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">We have a simple Product class that has an <code>ID</code>, <code>Name</code>, <code>Description</code>, and <code>Price</code>. Next we have a mocked up ProductRepository that can return a list of Products in a paged format. Now let's see the console code that outputs some products via a call to the ProductService to get a list of Products.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 2: Console app </span></h4><div class="syntaxhighlighter " id="highlighter_922894"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_922894" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_922894" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_922894" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">1.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">static</code> <code class="keyword">void</code> <code class="plain">Main(</code><code class="keyword">string</code><code class="plain">[] args) </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">2.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">3.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">foreach</code> <code class="plain">(Product product </code><code class="keyword">in</code> <code class="keyword">new</code> <code class="plain">ProductService().GetProducts(1,5)) </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">4.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">5.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Console.WriteLine(</code><code class="string">"ID: "</code> <code class="plain">+ product.Id + </code><code class="string">" Name: "</code> <code class="plain">+ product.Name); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">6.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">7.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">Console.ReadLine(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">8.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Now that we can see how this application works from a functional point of view a question comes up: How do we test this? Some might say that we can't test this but that is not technically true. Let's take a quick look at how we test non-injected code. We will start by creating a test for our ProductRepository. Given that this type of class connects to infrastructure in a real scenario this is more of an integration test than it is a unit test. But since this is a demo let's see a test that wraps around the repository.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 3: Product Repository Tests </span></h4><div class="syntaxhighlighter " id="highlighter_240007"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_240007" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_240007" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_240007" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">[TestFixture] </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">class</code> <code class="plain">ProductRepositoryTests </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">[Test] </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">void</code> <code class="plain">GetProducts_ReturnsListOfProducts() </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">ProductRepository productRepository = </code><code class="keyword">new</code> <code class="plain">ProductRepository(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Product[] products = productRepository.GetProducts(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotNull(products); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">As you can see this test simply gets a list of products from the ProductRepository and tests that the returned item is not null. For an integration test this might be acceptable. But this is not a real "unit" test though as this would not test only the code in a repository method but also all of the underlying code that touches the ORM, the database, etc. </span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">Now let's see how we can test the ProductService given that it has a strong dependency on the ProductRepository.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 4: Product Service Tests </span></h4><div class="syntaxhighlighter " id="highlighter_964155"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_964155" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_964155" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_964155" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">[TestFixture] </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">ProductServiceTests </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">[Test] </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">void</code> <code class="plain">GetProducts_NegativePageNumberPassed_ReturnsProducts() </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Product[] products = </code><code class="keyword">new</code> <code class="plain">ProductService().GetProducts(-2, 5); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotNull(products); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsTrue(products.Length == 5); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">[Test] </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">void</code> <code class="plain">GetProducts_UnrealPageNumberPassed_DoesntFail() </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">13.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">14.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Product[] products = </code><code class="keyword">new</code> <code class="plain">ProductService().GetProducts(1000000000, 5); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">15.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotNull(products); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">16.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotNull(products.Length == 0); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">17.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">18.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">The ProductService tests read a bit more like a unit test. In these tests you can see that we are testing the <code>GetProducts </code>method off of the ProductService class. In the first test we are testing the state where a negative page number is passed to the method and we are still expecting the method to return some products. You can see this in that the common form of the class.method name is very clear in documenting the setup.</span><br />
<code><span style="font-family: Verdana, sans-serif; font-size: xx-small;">ClassName.MethodName_StateUnderTest_ExpectedResult</span></code><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">Or</span><br />
<code><span style="font-family: Verdana, sans-serif; font-size: xx-small;">ProductServiceTests.GetProducts_NegativePageNumberPassed_ReturnsProducts</span></code><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">This way when you run the unit tests in some form of a test runner you can easily see what is passing or failing and understand exactly what is going on with little to no additional research.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><img alt="" height="341" src="http://dotnetslackers.com/images/articleimages/three-tiers-to-mvc--hooray--dependency-injection-1.jpg" title="" width="453" /></span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">Now even though this runs and appears on the surface to be in good unit testing form...it really isn't! The reason why these tests are not good is that when we are testing our ProductRepository there is no real way to just test the repository (assuming that it actually touched a database somewhere and didn't actually spin up fake data). In the same vein our ProductService tests are also incapable of testing just the ProductService in that it has a direct dependency on the ProductRepository. This means that when we test the ProductService we are also executing code in the ProductRepository! That totally breaks the definition of a Unit Test...or testing a single unit of code.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s4-code-that-does-use-dependency-injection"></a> </span><br />
<h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Code that does use dependency injection </span></h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Now let's refactor this code a bit so that it follows the dependency injection pattern. To do this we first need to move any dependencies up and out of the code and into either a method as a parameter or into a constructor as a parameter. Either is technically acceptable. The loose rule here is that if more than one method in a class will need access to the dependency then put it in the constructor; otherwise put it in the method signature. We will stick to putting the dependencies into our constructor for a reason that I will discuss later.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">Here is what our current code looks like now after refactoring out the dependencies.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 5: Product Service </span></h4><div class="syntaxhighlighter " id="highlighter_156349"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_156349" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_156349" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_156349" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">ProductService </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">private</code> <code class="plain">ProductRepository _productRepository; </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">ProductService(ProductRepository productRepository) </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">_productRepository = productRepository; </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">Product[] GetProducts(</code><code class="keyword">int</code> <code class="plain">Page, </code><code class="keyword">int</code> <code class="plain">RecordsPerPage) </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Product[] result = _productRepository.GetProducts(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">return</code> <code class="plain">result.Skip((Page - 1) * RecordsPerPage).Take(RecordsPerPage).ToArray(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">13.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Notice that we now have a new private field <code>_productRepository </code>of type ProductRepository and that this field is set through the constructor of the ProductService. Then when we need to use a ProductRepository we can use the <code>_productRepository </code>variable.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">Does this make our code more testable? No...not really. Why? ...you might ask. Although we have injected the dependency you might notice that we are still directly dependent on the class at hand. In this case the ProductService is still directly dependent on the ProductRepository. </span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">The key behind dependency injection is not only that the dependency is injected into the method but that the dependency is also abstracted away. To do this we will create interfaces that represent our current ProductService and ProductRepository. Then we can define the dependency around the interface rather than the type itself by specifying the interface as the type to be injected. Here is how that code looks.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 6: Product Service </span></h4><div class="syntaxhighlighter " id="highlighter_42593"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_42593" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_42593" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_42593" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">ProductService : IProductService </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">private</code> <code class="plain">IProductRepository _productRepository; </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">ProductService(IProductRepository productRepository) </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">_productRepository = productRepository; </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">Product[] GetProducts(</code><code class="keyword">int</code> <code class="plain">Page, </code><code class="keyword">int</code> <code class="plain">RecordsPerPage) </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Product[] result = _productRepository.GetProducts(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">return</code> <code class="plain">result.Skip((Page - 1) * RecordsPerPage).Take(RecordsPerPage).ToArray(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">13.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Now that the ProductService is only dependent on the interface that defines a ProductRepository we no longer need to worry about what is passed in as long as it implements the interface appropriately. This means that our code is a bit more testable now. Let's see how this changes our tests.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 7: Product Service Tests </span></h4><div class="syntaxhighlighter " id="highlighter_749106"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_749106" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_749106" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_749106" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">[TestFixture] </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">ProductServiceTests </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">[Test] </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">void</code> <code class="plain">GetProducts_NegativePageNumberPassed_ReturnsProducts() </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">MockRepository mocks = </code><code class="keyword">new</code> <code class="plain">MockRepository(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">List<Product> testData = </code><code class="keyword">new</code> <code class="plain">List<Product>(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = 0; i < 10; i++) </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 84px !important;"><code class="plain">testData.Add(</code><code class="keyword">new</code> <code class="plain">Product() { Description = </code><code class="string">"Product Description"</code><code class="plain">, Id = i, Name = </code><code class="string">"Product Name"</code><code class="plain">, Price = 12.00 }); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">13.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">IProductRepository _mockRepository = mocks.StrictMock<IProductRepository>(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">14.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Expect.Call(_mockRepository.GetProducts()).Return(testData.ToArray()); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">15.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">mocks.ReplayAll(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">16.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Product[] products = </code><code class="keyword">new</code> <code class="plain">ProductService(_mockRepository).GetProducts(-2, 5); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">17.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotNull(products); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">18.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsTrue(products.Length == 5); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">19.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">20.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">[Test] </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">21.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">void</code> <code class="plain">GetProducts_UnrealPageNumberPassed_DoesntFail() </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">22.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">23.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">MockRepository mocks = </code><code class="keyword">new</code> <code class="plain">MockRepository(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">24.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">List<Product> testData = </code><code class="keyword">new</code> <code class="plain">List<Product>(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">25.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = 0; i < 10; i++) </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">26.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">27.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 84px !important;"><code class="plain">testData.Add(</code><code class="keyword">new</code> <code class="plain">Product() { Description = </code><code class="string">"Product Description"</code><code class="plain">, Id = i, Name = </code><code class="string">"Product Name"</code><code class="plain">, Price = 12.00 }); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">28.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">29.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">IProductRepository _mockRepository = mocks.StrictMock<IProductRepository>(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">30.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Expect.Call(_mockRepository.GetProducts()).Return(testData.ToArray()); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">31.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">mocks.ReplayAll(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">32.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Product[] products = </code><code class="keyword">new</code> <code class="plain">ProductService(_mockRepository).GetProducts(1000000000, 5); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">33.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotNull(products); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">34.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotNull(products.Length == 0); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">35.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">36.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">You may have noticed in our new test code that we are no longer testing anything other than the ProductService. But since the ProductService does have a dependency on an IProductRepository we have to do something to provide it with a working implementation of IProductRepository. To do this we can use a tool such as <a href="http://www.ayende.com/projects/rhino-mocks.aspx" rel="nofollow">RhinoMocks</a> (or <a href="http://www.typemock.com/" rel="nofollow">TypeMock</a>) to create a mocked version of our IProductRepository. RhinoMocks allows you to dynamically mock out an object and then tell the framework to not only expect a call into the mocked object but also what that expected call should return.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">As you can see in the above code we have had to do a bit more work to set up a test. But as expected we have not done anything more than what our tests would require. Most importantly we are not touching any code outside the bounds of what we want to test directly. In order to create a mock you generally need three things, the mocking framework reference, to define what call expected, and to define what is returned by the call. Obviously you can do more as needed but this is generally the minimum requirement. Once you have these things in place you can tell RhinoMocks to get ready for use by calling <code>ReplayAll() </code>(in RhinoMocks's case any ways).</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s5-refactoring-for-dependency-injection"></a> </span><br />
<h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Refactoring for dependency injection </span></h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">How does all of this apply to our Knowledge Exchange project? Let's identify where we have issues first. Then we can address those issues one at a time until we have an entirely testable set up.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s6-what-needs-to-be-refactored"></a> </span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">What needs to be refactored? </span></h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">There is a big difference between our PostRepository in our project and the ProductRepository we used when discussing Dependency Injection above. Primarily that difference comes in that our PostRepository actually does communicate with the infrastructure using our ORM choice LINQ to SQL. We can fix this like any other dependency by pushing it up and having the Connection injected.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">The PostService object is very similar to our ProductService in that it's only dependency currently is on the PostRepository. For this reason we will need to push up the PostRepository dependency which means that we will also need to push up the Connection dependency a bit further too.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">The one other major bit of code that we really should have under test is our controllers. In this case we added our logic to our HomeController that comes with the MVC framework. In order to test the Index method of our HomeController we need to somehow push up our dependency on our PostService. This, more than the other two areas, will take quite a bit more to make happen. One thing at a time though!</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s7-lets-refactor-it"></a> </span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Lets refactor it! </span></h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">In order for us to refactor our PostRepository we need to push the Connection class up and out of the PostRepository. In order to do that we will need first create an interface that represents our Connection class.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 8: IConnection.cs </span></h4><div class="syntaxhighlighter " id="highlighter_579999"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_579999" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_579999" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_579999" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">1.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">interface</code> <code class="plain">IConnection </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">2.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">3.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">KEDataContext GetContext(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">4.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Once that is done we can then add a new parameter to our PostRepository's contructor.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 9: PostRepository.cs </span></h4><div class="syntaxhighlighter " id="highlighter_565234"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_565234" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_565234" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_565234" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">PostRepository </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">private</code> <code class="plain">IConnection _connection; </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">PostRepository(IConnection connection) </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">_connection = connection; </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">List<Domain.Post> GetAllPosts() </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">List<Post> result = </code><code class="keyword">new</code> <code class="plain">List<Post>(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">using</code><code class="plain">(KEDataContext dc = _connection.GetContext()) </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">13.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 84px !important;"><code class="plain">result = dc.Posts.OrderByDescending(p => p.CreateDate).ToList(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">14.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">15.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">return</code> <code class="plain">Mapper.Map<List<DataAccess.Post>, List<Domain.Post>>(result); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">16.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">17.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Once this refactoring is complete we can then look to refactor our PostService. In this class we need to push up our PostRepository class to our PostService's constructor.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 10: PostService.cs </span></h4><div class="syntaxhighlighter " id="highlighter_757719"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_757719" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_757719" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_757719" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">PostService </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">private</code> <code class="plain">IPostRepository _postRepository; </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">PostService(IPostRepository postRepository) </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">_postRepository = postRepository; </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="plain">List<Domain.Post> GetAllPosts() </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">return</code> <code class="plain">_postRepository.GetAllPosts(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">From here we need to address our controller. It should also have its dependencies injected as well so that we can test it in the same way that we can now test our PostRepository and PostService. Setting up injection for a controller is no different than adding injection to any other class. The only problem though is that we are not directly in charge of loading and controlling the Controllers - the framework is. In order to apply injection to a Controller we will have to jump through a few hoops that we will cover in the next article. </span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">For now we will modify our HomeController so that it can take care of our dependencies. While this means that we won't be able to test our HomeController (which means that the refactoring is not totally complete) we will be able to run our application and write some tests for the other parts of our code. We will finish this round of refactoring in the next chapter.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s8-writing-our-tests"></a> </span><br />
<h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Writing our tests </span></h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">We can start writing our tests around the PostRepository. Keep in mind that this is not really a UnitTest as much as it is an IntegrationTest. There really isn't anything in the PostRepository that can be unit tested at this particular time. And that is ok. Here is the integration test which actually goes to the database and fetches some data. In the test we simply verify that some data came back.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 11: PostRepositoryTests.cs </span></h4><div class="syntaxhighlighter " id="highlighter_957023"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_957023" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_957023" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_957023" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">[TestFixture] </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">PostRepositoryTests </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">[Test] </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">void</code> <code class="plain">GetAllPosts_ReturnsPosts() </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">AutoMapperBootStrapper.Initialize(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">PostRepository _postRepository = </code><code class="keyword">new</code> <code class="plain">PostRepository(</code><code class="keyword">new</code> <code class="plain">Connection()); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">var results = _postRepository.GetAllPosts(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotEmpty(results); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Notice that in the beginning of our test we have to run the AutoMapperBootStrapper to get the mapping functionality to work in our repository. Next we create an instance of our PostRepository which takes an instance of Connection. Then we call the <code>GetAllPosts() </code>method which returns a list of Posts into our results variable. Then we can run a test to check that the results list is not empty. Something not seen here and generally not seen in a standard unit test is that we also had to add an app.config file with our connection string. Being an integration test this test does reach out and touch our database.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">Next we can write a test for our PostService. This class is a little different to test in that it has a dependency on the PostRepository (or IPostRepository) but it is not much different from what was covered in our initial conversation. In this test you will notice that we initialize a MockRepository object first thing. From there we generate a list of Posts for our expected result. Then we spin up a new instance of IPostRepository using the RhinoMocks framework. Next we set up the expected call and return values. Then we are ready to initialize an instance of our IPostService which we pass in the mocked PostRepository. Finally we tell the RhinoMocks framework to get ready for our tests to call into it by calling the <code>ReplayAll() </code>method. Now we are ready to start our test. We set up a new list of Domain.Posts to hold our testResult which we populate by making a call to our <code>PostService.GetAllPosts() </code>method. If all works well we should have a list of Domain.Post items that we can test against.</span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Listing 12: PostServiceTests.cs </span></h4><div class="syntaxhighlighter " id="highlighter_88121"><div class="bar "><div class="toolbar"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a class="item viewSource" commandname="viewSource" highlighterid="highlighter_88121" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#viewSource" style="height: 16px; width: 16px;" title="view source">view source</a><a class="item printSource" commandname="printSource" highlighterid="highlighter_88121" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#printSource" style="height: 16px; width: 16px;" title="print">print</a><a class="item about" commandname="about" highlighterid="highlighter_88121" href="http://dotnetslackers.com/articles/aspnet/Building-a-StackOverflow-inspired-Knowledge-Exchange-Three-Tiers-to-MVC-Hooray-Dependency-Injection.aspx#about" style="height: 16px; width: 16px;" title="?">?</a></span></div></div><div class="lines"><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">01.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">[TestFixture] </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">02.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">PostServiceTests </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">03.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">04.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">[Test] </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">05.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="keyword">public</code> <code class="keyword">void</code> <code class="plain">GetsAllPosts_GetsPosts() </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">06.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">{ </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">07.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">MockRepository mocks = </code><code class="keyword">new</code> <code class="plain">MockRepository(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">08.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">List<AndrewSiemer.KnowledgeExchange.Domain.Post> posts = </code><code class="keyword">new</code> <code class="plain">List<AndrewSiemer.KnowledgeExchange.Domain.Post>(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">09.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="keyword">for</code> <code class="plain">(</code><code class="keyword">int</code> <code class="plain">i = 0; i < 10; i++) { posts.Add(</code><code class="keyword">new</code> <code class="plain">AndrewSiemer.KnowledgeExchange.Domain.Post() { Body = </code><code class="string">"Post Body"</code><code class="plain">, CreateDate = DateTime.Now, LastUpdated = DateTime.Now, PostID = i + 1, ProfileID = 1, Title = </code><code class="string">"Post Title"</code> <code class="plain">}); } </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">10.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">IPostRepository _postRepository = mocks.StrictMock<IPostRepository>(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">11.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Expect.Call(_postRepository.GetAllPosts()).Return(posts); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">12.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">IPostService _postService = </code><code class="keyword">new</code> <code class="plain">PostService(_postRepository); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">13.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">mocks.ReplayAll(); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">14.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">List<AndrewSiemer.KnowledgeExchange.Domain.Post> testResult = _postService.GetAllPosts(); </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">15.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 56px !important;"><code class="plain">Assert.IsNotEmpty(testResult); </code></span></span></span></div><div class="line alt2"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">16.</code><span class="content"><code class="spaces"> </code><span class="block" style="margin-left: 28px !important;"><code class="plain">} </code></span></span></span></div><div class="line alt1"><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><code class="number">17.</code><span class="content"><span class="block" style="margin-left: 0px !important;"><code class="plain">}</code></span></span></span></div></div></div><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Keep in mind that these tests are pretty lame! Since this is demo-ware we don't have a whole lot of moving parts in here to test. The PostRepository does reach out to the database so that is a good test. But the PostService is pretty much a pass through set of plumbing for expansion on later. If there were more moving parts in the PostRepository we would also want to create a UnitTest for the PostRepository that doesn't go to the database but instead tests the guts of the repository itself.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s9-analysis"></a> </span><br />
<h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Analysis </span></h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">The mere fact that we now have code that can be tested means that we have made some huge headway. We do have to keep in mind that our HomeController is not yet testable. More than even that we are instantiating all of our dependencies in the HomeController as well which may be even worse. No worries though we will remedy that soon enough. This issue of our HomeController doing what it does though has added an even more painful issue to our system in that our presentation tier now has a dependency on our data access (otherwise the HomeController wouldn't know how to create an instance of the PostRepository). We will fix that in the next article too!</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">Take a look at our dependency graph below. For the most part things are working out just as we would expect them too. Short of the web to dataaccess issue all is going the right direction! Do take note that our DataAccess.Tests and Domain.Tests items have no usage defined as neither have anything that can be tested just yet.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><img alt="" height="380" src="http://dotnetslackers.com/images/articleimages/three-tiers-to-mvc--hooray--dependency-injection-2.jpg" title="" width="550" /></span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s10-pros"></a> </span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Pros </span></h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Once everything can follow along with the Dependency Injection pattern we will be able to have all of our code under test. Using this pattern not only makes our code more testable but also more flexible. Once everything is dependent only on abstractions we can swap in and out whatever we like whenever we like. We will see the use and power of this the further we go.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s11-cons"></a> </span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Cons </span></h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">I have never heard of a con that has popped up due to the simple implementation of Dependency Injection. Taking a dependency on an Inversion of Control container (such as StructureMap which we will introduce in the next article) may provide a performance degradation but just following this pattern by itself won't hurt us!</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s12-comparison-chart"></a> </span><br />
<h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Comparison Chart </span></h4><h4><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Table 1: Comparison Chart </span></h4><table class="box"><thead>
<tr> <th><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Coding Concepts</span></th> <th><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Yes</span></th> <th><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Sorta</span></th> <th><span style="font-family: Verdana, sans-serif; font-size: xx-small;">No</span></th></tr>
</thead> <tbody>
<tr> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Fast/Easy to develop: Can we generate the end product quickly?</span></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td> <td></td> <td></td></tr>
<tr class="alt"> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Testable: Can we write tests around the majority of the projects code?</span></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td> <td></td> <td></td></tr>
<tr> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Flexible for refactoring: Can we easily refactor the code base to add new concepts?</span></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td> <td></td> <td></td></tr>
<tr class="alt"> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Well abstracted: Do the users of your code only know what they need too?</span></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td> <td></td> <td></td></tr>
<tr> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Well encapsulated: Can you change the internals of code without impacting the users of that code?</span></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td> <td></td> <td></td></tr>
<tr class="alt"> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Separation of concerns? Is your code well compartmentalized and only doing what it needs to?</span></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td> <td></td> <td></td></tr>
<tr> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">DRY? Does your code follow the "Don't repeat yourself motto?"</span></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td> <td></td> <td></td></tr>
<tr class="alt"> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">SOLID? Does this code comply with the SOLID principles?</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">S: <a href="http://www.objectmentor.com/resources/articles/srp.pdf" rel="nofollow">Single Responsibility Principle</a> - there should never be more than one reason for a class to change</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">O: <a href="http://www.objectmentor.com/resources/articles/ocp.pdf" rel="nofollow">Open Closed Principle</a> - should be open for extension but closed for modification</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">L: <a href="http://www.objectmentor.com/resources/articles/lsp.pdf" rel="nofollow">Liskov Substitution Principle</a> - functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">I: <a href="http://www.objectmentor.com/resources/articles/isp.pdf" rel="nofollow">Interface Segregation Principle</a> - clients should not be forced to depend upon interfaces that they do not use</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">D: <a href="http://www.objectmentor.com/resources/articles/dip.pdf" rel="nofollow">Dependency Inversion Principle</a> - high level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.</span></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td> <td></td> <td></td></tr>
<tr> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Swappable? Can you swap out an entire layer down the road?</span></td> <td></td> <td></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td></tr>
<tr class="alt"> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Distributable? Can you push complex processing of logical areas to a separate server easily to offload computing cycles and scale?</span></td> <td></td> <td></td> <td><span style="font-family: Verdana, sans-serif; font-size: xx-small;">X</span></td></tr>
</tbody></table><span style="font-family: Verdana, sans-serif; font-size: xx-small;"><a href="http://www.blogger.com/post-edit.g?blogID=5322762984010575366&postID=6099123352151488552" name="s13-summary"></a> </span><br />
<h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">Summary </span></h3><span style="font-family: Verdana, sans-serif; font-size: xx-small;">In this article we have walked through a discussion of how code that does not follow the dependency injection pattern is not as testable as code that does. With an understanding of how dependency injection works and why it is important we were then able to walk through the majority of our code to refactor towards the dependency injection pattern. Once our code was refactored to implement dependency injection we were then able to write some tests around our code. We did find that towards the end of the article there were parts of our code that we do not have total control over and therefore were unable to convert it to a testable format. With these issues identified we were able to discuss some approaches to fix them.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: xx-small;">In the next article we will discuss how we can enhance the power of the Dependency Injection pattern by implementing an Inversion of Control container. We will discuss what an IoC container can be used for and how it works. Then we will look at implementing an Inversion of Control container in our code. We will specifically implement the IoC container StructureMap. Once StructureMap is plugged in and working we will once again refactor our code to take advantage of StructureMap. And with that refactoring completed we can then address the Controller issue that we found in this article by creating a custom ControllerFactory that takes care of locating and returning a custom controller but also takes care of instantiating our dependencies in an automated fashion using StructureMap.</span></div></h4>Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-48045988756066977862011-12-01T21:33:00.000-08:002011-12-01T21:33:31.271-08:00Integrate SVN into VS2010<div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">1<b>. 1. Install below two tools in your PC</b></span></div><div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><span style="font: 7.0pt "Times New Roman";"></span><o:p></o:p></span></div><div class="MsoListParagraph" style="text-align: left;"><br />
</div><div class="MsoListParagraph" style="background-attachment: initial; background-clip: initial; background-color: #ffffe6; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">http://tortoisesvn.tigris.org/<o:p></o:p></span></div><div class="MsoListParagraph" style="background-attachment: initial; background-clip: initial; background-color: #ffffe6; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">http://ankhsvn.open.collab.net/<o:p></o:p></span></div><div class="MsoListParagraph" style="background-attachment: initial; background-clip: initial; background-color: #ffffe6; background-image: initial; background-origin: initial; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><b>2.<span style="font: 7.0pt "Times New Roman";"> </span>2. Open Visual Studio 2010 </b><o:p></o:p></span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><b><br />
</b></span></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">a.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->Goto Tools -> Options -> <o:p></o:p></span></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">b.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->On left hand side select “Source control”<o:p></o:p></span></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">c.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->Select ankhsvn and click OK <o:p></o:p></span></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6VGKo3thyphenhyphen1o_YySRIXUFyrmLZJMUFeJ17tUu2Z9QPXrY5kgVWv1hzO6Hn3X9NoGmMA8UHvfJzPAKPIxXIQmo3LV8NW2eaQH6XKG8aCIElw_hriygPer90HSXYEPSEEwWEnNLXiw2fDTzn/s1600/SVN1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><img border="0" height="211" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6VGKo3thyphenhyphen1o_YySRIXUFyrmLZJMUFeJ17tUu2Z9QPXrY5kgVWv1hzO6Hn3X9NoGmMA8UHvfJzPAKPIxXIQmo3LV8NW2eaQH6XKG8aCIElw_hriygPer90HSXYEPSEEwWEnNLXiw2fDTzn/s320/SVN1.png" width="320" /></span></a></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><o:p> </o:p> </span></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">d. Setup these settings for better usability<o:p></o:p></span></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQNQqJn2QKgqKF43qkb2cjhuLTpcbpolreYmWDI2j9BcBNmJlIg6S64QyV2m1bteo_h-FgrwrsR8VAVk0KWhZTnRQhVjOizDtoavFASTZvw1-fyYDVIIyeqhJjJyAQckyhJPUo-U5y6Tfy/s1600/SVN1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><img border="0" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQNQqJn2QKgqKF43qkb2cjhuLTpcbpolreYmWDI2j9BcBNmJlIg6S64QyV2m1bteo_h-FgrwrsR8VAVk0KWhZTnRQhVjOizDtoavFASTZvw1-fyYDVIIyeqhJjJyAQckyhJPUo-U5y6Tfy/s320/SVN1.png" width="320" /></span></a></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><o:p> </o:p> </span></div><div class="MsoListParagraph" style="margin-left: 1in; text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">e. Click on “Edit” Authentication cache and remove if you see any old SVN settings. To avoid SVN conflicts</span></div><div class="MsoNormal" style="margin-left: 0.75in; text-align: left;"><br />
</div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><b><span class="Apple-style-span" style="font-size: x-small;">3.<span style="font: normal normal normal 7pt/normal 'Times New Roman';"> </span>3. Create a new folder “XXX” in your local box </span></b><span class="Apple-style-span" style="font-size: x-small;"><o:p></o:p></span></span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">4.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->Open the folder and Right click on the folder space <o:p></o:p></span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">5.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->Select “Tortoise SVN” -> Repo browser and type URl as : http:// < Your repository URL in SVN</span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">6.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->Right click on right panel folder and select “CheckOut “<o:p></o:p></span></div><div class="MsoListParagraph" style="text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">7.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->Set checkout directory as you created XXXX<o:p></o:p></span></div><div class="MsoListParagraph" style="text-align: left;"><br />
</div><div class="MsoListParagraph" style="text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><b> 4. Open Project / solutions </b></span></div><div class="MsoListParagraph" style="text-align: left;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">8.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->Goto folder and open the solution file. It will automatically open VS 2010 <o:p></o:p></span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">9.<span style="font: 7.0pt "Times New Roman";"> </span><!--[endif]-->Right click on solution and select “Update solution to latest version” <o:p></o:p></span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">10.<span style="font: 7.0pt "Times New Roman";"> </span>Right click on project and select “update project to latest version”<o:p></o:p></span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><!--[if !supportLists]--><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;">11.<span style="font: normal normal normal 7pt/normal 'Times New Roman';"> </span><!--[endif]-->That’s it you are good to use VS2010 integrated with SVN</span><o:p></o:p></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> </span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> Good Luck</span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"> </span></div><div class="MsoListParagraph" style="text-align: left; text-indent: -0.25in;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif; font-size: x-small;"><br />
</span></div>Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-52836793058544776802011-12-01T04:25:00.000-08:002015-01-09T04:48:41.034-08:00JQuery Tools<strong><span style="color: blue;">Here are the list of tools essential for JQuery designers and web developers. </span></strong><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-family: Calibri;">jQAPI - Alternative jQuery Documentation</span></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<a href="http://jqapi.com/"><span style="color: blue; font-family: Calibri;">http://jqapi.com/</span></a></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-family: Calibri;">jsFiddle</span></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<a href="http://jsfiddle.net/"><span style="color: blue; font-family: Calibri;">http://jsfiddle.net/</span></a></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-family: Calibri;">FireQuery for Firebug. (Another thing FireQuery does is place event handlers on all your DOM objects so you know where it is being called like a click event or such.</span></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<a href="https://addons.mozilla.org/en-US/firefox/addon/firequery/"><span style="color: blue; font-family: Calibri;">https://addons.mozilla.org/en-US/firefox/addon/firequery/</span></a></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-family: Calibri;"> I use it help locate what selector I want to use.</span></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<a href="http://selectorgadget.com/"><span style="color: blue; font-family: Calibri;">http://selectorgadget.com</span></a></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-family: Calibri;">jQuery for Designer (really good screencasts on different jQuery stuff).</span></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<a href="http://jqueryfordesigners.com/"><span style="color: blue; font-family: Calibri;">http://jqueryfordesigners.com/</span></a></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-family: Calibri;">Also more jQuery stuff on his blog:</span></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<a href="http://remysharp.com/"><span style="color: blue; font-family: Calibri;">http://remysharp.com/</span></a></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-family: Calibri;">Another good blog resource is Elijah Manor’s blog.</span></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<a href="http://www.elijahmanor.com/"><span style="color: blue; font-family: Calibri;">http://www.elijahmanor.com/</span></a></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="font-family: Calibri;">He works for the jQuery training co </span><a href="http://appendto.com/"><span style="color: blue; font-family: Calibri;">http://appendto.com/</span></a><span style="font-family: Calibri;"> and speaks at conferences such as MIX.</span></div>
Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-71113359684838060262011-05-04T14:33:00.000-07:002011-05-04T14:33:28.691-07:00How to HTTP POST in C# from a secure Network ( using proxy and credentials )<div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using System.Net </span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">static</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">[] args)</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">//1. Setup Network credentials ( Your NT username and password)</span></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"></span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>System.Net.</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">NetworkCredential</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> Nc = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">NetworkCredential</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">("xxxUser</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">,</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"yyyPassword"</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">);</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">using</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">WebClient</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> client = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">WebClient</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">())</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>client.Headers[</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"User-Agent"</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">] = </span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)"</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> +</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">" (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">;</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="color: green; font-family: Consolas; font-size: 9.5pt;"> //2. Setup Network Proxy server and port </span></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="color: green; font-family: Consolas; font-size: 9.5pt;"></span></span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>client.Proxy = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">WebProxy</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"proxy.xxxxx.com"</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">,8080);</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>client.Proxy.Credentials = Nc;</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: Consolas; font-size: 9.5pt;">//3. Post the request to the URL with data in query string </span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">byte</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">[] arr = client.DownloadData(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;"><a href="http://demo.test.do/?command=getdata&input=abceed">http://demo.test.do/?command=getdata&input=abceed</a></span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">);</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"></span><span style="color: maroon; font-family: Consolas;"> <span style="color: green; font-family: Consolas; font-size: 9.5pt;">//4. Convert Byte[] array into string </span></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas;"><span style="color: green; font-family: Consolas; font-size: 9.5pt;"></span></span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"> txt = System.Text.</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Encoding</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">.GetEncoding(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"utf-8"</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">).GetString(arr);<span style="mso-spacerun: yes;"> </span></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: green; font-family: Consolas; font-size: 9.5pt;">// Write values.</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"--- WebClient result ---"</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">);</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">.WriteLine(txt);</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;">.ReadKey();</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt; mso-layout-grid-align: none;"><span style="color: maroon; font-family: Consolas; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt;"><br />
</div>Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-15781647536837936132011-04-19T15:00:00.000-07:002011-04-22T14:25:32.282-07:00NuGet - Add Library Package ReferencesVisual Studio 2010 has powerfulul plug in ( NuGet ) which installs and integrate Open source tools into your application. Here are the steps <br />
<br />
<span style="color: #cc0000; font-family: Georgia, "Times New Roman", serif; font-size: x-small;"><strong>Step 1 : Install Web Plat form Installer. </strong></span><br />
<br />
<span style="color: #cc0000; font-family: Georgia, "Times New Roman", serif; font-size: x-small;"><strong> </strong></span><a href="http://www.microsoft.com/web/downloads/platform.aspx"><span style="color: #cc0000; font-family: Georgia, "Times New Roman", serif; font-size: x-small;"><strong>http://www.microsoft.com/web/downloads/platform.aspx</strong></span></a><br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 0pt;"><strong><span style="color: #cc0000; font-family: Georgia, "Times New Roman", serif; font-size: x-small; mso-bidi-font-family: Arial;">Step2 : Install NuGet Package Explorer</span><span style="mso-bidi-font-family: Arial;"></span></strong></div><div class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="mso-bidi-font-family: Arial;"><span style="color: #cc0000; font-family: Georgia, "Times New Roman", serif; font-size: x-small;"><strong> </strong></span><a href="http://nuget.codeplex.com/releases" target="_blank"><span style="color: #cc0000; font-family: Georgia, "Times New Roman", serif; font-size: x-small;"><strong>http://nuget.codeplex.com/releases</strong></span></a></span></div><span style="color: #cc0000;"><br />
<strong><span style="font-family: Georgia, "Times New Roman", serif; font-size: x-small;"></span></strong></span><br />
<div class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #cc0000; font-family: Georgia, "Times New Roman", serif; font-size: x-small; mso-bidi-font-family: Arial;"><strong>Step3: Read on How to ues "Add Library Package References"</strong></span><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: #cc0000; font-family: Georgia, "Times New Roman", serif; font-size: x-small; mso-bidi-font-family: Arial;"><a href="http://nuget.codeplex.com/wikipage?title=Finding%20and%20Installing%20a%20Package%20Using%20the%20Add%20Library%20Package%20Reference%20Dialog%20Box" target="_blank"><strong>http://nuget.codeplex.com/wikipage?title=Finding%20and%20Installing%20a%20Package%20Using%20the%20Add%20Library%20Package%20Reference%20Dialog%20Box</strong></a></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt;"><br />
</div>Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-81309068740933573242011-04-18T14:21:00.000-07:002011-04-18T14:21:28.806-07:00Connect to MySQL using ADO.Net<div class="MsoNormal" style="margin: 0in 0in 0pt;"><br />
</div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;"><span style="font-family: Calibri;">1.</span><span style="font: 7pt "Times New Roman";"> </span></span></span><span style="font-family: Calibri;">Install .Net Connector for MySQL </span></div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in;"><a href="http://dev.mysql.com/downloads/connector/net/"><span style="font-family: Calibri;">http://dev.mysql.com/downloads/connector/net/</span></a></div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in;"><br />
</div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;"><span style="font-family: Calibri;">2.</span><span style="font: 7pt "Times New Roman";"> </span></span></span><span style="font-family: Calibri;">Use this Connection String in config file </span></div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";">using <span style="color: blue;">EID.MySqlClient</span>;</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="color: blue; font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";">MySqlConnection</span><span style="font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";"> oMySqlConn = new MySqlConnection();</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";">oMySqlConn.ConnectionString = "Data Source=localhost;" +</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>"Database=mySQLDatabase;" +</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>"User ID=myUsername;" +</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>"Password=myPassword;" +</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>"Command Logging=false";</span></div><div class="MsoNormal" style="margin: 0in 0in 0pt 45.8pt; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: "Courier New"; font-size: 10pt; mso-fareast-font-family: "Times New Roman";">oMySqlConn.Open();</span></div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in;"><br />
</div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;"><span style="font-family: Calibri;">3.</span><span style="font: 7pt "Times New Roman";"> </span></span></span><span style="font-family: Calibri;">Connect to Database via UI</span></div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in;"><br />
</div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in;"><a href="http://dev.mysql.com/doc/refman/5.0/en/connector-net-visual-studio-making-a-connection.html"><span style="font-family: Calibri;">http://dev.mysql.com/doc/refman/5.0/en/connector-net-visual-studio-making-a-connection.html</span></a></div>Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-29494119194056415642011-04-12T08:02:00.000-07:002011-04-12T08:02:02.684-07:00Useful Blog LinksUsing Razor View Engine with ASP.NET MVC3<br />
<br />
<a href="http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx">http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx</a>Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0tag:blogger.com,1999:blog-5322762984010575366.post-41596717467709741642011-04-08T13:27:00.000-07:002011-04-08T13:29:09.108-07:00How to access TFS from Windows Explorer<div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="font-family: "MS Shell Dlg 2", "sans-serif"; font-size: 10pt; mso-fareast-font-family: "MS Shell Dlg 2";"><span style="mso-list: Ignore;">1.<span style="font-family: "Times New Roman";"> </span></span></span><span style="font-family: "MS Shell Dlg 2", "sans-serif"; font-size: 10pt;">Install </span><span lang="EN" style="color: #610000; font-family: "Segoe UI", "sans-serif"; font-size: 18.5pt; mso-ansi-language: EN;">Microsoft Visual Studio Team Explorer 2010 - ISO</span><span style="font-family: "MS Shell Dlg 2", "sans-serif"; font-size: 10pt;"></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt;"><span style="color: black; font-family: "MS Shell Dlg 2", "sans-serif"; font-size: 10pt;"><a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=fe4f9904-0480-4c9d-a264-02fedd78ab38&displaylang=en"><span style="color: blue;">http://www.microsoft.com/downloads/en/details.aspx?FamilyID=fe4f9904-0480-4c9d-a264-02fedd78ab38&displaylang=en</span></a></span></div><div class="MsoNormal" style="margin: 0in 0in 0pt;"><br />
</div><div class="MsoListParagraph" style="margin: 0in 0in 0pt 0.5in; mso-list: l0 level1 lfo1; text-indent: -0.25in;"><span style="color: black; font-family: "MS Shell Dlg 2", "sans-serif"; font-size: 10pt; mso-fareast-font-family: "MS Shell Dlg 2";"><span style="mso-list: Ignore;">2.<span style="font-family: "Times New Roman";"> </span></span></span><span style="color: black; font-family: "MS Shell Dlg 2", "sans-serif"; font-size: 10pt;">Install Team Foundation Power Tools </span></div><div class="MsoNormal" style="margin: 0in 0in 0pt 0.25in;"><br />
</div><div class="MsoNormal" style="margin: 0in 0in 0pt 0.25in;"><a href="http://visualstudiogallery.msdn.microsoft.com/c255a1e4-04ba-4f68-8f4e-cd473d6b971f/"><span style="color: blue; font-family: Calibri;">http://visualstudiogallery.msdn.microsoft.com/c255a1e4-04ba-4f68-8f4e-cd473d6b971f/</span></a><span style="font-family: Calibri;"> or </span><a href="file://cclshared2/infoshared/NiveshG/TFS/software/tfpt.msi"><span style="color: blue; font-family: Calibri;">\\cclshared2\infoshared\NiveshG\TFS\software\tfpt.msi</span></a></div><br />
3. Goto Command Prompt and Navigate to work space local path and then right click, you should see below options<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp0sr6Q30wT8YYMfyXLK-h-YX6M4VUJ9QdepAJmJY7kEITKQr2imvccSO3LD-9nM7v5A1bdFtGgKQpJgOJ_G58vCcC7Vu6GNj_kGz22vRm06w3nJQwM96b_djCbpcm2NmYAozw84d4gKeF/s1600/TFS_Explorer.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="207" r6="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp0sr6Q30wT8YYMfyXLK-h-YX6M4VUJ9QdepAJmJY7kEITKQr2imvccSO3LD-9nM7v5A1bdFtGgKQpJgOJ_G58vCcC7Vu6GNj_kGz22vRm06w3nJQwM96b_djCbpcm2NmYAozw84d4gKeF/s320/TFS_Explorer.png" width="320" /></a></div><br />
Have fun with TFS<br />
<br />
Nandurihttp://www.blogger.com/profile/01420346640222222683noreply@blogger.com0