<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Gideon Dsouza - csharp</title><link>http://gideondsouza.com:80/Tags/csharp</link><description>Gideon Dsouza - csharp</description><item><title>a superfast way to search for prime numbers [sieve of eratosthenes]</title><link>http://gideondsouza.com:80/blog/a-superfast-way-to-search-for-prime-numbers</link><description>&lt;p&gt;I've been dabbling a little lately on &lt;em&gt;pure&lt;/em&gt; computer science stuff like computational complexity, algorithm design and math. (And interestingly I failed high school math quite badly once)&lt;/p&gt;

&lt;p&gt;Project Euler has been an interesting learning/practice ground : &lt;a href="http://projecteuler.net/about"&gt;http://projecteuler.net/about&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The problems here involve some math, but also deal with time complexity. If done wrong, some of the solutions to their problems could take days or longer (maybe years) to complete.&lt;/p&gt;

&lt;p&gt;Today at &lt;a href="http://projecteuler.net/problem=10"&gt;problem 10&lt;/a&gt; this was the case:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Find the sum of all the primes below two million.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A normal &lt;em&gt;brute-force&lt;/em&gt; method just took way too long. On a relatively fast machine using the following method for checking for primes took nearly 22 minutes to run:&lt;/p&gt;

&lt;script src="https://gist.github.com/1978856.js"&gt; &lt;/script&gt;

&lt;p&gt;&lt;strong&gt;Enter-&lt;/strong&gt; Sieve of Eratosthenes : An interestingly fast way to find primes. &lt;/p&gt;

&lt;p&gt;You make a list of all your target numbers, in our case 1 to 2 million, then remove (or filter) all the multiples of numbers starting with two. You keep going removing multiples and your list ends up being only &lt;strong&gt;prime numbers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's a visualization from the &lt;a href="http://en.wikipedia.org/wiki/Sieve_of_eratosthenes"&gt;wikipedia article&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/Sieve_of_Eratosthenes_animation.gif" alt="Visualization of Sieve of Eratosthenes"&gt;&lt;/p&gt;

&lt;p&gt;Here is a snippet of how I implemented it for the problem. See my whole problem solved &lt;a href="https://github.com/gideondsouza/ProjectEuler_CSharp/blob/master/ProjectEuler/Problems/Problem10.cs"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;script src="https://gist.github.com/1978926.js"&gt; &lt;/script&gt;

&lt;p&gt;&lt;strong&gt;Drum roll!&lt;/strong&gt; It took ~80 millisconds to run!&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;I'm aching to sneak in the time to get to &lt;code&gt;Level 1&lt;/code&gt; which is 25 problems solved! I just got my decathlete badge today.  Unfortunately the badges don't show in the little flair, they only show in your statistics page:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://projecteuler.net/profile/gideondsouza.png" alt="Project Euler Badge"&gt;&lt;/p&gt;

&lt;p&gt;I'm put up all my problems as I go on github. Check it out here:&lt;br&gt;
&lt;a href="https://github.com/gideondsouza/ProjectEuler_CSharp"&gt;https://github.com/gideondsouza/ProjectEuler_CSharp&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully this was somewhat insightful. &lt;/p&gt;
</description><pubDate>Mon, 05 Mar 2012 15:52:00 GMT</pubDate><guid isPermaLink="true">http://gideondsouza.com:80/blog/a-superfast-way-to-search-for-prime-numbers</guid></item><item><title>A nifty export to CSV linq extension</title><link>http://gideondsouza.com:80/blog/a-nifty-export-to-csv-linq-extension</link><description>&lt;p&gt;So I've been using this little gem of a code snippet in quite a few apps.&lt;/p&gt;

&lt;p&gt;Nope, I didn't come up with it on my own, I got it from this blog article:&lt;br&gt;
&lt;a href="http://mikehadlow.blogspot.com/2008/06/linq-to-csv.html"&gt;http://mikehadlow.blogspot.com/2008/06/linq-to-csv.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm not sure what exactly wasn't working but I changed something I don't quite remember.&lt;/p&gt;

&lt;p&gt;You use it like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;IList&amp;lt;AnyTypeWithProperties&amp;gt; tableData = ...
tableData.AsCsv();//returns the data as csv in a string.
&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;How-To:&lt;/h4&gt;

&lt;p&gt;Add a new file into visual studio, call it 'ExportToCsvExtension.cs' or anything you want. Remember an extension method must be defined on the TOP of its namespace.&lt;/p&gt;

&lt;p&gt;Here is the code:&lt;/p&gt;

&lt;script src="https://gist.github.com/1422966.js"&gt; &lt;/script&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;
</description><pubDate>Sat, 26 Nov 2011 15:05:21 GMT</pubDate><guid isPermaLink="true">http://gideondsouza.com:80/blog/a-nifty-export-to-csv-linq-extension</guid></item><item><title>Abstracting reactive extensions for sql server compact and implementing an instant search</title><link>http://gideondsouza.com:80/blog/abstracting-reactive-extensions-for-sql-server-compact-and-implementing-an-instant-search</link><description>&lt;p&gt;So in the &lt;a href="http://www.gideondsouza.com/blog/implementing-simple-instant-search-with-rx-reactive-extensions"&gt;last Rx article&lt;/a&gt; I talked about instant search. This article, we're going to go quite a few steps further and search an sql-compact-4 database asychronously! Thats right!&lt;/p&gt;

&lt;p&gt;But! Its going to be a little head churner with lambdas and what nots, because we're going to encapsulate retreiving the data in a nice clean way(in my opinion) &lt;/p&gt;

&lt;p&gt;You could also just:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="#code"&gt;Skip to the code below&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.gideondsouza.com/Media/Default/Misc/BasicRxSearchClean.zip"&gt;Download the Sample&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Check the source on &lt;a href="https://github.com/thegiddygeek/RxSearchDB"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What are we making:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/product.JPG" alt="enter image description here"&gt;&lt;/p&gt;

&lt;p&gt;You'll need the &lt;a href="http://visualstudiogallery.msdn.microsoft.com/0e313dfd-be80-4afb-b5e9-6e74d369f7a1/"&gt;SQL CE Compact Toolbox&lt;/a&gt; because we are going to want an entity framework object context. (Which visual studio won't create for sqlce4 if its not a web project)&lt;/p&gt;

&lt;p&gt;You'll also want the solution from the &lt;a href="http://www.gideondsouza.com/blog/implementing-simple-instant-search-with-rx-reactive-extensions"&gt;previous blog post on Rx&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Instructions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt; - Open the solution, open solution explorer (Ctrl + W, S). Right click the project and choose &lt;em&gt;add new item&lt;/em&gt;. Select &lt;em&gt;Data&lt;/em&gt; on the left and choose &lt;em&gt;SQL Server Compact 4 Local Database&lt;/em&gt; (Phew!)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt; - Its more easier to use Visual Studio's server explorer to create a new table. Open server explorer (Ctrl + W, L), it should show your database. (If it doesn't, right click connections and click &lt;em&gt;add connection&lt;/em&gt;). Expand your database, Right click Tables and click &lt;em&gt;Create Table&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Create a table like the following, note &lt;em&gt;ID&lt;/em&gt; is an identity:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/step2.JPG" alt="enter image description here"&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt; - Throw in some data:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/step3.JPG" alt="enter image description here"&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt; - Add an entity model for your database. You'll need to have installed SQL Compact Toolbox, goto &lt;code&gt;Tools&amp;gt;Sql Compact Toolbox&lt;/code&gt;, Right Click your database and click &lt;em&gt;Add Entity Data Model to current project&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/step4.jpg" alt="enter image description here"&gt;&lt;/p&gt;

&lt;h4&gt;The code&lt;p id="code"&gt;&lt;/h4&gt;

&lt;p&gt;We'll add two helper classes to &lt;strong&gt;abstract&lt;/strong&gt; the task of searching our database asychronously with Rx.&lt;/p&gt;

&lt;script src="https://gist.github.com/1422970.js"&gt; &lt;/script&gt;

&lt;p&gt;So we're defining a method here that will be based on a Type T, which will be our entity object and TContext which will be the DataContext. This complete generic interface will help you later on when you want to mock and/or use DI. (I typically use &lt;a href="http://mef.codeplex.com/"&gt;MEF&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;We then implement it like so:&lt;/p&gt;

&lt;script src="https://gist.github.com/1422975.js"&gt; &lt;/script&gt;

&lt;p&gt;Notice, we'll use the &lt;code&gt;T&lt;/code&gt; at &lt;em&gt;runtime&lt;/em&gt; so we can use this like a repository for any entity object. &lt;/p&gt;

&lt;p&gt;Our one and only function &lt;code&gt;GetAllAsObservables&lt;/code&gt;, uses the &lt;code&gt;ToAsync&lt;/code&gt; method to &lt;strong&gt;convert&lt;/strong&gt; the lambda passed as an argument, into a function which returns an &lt;code&gt;IObservable&lt;/code&gt;. We then make a call to this function &lt;code&gt;getall()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We wrote &lt;code&gt;ToAsync(Database1Entities, IQueryable&amp;lt;T&amp;gt;)&lt;/code&gt; so &lt;code&gt;getall&lt;/code&gt; is a function that takes an &lt;strong&gt;instance&lt;/strong&gt; of &lt;code&gt;Database1Entities&lt;/code&gt; and returns an &lt;code&gt;IObservable&amp;lt;T&amp;gt;&lt;/code&gt; which we transform/project into &lt;code&gt;IObservable&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our MainForm code now looks like this:&lt;/p&gt;

&lt;script src="https://gist.github.com/1422976.js"&gt; &lt;/script&gt;

&lt;h4&gt;Explaination :&lt;/h4&gt;

&lt;p&gt;First off, what we did &lt;a href="http://www.gideondsouza.com/blog/implementing-simple-instant-search-with-rx-reactive-extensions"&gt;last time&lt;/a&gt; was, we changed the &lt;code&gt;Textbox.TextChanged&lt;/code&gt; event to an observable sequence and throttled it by 500 milliseconds. So, our code got called by the &lt;em&gt;system&lt;/em&gt; every 500ms, we then executed &lt;code&gt;DO&lt;/code&gt; every time this happened. &lt;/p&gt;

&lt;p&gt;The original &lt;code&gt;SearchList&lt;/code&gt; function does not &lt;strong&gt;search asynchronously&lt;/strong&gt;, you could try and put &lt;code&gt;Thread.Sleep(5000)&lt;/code&gt; inside and it will block the UI.&lt;/p&gt;

&lt;p&gt;So now!! We execute an async call through our little helper class, to search our database.&lt;/p&gt;

&lt;p&gt;Remember our &lt;code&gt;GetAllAsObservables()&lt;/code&gt; expects a lamba that takes our data context and returns an &lt;code&gt;IQueryable&amp;lt;T&amp;gt;&lt;/code&gt;, this means you can pretty much run any query against your context an long as it returns the right type. Also, this runs in the context of sql, not in memory.&lt;/p&gt;

&lt;p&gt;We get in return an &lt;code&gt;IObservable&amp;lt;IList&amp;lt;T&amp;gt;&amp;gt;&lt;/code&gt; getfn. We then use &lt;code&gt;ObserveOn&lt;/code&gt; because we're going to touch UI from another thread. (This is equivalent to those &lt;code&gt;Invoke()&lt;/code&gt; calls you made in your old async code). This time on subscribe you are returned a an &lt;code&gt;IList&amp;lt;Customer&amp;gt;&lt;/code&gt;. The lambda you pass into &lt;code&gt;Subscribe&lt;/code&gt; is executed when the search is done async!&lt;/p&gt;

&lt;p&gt;So try and run the code! Type in some names! =D&lt;/p&gt;

&lt;p&gt;I hope this was useful, I'll take this further and get into how to properly abstract data access with Rx by creating Observable repositories. I've used these quite sucessfully in a number of apps!&lt;/p&gt;

&lt;p&gt;Any critique,  good, bad, ugly is welcome!&lt;/p&gt;

&lt;p&gt;Happy Coding&lt;/p&gt;
</description><pubDate>Sat, 19 Nov 2011 11:04:27 GMT</pubDate><guid isPermaLink="true">http://gideondsouza.com:80/blog/abstracting-reactive-extensions-for-sql-server-compact-and-implementing-an-instant-search</guid></item><item><title>Implementing simple instant search with Rx (Reactive Extensions)</title><link>http://gideondsouza.com:80/blog/implementing-simple-instant-search-with-rx-reactive-extensions</link><description>&lt;p&gt;Haven't heard of Rx, this could be a little starter tutorial for you.&lt;/p&gt;

&lt;p&gt;Rx or Microsoft's &lt;a href="http://msdn.microsoft.com/en-us/data/gg577609"&gt;Reactive extensions &lt;/a&gt;makes life simple when it comes to asychronous programming&lt;/p&gt;

&lt;p&gt;My most favorite thing about Rx is how simple it makes &lt;em&gt;instant&lt;/em&gt; search! 
A while ago I &lt;a href="http://stackoverflow.com/questions/4278069/implement-instant-threaded-search-algorithm"&gt;asked around on stackoverflow&lt;/a&gt; and just architecturing it was so difficult.&lt;/p&gt;

&lt;p&gt;So I'm going to go through a dead simple example, where we're going to have an &lt;strong&gt;instant search&lt;/strong&gt;, but, we'll have some dummy data in memory that we're going to search.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; : I wrote a continuation of this article &lt;a href="http://gideondsouza.com/blog/abstracting-reactive-extensions-for-sql-server-compact-and-implementing-an-instant-search"&gt;here&lt;/a&gt; where I describe instant search with Rx on a SqlCE database and a nice clean abstraction.&lt;/p&gt;

&lt;p&gt;Here's what it'll look like:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/appCapture.JPG" alt="Instant Search App"&gt;&lt;/p&gt;

&lt;p&gt;I hope to do a post on searching real data, maybe with sql-compact 4, and then I'll get on to how to write &lt;a href="http://stackoverflow.com/questions/5349140/iasyncrepository-or-iobservablerepository-for-silverlight-4-wcf-data-services"&gt;Observable Repositories&lt;/a&gt; which makes life easier, especially for Silverlight!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How To:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So head over to Visual Studio and create a new form (or you can &lt;a href="http://www.gideondsouza.com/Media/Default/Misc/BasicRxSearchClean.zip"&gt;download the sample&lt;/a&gt;). Add a textbox and listbox like the screenshot.&lt;/p&gt;

&lt;p&gt;We need to install &lt;a href="http://nuget.org/List/Packages/Rx-Main"&gt;Rx-Main&lt;/a&gt; and &lt;a href="http://nuget.org/List/Packages/Rx-WinForms"&gt;Rx-WinForms&lt;/a&gt; from nuget.&lt;/p&gt;

&lt;p&gt;You can right click your solution and choose &lt;code&gt;Manage Nuget Packages..&lt;/code&gt; and search and install these.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/nugetScrenie.JPG" alt="Visual Studio Package Manager"&gt;&lt;/p&gt;

&lt;p&gt;This is the code then:&lt;/p&gt;

&lt;script src="https://gist.github.com/1422982.js"&gt; &lt;/script&gt;

&lt;p&gt;&lt;strong&gt;What/How/Why?&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;The starting point to observables is the &lt;a href="http://msdn.microsoft.com/en-us/library/hh244252(v=VS.103).aspx"&gt;Observable&lt;/a&gt; class, the &lt;a href="http://msdn.microsoft.com/en-us/library/hh229251(v=VS.103).aspx"&gt;FromEventPattern&lt;/a&gt; method creates an observable sequence from the &lt;code&gt;TextChanged&lt;/code&gt; event of the textbox. &lt;/p&gt;

&lt;p&gt;Think of this like a list of notifications coming to you. &lt;/p&gt;

&lt;p&gt;We then chain a method call to &lt;a href="http://msdn.microsoft.com/en-us/library/hh229400(v=VS.103).aspx"&gt;Throttle&lt;/a&gt; (my fav part!), it well, throttles this list of notifications by 500 milliseconds. &lt;/p&gt;

&lt;p&gt;Here, for win-forms you need the &lt;a href="http://msdn.microsoft.com/en-us/library/hh212067(v=VS.103).aspx"&gt;ControlScheduler&lt;/a&gt; class otherwise the throttling doesn't work too well. This class comes from the Rx-WinForms package we added earlier. The &lt;a href="http://msdn.microsoft.com/en-us/library/hh228941(v=VS.103).aspx"&gt;Do&lt;/a&gt; method just does the metho you want to run, and you send off the text from the the textbox over. Lastly, calling Subscribe starts off the notifications.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.gideondsouza.com/Media/Default/Misc/BasicRxSearchClean.zip"&gt;Download the Sample here.&lt;/a&gt;(1.3MB) It's a clean solution but with the Rx nuget packages.&lt;/p&gt;

&lt;p&gt;Hope this was useful!&lt;/p&gt;
</description><pubDate>Thu, 17 Nov 2011 19:21:00 GMT</pubDate><guid isPermaLink="true">http://gideondsouza.com:80/blog/implementing-simple-instant-search-with-rx-reactive-extensions</guid></item><item><title>Scanning multiple documents with WIA 2.0 (ADF Scanner)</title><link>http://gideondsouza.com:80/blog/scanning-multiple-documents-with-wia-2.0-adf-scanner</link><description>&lt;p&gt;So a long time ago I had to do a project that involved getting images from a scanner with an auto document feeder. It was a challenge doing this with WIA but I found some code somewhere online and made it into a nice library.&lt;/p&gt;

&lt;p&gt;Today I finally shared this library with the world here: &lt;a href="http://adfwia.codeplex.com/"&gt;http://adfwia.codeplex.com/&lt;/a&gt;&lt;br&gt;
[&lt;strong&gt;Update&lt;/strong&gt;-15Dec2011]: I put it up on github too: &lt;a href="https://github.com/gideondsouza/AutoDocumentFeed_for_WIA"&gt;https://github.com/gideondsouza/AutoDocumentFeed&lt;em&gt;for&lt;/em&gt;WIA&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It includes the source under the MIT License, it also has a little test app you can play with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Some scanners have trouble with the DIP setting. If you get:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Unhandled Exception : Value does not fall within range. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Try a higher or lower DPI.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/screenie[4].jpg" alt="enter image description here"&gt;&lt;/p&gt;
</description><pubDate>Sun, 23 Oct 2011 13:15:00 GMT</pubDate><guid isPermaLink="true">http://gideondsouza.com:80/blog/scanning-multiple-documents-with-wia-2.0-adf-scanner</guid></item><item><title>Difference between dependency injection and mocking frameworks</title><link>http://gideondsouza.com:80/blog/difference-between-dependency-injection-and-mocking-frameworks</link><description>&lt;p&gt;This was originally answer I posted to a question on stackoverflow &lt;a href="http://stackoverflow.com/questions/5433211/difference-between-dependency-injection-and-mocking-framework-ninject-vs-rhinom/5433231#5433231"&gt;here&lt;/a&gt;. Since its popularity I posted it as an article here.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://ninject.org/"&gt;Ninject&lt;/a&gt; is &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;Dependency Injection&lt;/a&gt; for .NET.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.ayende.com/projects/rhino-mocks.aspx"&gt;RhinoMocks&lt;/a&gt; and &lt;a href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; are both mocking frameworks.&lt;/p&gt;

&lt;p&gt;Now both have nothing to do with each other. I really had trouble understanding both so here I go trying to explain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependency Injection&lt;/strong&gt;: is an implementation (lets call it) of Inversion of Control. You don't confuse the two. You are taking the control of creating an object out of your code. Dependencies, like say a &lt;code&gt;IRepository&lt;/code&gt; would not be created by your classes/code but instead &lt;em&gt;injected&lt;/em&gt; by someone else, a dependency injection framework.&lt;/p&gt;

&lt;p&gt;Lets say you have:&lt;/p&gt;

&lt;script src="https://gist.github.com/1423353.js"&gt; &lt;/script&gt;

&lt;p&gt;Now you have an actual implementation:&lt;/p&gt;

&lt;script src="https://gist.github.com/1423394.js"&gt; &lt;/script&gt;

&lt;p&gt;Now all over the place, you'll have:&lt;/p&gt;

&lt;script src="https://gist.github.com/1423395.js"&gt; &lt;/script&gt;

&lt;p&gt;Why? Ask yourself why you made an interface in the first place? So you can cope with &lt;strong&gt;change&lt;/strong&gt;. Well now, when you need to change your repository to something else. You have to replace all the lines that have &lt;code&gt;new MyUserRepo()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A simple method is user a factory method which is another form of IOC.&lt;/p&gt;

&lt;script src="https://gist.github.com/1423450.js"&gt; &lt;/script&gt;

&lt;p&gt;And use it like this:&lt;/p&gt;

&lt;script src="https://gist.github.com/1423454.js"&gt; &lt;/script&gt;

&lt;p&gt;Now when you have to change your repository you have to change only your factory. &lt;strong&gt;Dependency injection&lt;/strong&gt; takes this to the next level by doing all the work. You don't need to change the code at all (or maybe a few declarations).&lt;/p&gt;

&lt;script src="https://gist.github.com/1423469.js"&gt; &lt;/script&gt;

&lt;hr&gt;

&lt;p&gt;&lt;strong&gt;A Mocking Framework&lt;/strong&gt; : Boy this was like rocket science to me. But Steven Sandersons book had a brilliant simple explanation.&lt;/p&gt;

&lt;p&gt;We keep going on with the &lt;code&gt;IUserRepository&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now you have to test some complicated UI/Authentication whatever that depends on &lt;code&gt;IUserRepository&lt;/code&gt;. &lt;/p&gt;

&lt;script src="https://gist.github.com/1423474.js"&gt; &lt;/script&gt;

&lt;p&gt;Now in your test, when you make &lt;code&gt;IUserRepository&lt;/code&gt; an instance of &lt;code&gt;MyUserRepo&lt;/code&gt;. If something goes wrong you don't know what went wrong! Was it your user control or your database connection?&lt;/p&gt;

&lt;p&gt;You want make the test more deterministic as someone said. &lt;/p&gt;

&lt;p&gt;So you make a fake user repository.&lt;/p&gt;

&lt;script src="https://gist.github.com/1423481.js"&gt; &lt;/script&gt;

&lt;p&gt;So now, when you pass this &lt;strong&gt;fake&lt;/strong&gt; repo. If you're test fails you KNOW it was something else, not the data base.&lt;/p&gt;

&lt;p&gt;My example was simple, but if its a large number of Interfaces. You'll need to write a lot of &lt;em&gt;fake&lt;/em&gt; code, its a lot of code bloat! &lt;/p&gt;

&lt;p&gt;So you can use a mocking framework to write less code here.&lt;/p&gt;

&lt;p&gt;Moq uses a fluent interface and is quite nice. Using Moq would look like this:&lt;/p&gt;

&lt;script src="https://gist.github.com/1423487.js"&gt; &lt;/script&gt;

&lt;p&gt;Creating fake objects becomes a lot easier =)&lt;/p&gt;

&lt;p&gt;Now I hope your seeing how you can use both to your advantage. You can create your fake objects with a mocking framework, then use dependency injection to hook up the right objects at the right time.&lt;/p&gt;

&lt;p&gt;For my smaller Silverlight applications I use &lt;a href="http://mef.codeplex.com/"&gt;MEF&lt;/a&gt; (Inbuilt in .Net4) for Dependency Injection. And then I have little &lt;code&gt;#Ifdef&lt;/code&gt; on the declarations for which classes to &lt;code&gt;Export&lt;/code&gt; (or expose) Based on a &lt;code&gt;#define&lt;/code&gt;  symbol. So I just change one &lt;code&gt;#define&lt;/code&gt; and I can switch my app to using fake classes here and there.&lt;/p&gt;

&lt;p&gt;Really Hope that was helpful.&lt;/p&gt;
</description><pubDate>Sun, 23 Oct 2011 09:44:21 GMT</pubDate><guid isPermaLink="true">http://gideondsouza.com:80/blog/difference-between-dependency-injection-and-mocking-frameworks</guid></item></channel></rss>