<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" gd:etag="W/&quot;CkUHQX0zfCp7ImA9WhBUEk0.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533</id><updated>2013-04-28T21:50:30.384-04:00</updated><category term="Threading" /><category term="calendar" /><category term="Windows RT" /><category term="dependency injection" /><category term="ienumerable" /><category term="static" /><category term="lists" /><category term="azure" /><category term="moles" /><category term="multithreading" /><category term="ARM" /><category term="jaxdug" /><category term="MSBuild" /><category term="jaxcc" /><category term="salesforce" /><category term="extension methods" /><category term="VS2008" /><category term="inversion of control" /><category term="rest" /><category term="mvc" /><category term="c#" /><category term="pair programming" /><category term="objective-c" /><category term="jquery" /><category term="WF" /><category term="popup" /><category term="moq" /><category term="nuget" /><category term="mocking" /><category term="ninject" /><category term="VS2012" /><category term="generics" /><category term="rss" /><category term="session" /><category term="VS2010" /><category term="unit testing" /><category term="asp.net" /><category term="web api" /><category term="iOS" /><category term="channel9" /><category term="c++" /><category term="WPF" /><category term="vista" /><category term="json" /><category term="vb.net" /><category term="aero" /><title>Adventures in .NET</title><subtitle type="html">Tips and Tricks for VB and C# Developers</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>58</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/AdventuresDotNet" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="adventuresdotnet" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CUYFR3o5fyp7ImA9WhJUFUs.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-5662702153332204415</id><published>2012-09-13T15:05:00.001-04:00</published><updated>2012-09-13T15:05:16.427-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-13T15:05:16.427-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="c++" /><category scheme="http://www.blogger.com/atom/ns#" term="ARM" /><category scheme="http://www.blogger.com/atom/ns#" term="VS2012" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows RT" /><title>Compiling Desktop Apps for Windows RT… Maybe</title><content type="html">&lt;p&gt;As you may know, Windows RT when it launches in 6 weeks will only support limited applications in desktop mode, like Notepad, Paint, Explorer, and Office Home &amp;amp; Student 2013 RT. However, Visual Studio 2012 ships with a fully-functional ARM cross-compiler for C++ code! Fully-functional means just that – it can compile any assembly for the ARM platform.&lt;/p&gt; &lt;p&gt;The compiler is located in C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\x86_arm. If you want to use the ARM compiler tools, like cl, you must first register the environment variables by executing vcvarsx86_arm.bat in that folder.&lt;/p&gt; &lt;p&gt;However, if you try to use cl to compile a simple console application, like a Hello World example, it will give you this nasty (and expected) error:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;fatal error C1189: #error :&amp;nbsp; Compiling Desktop applications for the ARM platform is not supported.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;I was curious about whether or not it was possible to get around this. Then I found &lt;a href="http://stackoverflow.com/a/11708741"&gt;this StackOverflow question&lt;/a&gt; that gave me a clue. There is a part of the file "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\crtdefs.h" that contains a reference to an ARM SDK definition:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;#if defined(_M_ARM) &lt;br&gt; #if _CRT_BUILD_DESKTOP_APP &amp;amp;&amp;amp; !_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE&lt;br&gt;&amp;nbsp; #error Compiling Desktop applications for the ARM platform is not supported.&lt;br&gt; #endif&lt;br&gt;#endif&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;So all you have to do is define that _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE with the /D argument and you can compile for ARM:&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-OQ10w6pjNhY/UFIuaJb7XYI/AAAAAAAAAbg/TxJiwnua9dE/s1600-h/image%25255B8%25255D.png"&gt;&lt;img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://lh3.ggpht.com/-3MaALaTNkXE/UFIuahc-dtI/AAAAAAAAAbo/kaUni5h8V7M/image_thumb%25255B4%25255D.png?imgmax=800" width="587" height="300"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Obviously you can’t run ARM apps on an x86/x64 machine, but this shows that it did successfully compile a PE executable for the Windows on ARM platform. Now, since there aren’t any Windows RT devices out yet to test this on, I can’t even begin to say whether or not this will work. Most likely I’m assuming that Windows RT has some sort of either whitelisting or digital signature verification for desktop apps that are allowed to run, which would be beyond my ability to work around. &lt;/p&gt; &lt;p&gt;But, this brings up the point that obviously developers at Microsoft have been using an ARM compiler to compile apps for Windows RT, and it seems like they are most likely using these same tools. I’m assuming they define that _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE definition when they compile, too. And perhaps there’s a more elaborate SDK available, since that definition mentions “DESKTOP_SDK”, if it is true that there are digital signatures involved.&lt;/p&gt; &lt;p&gt;So it could be (and is most likely the case) that this was a complete waste of 10 minutes if it won’t run on Windows RT without some hackery, and I won’t know that until 6 weeks or so when I hope to have my hands on a Microsoft Surface – but it was a fun exercise nonetheless.&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/5662702153332204415/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=5662702153332204415" title="8 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/5662702153332204415?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/5662702153332204415?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2012/09/compiling-desktop-apps-for-windows-rt.html" title="Compiling Desktop Apps for Windows RT… Maybe" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-3MaALaTNkXE/UFIuahc-dtI/AAAAAAAAAbo/kaUni5h8V7M/s72-c/image_thumb%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>8</thr:total></entry><entry gd:etag="W/&quot;C0cGSHozeCp7ImA9WhJVEEQ.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-2728583488122730620</id><published>2012-08-27T14:10:00.001-04:00</published><updated>2012-08-27T14:10:29.480-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-08-27T14:10:29.480-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="nuget" /><category scheme="http://www.blogger.com/atom/ns#" term="c#" /><title>Extending Blaze with NuGet Support</title><content type="html">&lt;h2&gt;&lt;/h2&gt; &lt;h3&gt;TL;DR Version&lt;/h3&gt; &lt;p&gt;I recently extended the open-source &lt;a href="http://db.codeplex.com/"&gt;Blaze&lt;/a&gt; C# Script IDE with &lt;a href="http://nuget.org/"&gt;NuGet&lt;/a&gt; support. You can get the latest version at &lt;a href="http://db.codeplex.com/"&gt;the Blaze project site&lt;/a&gt;, or check out &lt;a href="https://bitbucket.org/paulirwin/sharpscript"&gt;the source code changes at Bitbucket&lt;/a&gt;.&lt;/p&gt; &lt;h3&gt;&lt;/h3&gt; &lt;h3&gt;&lt;/h3&gt; &lt;h3&gt;Continuing on…&lt;/h3&gt; &lt;p&gt;I am a big fan of &lt;a href="http://en.wikipedia.org/wiki/REPL"&gt;REPL loops&lt;/a&gt;. Even if you’re not familiar with the term, you’ve probably used one as a software developer. Standing for “Read-Evaluate-Print Loop”, it’s a console-like entry where you can type code and immediately execute it and see the result. For an example, hit F12 on any page in Google Chrome and go over to the Console tab. You can start typing JavaScript and hit enter and it will immediately run.&lt;/p&gt; &lt;p&gt;As a C# developer, occasionally I find myself needing to use a REPL to validate logic. Of course, I could open up a new Visual Studio console application project and do it that way, but that takes a while to get set up and then you have to write a bunch of nasty other code to handle the console, and then you have all these files on your hard drive that you probably won’t use again. A REPL makes that situation so much easier, as you can just fire it up, type in your code, run it, and see the result without creating a single file on your hard disk (temporary files aside, of course).&lt;/p&gt; &lt;p&gt;Thanks to &lt;a href="http://msdn.microsoft.com/en-us/vstudio/hh500769.aspx"&gt;the Roslyn project&lt;/a&gt;, there will be a Microsoft-supported C# REPL sometime in the future. Until then, though, I’ve previously been using the good-but-not-great &lt;a href="http://tirania.org/blog/archive/2008/Nov-02.html"&gt;gsharp&lt;/a&gt; utility that’s included as part of the &lt;a href="http://www.mono-project.com/Main_Page"&gt;Mono project&lt;/a&gt; install. That is, until I heard about &lt;a href="http://db.codeplex.com/"&gt;Blaze&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-uKps9lcQ4qw/UDu4Bithz8I/AAAAAAAAAaY/_KQAQKL394c/s1600-h/image%25255B3%25255D.png"&gt;&lt;img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh5.ggpht.com/-Zg0RbnMoUwQ/UDu4CJ45N4I/AAAAAAAAAac/am45L-Kc14c/image_thumb%25255B1%25255D.png?imgmax=800" width="499" height="375"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Blaze is a C# script IDE that is the next best thing to a C# REPL. In many ways, it’s better than a REPL, as it is document based, so you can save and load scripts to/from your hard drive, and you can run just a portion of the script by selecting the text to run and hitting F5. It’s developed by my colleague, fellow &lt;a href="http://www.feature23.com/"&gt;feature[23]&lt;/a&gt; lead software engineer &lt;a href="http://www.feature23.com/about/teammember/zack-gramana"&gt;Zachary Gramana&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Since I heard about Blaze shortly after starting with feature[23], I’ve been using it often for testing code and validating logic. It’s exactly what I need as a C# developer, and it’s now an essential part of my toolkit. You can download the app from the &lt;a href="http://db.codeplex.com/"&gt;Codeplex project site&lt;/a&gt; by hitting the big Download button on the right side. It’s a ClickOnce application, so any time it gets updated you’ll get the latest version on launch.&lt;/p&gt; &lt;p&gt;One handy feature of Blaze is that you can add references to DLLs, via the “Reference Assemblies…” menu option under “Runtime Settings”. This allows you to add in, say, a reference to Json.NET (also known as Newtonsoft.Json) and test out JSON serialization using a third-party utility before adding it to your app.&lt;/p&gt; &lt;p&gt;That’s exactly what I was trying to do last week when I realized “hey wait a second, I usually add Json.NET to my project via NuGet, so now I have to hunt around for the DLLs!” After a bit of thinking and discussion, I realized that I could probably add NuGet support to Blaze! And so I did.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-OFHAS-bElMY/UDu4CVsV0jI/AAAAAAAAAak/8JDS3PljiDI/s1600-h/image%25255B8%25255D.png"&gt;&lt;img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-yOW7aDn-wjU/UDu4Cv90V2I/AAAAAAAAAas/FidvykKMjEM/image_thumb%25255B4%25255D.png?imgmax=800" width="545" height="402"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;From the Add References screen, click Add a NuGet Package and you’ll get this dialog above. It’s designed like the Package Manager console, so you can type in the name just like you might see on the NuGet Gallery package page, &lt;a href="http://nuget.org/packages/Newtonsoft.Json"&gt;like this one for Json.NET&lt;/a&gt;. (Specific versions with the --version argument are not yet supported.)&lt;/p&gt; &lt;p&gt;After typing in the term, hit enter, and it’ll search NuGet for the package. If it’s found, you’ll see any dependencies the package has, as well as it’s DLLs.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-6zzezv--qfE/UDu4DNrgvoI/AAAAAAAAAa4/0TaKINSvgag/s1600-h/image%25255B12%25255D.png"&gt;&lt;img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh6.ggpht.com/-ayJ4YyN3LmI/UDu4DgKEn6I/AAAAAAAAAbA/kOYc8migffQ/image_thumb%25255B6%25255D.png?imgmax=800" width="501" height="261"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Selecting a DLL then clicking Add Selected References adds the reference to the parent dialog. If there are any dependencies, double-clicking on the dependency will jump to that NuGet package.&lt;/p&gt; &lt;p&gt;After adding the references, you can then use the DLLs as you normally would.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-t8LS_YNAkuA/UDu4EbL1QpI/AAAAAAAAAbI/6VJf_SjKvdA/s1600-h/image%25255B17%25255D.png"&gt;&lt;img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://lh4.ggpht.com/-l8zDb0i4TxU/UDu4E5qxu_I/AAAAAAAAAbQ/hxJDA6TFjI4/image_thumb%25255B9%25255D.png?imgmax=800" width="701" height="491"&gt;&lt;/a&gt;&lt;/p&gt; &lt;h3&gt;Using the NuGet API&lt;/h3&gt; &lt;p&gt;The NuGet API is very clean and easy to use, but there aren’t many good tutorials out there for using it. Here’s a simple example tutorial, hopefully from here you can explore the rest of the API and make more advanced use of it. First, add a reference to NuGet.Core using NuGet (how meta is that?!). Next, create a new PackageRepository using the PackageRepositoryFactory:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font face="Consolas"&gt;var repo = PackageRepositoryFactory.Default.CreateRepository("https://nuget.org/api/v2/");&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Of course, you eventually will want to not hard-code that link (although it’s currently hard-coded in Blaze). Next, if you’re looking for a package by its identifier (like we are in Blaze), you’ll call FindPackage on that repository, passing in the identifier as the sole parameter. (If you’d rather search, you can call repo.Search and handle the results differently.)&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font face="Consolas"&gt;var result = repo.FindPackage("Newtonsoft.Json") as DataServicePackage;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;This method can throw exceptions, like if there’s a 500 server error, so make sure this is wrapped in a try/catch. If it is successful, result will be a DataServicePackage instance. You’ll want to cast it as DataServicePackage so you have access to the package DownloadUrl property. To download the package, first new up a PackageDownloader.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font face="Consolas"&gt;var downloader = new PackageDownloader();&lt;br&gt;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Now you can call DownloadPackage to do an HTTP request to download the package, which is basically a ZIP package.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font face="Consolas"&gt;var downloaded = downloader.DownloadPackage(new HttpClient(result.DownloadUrl), result) as NuGet.ZipPackage;&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The DownloadPackage method is a blocking call that can also throw exceptions. Now that you have the ZipPackage, you’ll want to open it to extract the files. In our case, we’re looking for an exact file name to extract. In order to extract files, you must use the System.IO.Packaging namespace that’s in WindowsBase.dll, part of the .NET Framework 4.0. You’ll usually need to add a reference to this DLL, unless you’re writing a WPF app.&lt;/p&gt; &lt;p&gt;Finally, you’ll need to open the package stream, open the stream of the file within the package you want to extract, then copy the stream.&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font face="Consolas"&gt;using (var stream = downloaded.GetStream())&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var zip = Package.Open(stream);&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; var part = zip.GetPart(new Uri("/lib/net40/Newtonsoft.Json.dll", UriKind.Relative));&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string filePath = "C:\\Users\\Paul Irwin\\Desktop\\Newtonsoft.Json.dll";&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (var zipFileStream = part.GetStream())&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (var fileStream = new FileStream(filePath, FileMode.CreateNew))&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; zipFileStream.CopyTo(fileStream);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;}&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;The namespaces you’ll want to include are NuGet, System.IO, and System.IO.Packaging. And, of course, I hard coded a number of strings in that example, but you should be able to take this from here and modify it as you please. &lt;/p&gt; &lt;p&gt;All in all I’ve enjoyed playing around with the NuGet API, and it was a fun project to work on!&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/2728583488122730620/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=2728583488122730620" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/2728583488122730620?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/2728583488122730620?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2012/08/extending-blaze-with-nuget-support.html" title="Extending Blaze with NuGet Support" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-Zg0RbnMoUwQ/UDu4CJ45N4I/AAAAAAAAAac/am45L-Kc14c/s72-c/image_thumb%25255B1%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;D0AEQXc_cSp7ImA9WhVbE08.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-1915749823483806653</id><published>2012-05-29T14:27:00.001-04:00</published><updated>2012-05-29T17:08:20.949-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-29T17:08:20.949-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="c#" /><category scheme="http://www.blogger.com/atom/ns#" term="iOS" /><category scheme="http://www.blogger.com/atom/ns#" term="objective-c" /><title>Translating Objective-C to C# – Part 2 of n</title><content type="html">&lt;p&gt;I hope by now you’ve already read &lt;a href="http://adventuresdotnet.blogspot.com/2012/05/translating-objective-c-to-c-part-1-of.html"&gt;Part 1&lt;/a&gt;, and if not, go do so now! As I left off last time I mentioned that I’d cover interfaces vs. implementations, type checking, inheritance, alloc-init, polymorphism, self/this and super/base keywords, properties, and initializers from Chapters 4-5 of the excellent book &lt;a href="http://shop.oreilly.com/product/0636920023562.do"&gt;Programming iOS 5, Second Edition&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;These 2 chapters get a little more in-depth into understanding Objective-C enough that you can then go and understand the basic Cocoa usage examples. This might be the last post purely about the Objective-C and C# languages, but I hope to get into more of a framework discussion of translating Cocoa to XAML for Windows Phone or Windows 8 Metro app development.&lt;/p&gt;  &lt;p&gt;Again, I’ll be using a subset of the examples from the book as the source, but to fully understand the context, I recommend picking up a physical or eBook copy as it really is an excellent book!&lt;/p&gt;  &lt;h2&gt;Interface vs Implementation&lt;/h2&gt;  &lt;p&gt;At first, I assumed that Objective-C interfaces were much like C# interfaces in that they define the methods that a concrete type implements (and Objective-C actually uses the @implementation keyword for the concrete type), but after getting to the Cocoa examples, I realized this is not the case. &lt;/p&gt;  &lt;p&gt;An Objective-C interface is pretty much an equivalent of a C++ header file (and, in fact, if you have the interface in a separate file, it uses a .h file extension, just like a C++ header file). It does not define an interface that other types can use as a contract – that’s what protocols are for, which I’ll cover later.&lt;/p&gt;  &lt;p&gt;In Objective-C, you must have an @interface and an @implementation for classes you create. They can be in the same file or in separate files (the @interface in a .h file, and the @implementation in a .m file). In C#, you do not need a “header” for defining a class – this information is extracted as metadata at compile time.&lt;/p&gt;  &lt;p&gt;Here’s the example of a trivial, single-file interface and implementation:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@interface MyClass : NSObject        &lt;br /&gt;- (NSString*) sayGoodnightGracie;         &lt;br /&gt;@end         &lt;br /&gt;@implementation MyClass         &lt;br /&gt;- (NSString*) sayGoodnightGracie {         &lt;br /&gt;return @&amp;quot;Good night, Gracie!&amp;quot;;         &lt;br /&gt;}         &lt;br /&gt;@end&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Notice that the @interface is where the inheritance is defined – MyClass here inherits from NSObject. There is a single instance (non-static) method called sayGoodnightGracie, and its signature is defined in the @interface and implemented in the @implementation. Notice that the interface and implementation share the same name – that should be your first clue that interfaces in Objective-C are not the same as interfaces in C#, as a class cannot share the same name (in the same namespace) as an interface it implements (or any other type, for that matter).&lt;/p&gt;  &lt;p&gt;In C# meanwhile, the class is a bit simpler to define:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public class MyClass        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string SayGoodnightGracie()         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;Good night, Gracie!&amp;quot;;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now I’ll temporarily deviate from the book’s order to cover protocols, because they are what is equivalent to C#’s (or rather the CLI’s) interfaces. Like I mentioned, an Objective-C protocol is equivalent to a C# interface. Often you may read/hear Objective-C developers refer to protocols as delegates, as they are often used in a &lt;a href="http://en.wikipedia.org/wiki/Delegation_pattern"&gt;delegation pattern&lt;/a&gt; by Cocoa types, but a protocol itself does not innately imply delegation. If we wanted to create a protocol for the MyClass above, here’s what it might look like in Objective-C:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@protocol ISayGoodnightGracie &amp;lt;NSObject&amp;gt;        &lt;br /&gt;@required         &lt;br /&gt;- (NSString*) sayGoodnightGracie;         &lt;br /&gt;@end&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Notice that the @required keyword means that any signatures following it must be implemented in the concrete types – there is no equivalent to this in C#. If a C# interface defines a signature, it &lt;em&gt;must&lt;/em&gt; be implemented by the inheriting type.&lt;/p&gt;  &lt;p&gt;Now, in order for MyClass to implement ISayGoodnightGracie, we use a special syntax using angle brackets. Also, we remove the signature from the @interface since it is now defined in the @protocol:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@interface MyClass : NSObject &amp;lt;ISayGoodnightGracie&amp;gt;        &lt;br /&gt;@end         &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas, courier new"&gt;       &lt;br /&gt;@implementation MyClass         &lt;br /&gt;- (NSString*) sayGoodnightGracie {         &lt;br /&gt;return @&amp;quot;Good night, Gracie!&amp;quot;;         &lt;br /&gt;}         &lt;br /&gt;@end&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;To translate this to C#, we define a new public interface called ISayGoodnightGracie, and make MyClass inherit from it using a colon delimiter:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public interface ISayGoodnightGracie        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string SayGoodnightGracie();         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public class MyClass : ISayGoodnightGracie        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string SayGoodnightGracie()         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;Good night, Gracie!&amp;quot;;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Type Checking&lt;/h2&gt;  &lt;p&gt;Now, back to where the book left off. Next up is type checking – or, checking whether an instance inherits from a given type in its inheritance heirarchy.&lt;/p&gt;  &lt;p&gt;You might think this is similar to .NET’s Reflection, and it is, to a small extent. Each class in Objective-C has a corresponding Class instance, which is roughly equivalent to the System.Type type in .NET. You can get a class’ Class instance in Objective-C like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;Class c = [MyClass class];&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Which is mostly equivalent to the typeof operator in C#:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;Type t = typeof(MyClass);&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;One major difference, however, is that you can send messages to static (class) methods to that “c” variable above – something you cannot do in C# to the “t” variable above. &lt;/p&gt;  &lt;p&gt;You can also get the Class of a given object instance using the same “class” method:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;MyClass* m = // something or other...        &lt;br /&gt;Class c = [m class];&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And in C#, this is equivalent to the Object.GetType instance method:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;MyClass m = new MyClass();        &lt;br /&gt;Type t = m.GetType();&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Again, the same limitation of static method calls in C# applies.&lt;/p&gt;  &lt;p&gt;Now that you can see how you can get type information, hopefully that will help in understanding determining inheritance. Objective-C has a way of checking to see if an object instance inherits from a type, using the isKindOfClass method, defined like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;- (BOOL)isKindOfClass:(Class)aClass&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In the book, it is used like this, in an if statement:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;if ([someObject isKindOfClass: [MyClass class]]) // ...&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;C# has a few methods to determine inheritance. The most direct translation of the method above would be to use the “is” keyword:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;if (someObject is MyClass) //...&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This will work for exact matches as well as looking up the inheritance heirarchy, just like isKindOfClass does. You can use isMemberOfClass for exact matches in Objective-C, while you can use someObject.GetType() == typeof(MyClass) in C# to do an exact comparison.&lt;/p&gt;  &lt;h2&gt;[[MyClass alloc] init] and Initializers&lt;/h2&gt;  &lt;p&gt;Repeatedly in Objective-C you will see code equivalent to the following, known as the “alloc-init pattern”:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;MyClass* m = [[MyClass alloc] init];&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The inner message allocates space in memory to hold an instance of MyClass based on its known size, while the init message call performs any default initialization of instance variables or other initialization tasks. It then returns a pointer to the newly created object. According to the book, you will almost never want to separate these out into separate lines (i.e. storing just the result of alloc to a variable, then calling init), and as I’m just learning Objective-C, I can’t foresee a situation where you would need to. (I would appreciate if someone could outline for me an example where you might want to break with this pattern and do so – as well as what the dangers would be.)&lt;/p&gt;  &lt;p&gt;In C#, the allocation of a new instance of a type is accomplished with the “new” keyword, and which constructor (equivalent to initializers in Objective-C) to call is determined by the parameters following the type name. The “init” message above is a default, parameterless initializer, which is much like a default, parameterless constructor in C#. Like I mentioned in the previous post, the pointers are implicitly managed for you, so you do not need the asterisk. The Objective-C example above would be equivalent to the following in C#:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;MyClass m = new MyClass();&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You will find that there are parameter-based initializers as well, such as NSArray’s initWithObjects initializer:&lt;/p&gt;  &lt;blockquote&gt;&lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;NSArray* pep = [[NSArray alloc] initWithObjects:@&amp;quot;Manny&amp;quot;, @&amp;quot;Moe&amp;quot;, @&amp;quot;Jack&amp;quot;, nil];&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This initializer would be equivalent to the following constructor, if you recreated the NSArray type in C# and didn’t reuse the BCL array type:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public NSArray(params object[] objects) { … }&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Which would be called like this:&lt;/p&gt;  &lt;blockquote&gt;&lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;NSArray pep = new NSArray(&amp;quot;Manny&amp;quot;, &amp;quot;Moe&amp;quot;, &amp;quot;Jack&amp;quot;);&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;One major difference between Objective-C initializers and C# constructors is that C# (or rather .NET’s CLI) doesn’t use different identifiers for different constructors, they are merely method overloads if you have more than one – whereas the above Objective-C “initWithObjects” is clearly a different identifier than just “init”.&lt;/p&gt;  &lt;p&gt;Also note in this example that variable-length arguments must end with nil as a terminator in Objective-C, but that is not required in C#.&lt;/p&gt;  &lt;p&gt;There also is the concept of default initializers in Objective-C, where other initializers must ultimately call the default one – but there is no such requirement amongst instance constructor overloads in C#, although it is possible with constructor chaining:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public MyArray(params object[] objects) { /* ... */ } &lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public MyArray(List&amp;lt;object&amp;gt; objects) : this(objects.ToArray()) { }&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In the above example, if you pass a List&amp;lt;object&amp;gt; into the MyArray constructor, it will pass the list as an array to the variable-length constructor overload.&lt;/p&gt;  &lt;h2&gt;Polymorphism&lt;/h2&gt;  &lt;p&gt;Polymorphism in Objective-C works very much like a C# developer would expect. You can implicitly cast an object to one of it’s inherited types upon assignment, like in this example from the book, where UIButton inherits from UIControl, which in turn inherits from UIView:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];        &lt;br /&gt;UIView* v = b;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If these types existed in .NET, the previous code might look like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;UIButton b = UIButton.ButtonWithType(UIButtonType.UIButtonTypeRoundedRect);        &lt;br /&gt;UIView v = b;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Both examples demonstrate the implicit conversion between a child class and a class higher up in its inheritance heirarchy. Much like C#, in Objective-C you can’t call a method on a variable of the higher class (UIView) that is defined in the lower class (UIButton) without an explicit cast (except without ARC enabled, in which case you’ll get a compiler warning). So the following would be illegal Objective-C with ARC enabled:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];        &lt;br /&gt;UIView* v = b;         &lt;br /&gt;[v setTitle:@&amp;quot;Howdy!&amp;quot; forState:UIControlStateNormal];&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;But if you cast V to UIButton, it does work:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];        &lt;br /&gt;UIView* v = b;         &lt;br /&gt;[(UIButton*)v setTitle:@&amp;quot;Howdy!&amp;quot; forState:UIControlStateNormal];&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Likewise, here’s an illegal C# example of the above code:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;UIButton b = UIButton.ButtonWithType(UIButtonType.UIButtonTypeRoundedRect);        &lt;br /&gt;UIView v = b;&lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;v.SetTitle(&amp;quot;Howdy!&amp;quot;, UIControlState.UIControlStateNormal);&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;But the following is legal, since we’ve cast v to the type that defines SetTitle:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;UIButton b = UIButton.ButtonWithType(UIButtonType.UIButtonTypeRoundedRect);        &lt;br /&gt;UIView v = b;&lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;((UIButton)v).SetTitle(&amp;quot;Howdy!&amp;quot;, UIControlState.UIControlStateNormal);&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Self vs This and Super vs Base&lt;/h2&gt;  &lt;p&gt;Objective-C’s “self” keyword behaves very similarly to C#’s “this” keyword. In a simple, single-level class, the self/this keywords refer to the current instance of the class that the method is being executed on. In a parent/child class example, if you refer to the self/this keyword in a parent-level method, it can refer to an instance of the child class. This applies to virtual method calls in C#, much as it does overridden methods in Objective-C. &lt;/p&gt;  &lt;p&gt;In the book, he uses an example where a parent class Dog has a child class Basenji, where the parent class Dog’s “bark” method is overridden to return an empty string in the child Basenji class, because Basenji’s can’t bark. Yet, when the parent class’ “speak” method calls [self bark], the “self” is referring to the most inherited version of the method for the current instance, not the “bark” method on the parent class:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@implementation Dog        &lt;br /&gt;- (NSString*) bark {         &lt;br /&gt;return @&amp;quot;Woof!&amp;quot;;         &lt;br /&gt;}         &lt;br /&gt;- (NSString*) speak {         &lt;br /&gt;return&lt;strong&gt; [self bark];&lt;/strong&gt;         &lt;br /&gt;}         &lt;br /&gt;@end         &lt;br /&gt;@implementation Basenji : Dog         &lt;br /&gt;- (NSString*) bark {         &lt;br /&gt;return @&amp;quot;&amp;quot;; // empty string, Basenjis can't bark         &lt;br /&gt;}         &lt;br /&gt;@end         &lt;br /&gt;// [so, in some other class...]         &lt;br /&gt;Basenji* b = [[Basenji alloc] init];         &lt;br /&gt;NSString* s = [b speak];&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This behaves the same in C# with virtual method calls:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public class Dog        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public virtual string Bark()         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;Woof!&amp;quot;;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;&amp;#160;&amp;#160;&amp;#160; public string Speak()        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return this.Bark();         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public class Basenji : Dog        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public override string Bark()         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;&amp;quot;;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;}         &lt;br /&gt;&lt;/span&gt;&lt;span style="font-family: consolas, courier new"&gt;       &lt;br /&gt;// somewhere later on...         &lt;br /&gt;Basenji b = new Basenji();         &lt;br /&gt;string s = b.Speak();&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Just like in Objective-C, this C# code would store an empty string to the s variable, because calling “this.Bark()” will call Basenji’s overridden method.&lt;/p&gt;  &lt;p&gt;Much like self/this, the Objective-C “super” keyword is analogous to C#’s “base” keyword, and calls to parent class methods (like virtual methods in C# calling the base version) using the super/base keyword work much the same way. &lt;/p&gt;  &lt;h2&gt;Properties&lt;/h2&gt;  &lt;p&gt;Objective-C and C# share a powerful feature that Java still lacks – properties. Properties allow you to encapsulate a field with getter and setter methods while still allowing it to feel like you’re working with a field rather than methods. To define a property in Objective-C, you use the @property keyword in your interface:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@property (nonatomic, retain) NSString* MyProperty;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If you want to define the getter and setter methods yourself, you can use the @dynamic keyword in your implementation. (I’m a little fuzzy on this, so please someone correct me if I’m on the wrong track.)&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@implementation MyClass {        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; @private NSString* _myProperty;         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@dynamic MyProperty;&lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;- (NSString*) MyProperty {        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return _myProperty;         &lt;br /&gt;}         &lt;br /&gt;- (void) setMyProperty:(NSString*)value {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; self-&amp;gt;_myProperty = value;         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@end&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The @dynamic keyword tells the compiler that you will supply get and set methods for the property of MyProperty. The equivalent in C# (including the @property declaration) would be:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public class MyClass        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; private string _myProperty;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string MyProperty         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; get { return _myProperty; }         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; set { _myProperty = value; }         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;As an alternative to the @dynamic keyword with properties, you can use the @synthesize keyword to tell the compiler to automatically create getter/setter methods for you, as well as a backing field.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@implementation MyClass        &lt;br /&gt;@synthesize MyProperty;         &lt;br /&gt;@end&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In C#, there is the automatic property syntax to do the same thing:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;public class MyClass        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public string MyProperty { get; set; }         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;One feature that Objective-C has that C# lacks is the ability to automatically create get/set methods around a named private field that you have access to. C# will automatically create a backing field for each property, but it is given a special name by the compiler and you do not have access to it without some heavy (and largely unnecessary) reflection. To use this feature of Objective-C, add the private field name with an equals sign after the property name you’re synthesizing:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: consolas, courier new"&gt;@synthesize MyProperty = _myProperty;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;It would be great if C# would add equivalent functionality in the future, but that doesn’t appear to be on the roadmap.&lt;/p&gt;  &lt;p&gt;Also of note is that C# lacks property attributes – in the interface example, the attributes are “nonatomic, retain”. These attributes signal to the compiler how to treat the accessor methods – nonatomic signals that the get/set methods should not be treated in a thread-safe manner and any thread safety should be left up to the implementation method if needed, and retain says that the passed-in variable should be retained before setting the instance variable. Since these (and other) property attributes have no C# equivalent, I’ll leave them for another day.&lt;/p&gt;  &lt;h2&gt;Summary&lt;/h2&gt;  &lt;p&gt;Hopefully this has shed some more light on how Objective-C and C# are really not all that different if you can apply a little bit of translation in your head while reading/writing code. If you live near Jacksonville, I hope you’ll come out to &lt;a href="http://adventuresdotnet.blogspot.com/2012/05/im-speaking-at-jaxdug-june-6th-2012.html"&gt;my presentation on iPhone Development with a Windows Azure-hosted API at JaxDUG on June 6th&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;Please give me feedback on these posts in the comments. Thanks!&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/1915749823483806653/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=1915749823483806653" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/1915749823483806653?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/1915749823483806653?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2012/05/translating-objective-c-to-cpart-2-of-n.html" title="Translating Objective-C to C# – Part 2 of n" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUQCQX0-fCp7ImA9WhVUGEo.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-3996480151170069799</id><published>2012-05-22T17:33:00.001-04:00</published><updated>2012-05-24T11:29:20.354-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-24T11:29:20.354-04:00</app:edited><title>Translating Objective-C to C# – Part 1 of n</title><content type="html">&lt;p&gt;As I mentioned in the &lt;a href="http://adventuresdotnet.blogspot.com/2012/05/im-speaking-at-jaxdug-june-6th-2012.html"&gt;previous post&lt;/a&gt;, I’m copresenting at JaxDUG in June on iPhone Development using a Windows Azure-hosted REST API. Before last week, I knew little about Objective-C or Cocoa. I still know very little – but I gained a wealth of knowledge by sitting down with my copresenter Mike Glass for about half an hour and he explained some of the concepts for me and it finally clicked. So I’ve spent the past 2 days reading through &lt;a href="http://shop.oreilly.com/product/0636920023562.do"&gt;Programming iOS 5, Second Edition&lt;/a&gt; by Matt Neuberg to familiarize myself with the concepts. &lt;/p&gt;  &lt;p&gt;While reading, I found that it was helpful to try and equate the concepts to C# equivalents. I figured that it might be helpful to others as well if I put these comparisons into blog form and shared them to the world. Hopefully this will encourage iOS and Mac developers to take a look at Windows Phone and Windows 8 development in C#. As I’m still learning, I’d appreciate any feedback you might have, and definitely let me know if I’ve said something wrong.&lt;/p&gt;  &lt;p&gt;I’ll be using the examples in the eBook as the source, in the order they appear (but not using every example, of course).&lt;/p&gt;  &lt;h2&gt;Enums&lt;/h2&gt;  &lt;p&gt;In Chapter 1, he shows a common C enum type defined in Cocoa:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;typedef enum {        &lt;br /&gt;UIStatusBarAnimationNone,         &lt;br /&gt;UIStatusBarAnimationFade,         &lt;br /&gt;UIStatusBarAnimationSlide,         &lt;br /&gt;} UIStatusBarAnimation;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This is easily convertible to a C# enum:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;public enum UIStatusBarAnimation        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; UIStatusBarAnimationNone,         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; UIStatusBarAnimationFade,         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; UIStatusBarAnimationSlide         &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Structs&lt;/h2&gt;  &lt;p&gt;Just like enums, the C-style structs are easy to convert to C# structs.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;struct CGPoint {        &lt;br /&gt;CGFloat x;         &lt;br /&gt;CGFloat y;         &lt;br /&gt;};         &lt;br /&gt;typedef struct CGPoint CGPoint;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And in C#:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;public struct CGPoint        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public CGFloat x;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public CGFloat y;         &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Strings&lt;/h2&gt;  &lt;p&gt;Strings in Objective-C are treated very similarly to C#, in that the resulting type of a string literal is an actual reference-type class instance.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;NSString* s = @&amp;quot;Hello, world!&amp;quot;;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In C#, there is no @ delimiter for the start of the string, and the pointers are implicitly managed by the runtime, so there’s no need to be explicit with the * identifier. The NSString type is analogous to System.String.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;string s = &amp;quot;Hello, world!&amp;quot;;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;For Each&lt;/h2&gt;  &lt;p&gt;Objective-C has an equivalent to C#’s foreach statement, using the more concise “for” and “in” keywords:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;for (SomeType* oneItem in myCollection) {        &lt;br /&gt;// ... statements ....         &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Again, the pointers are implicitly managed, but this is easily done in C# using foreach:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;foreach (SomeType oneItem in myCollection)        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // ... statements ...         &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;Instance vs Static Methods/Messages&lt;/h2&gt;  &lt;p&gt;In Objective-C, any time you see a statement in square brackets, that is called passing a message to an object, which is roughly equivalent to calling a method in C#. The first part inside the brackets is the object to pass the message to, followed by the “selector” (or method signature) and their parameters. Each parameter follows a colon, which makes the code more readable than C# in some cases.&lt;/p&gt;  &lt;p&gt;In the interface definition for a type (more on that later), or in Apple’s documentation, you will see instance methods defined with a minus sign (“-“) and class (or “static” in C#) methods defined with a plus sign (“+”). So the following method is defined as an instance method:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;- (NSString *)stringByAppendingString:(NSString *)aString&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;While this method is defined as a static method:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;+ (id)stringWithContentsOfFile:(NSString *)path        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; encoding:(NSStringEncoding)enc         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; error:(NSError **)error&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The C# equivalent signatures would be (replacing types with those found in the .NET BCL):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;public string StringByAppendingString(string aString)        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // ...         &lt;br /&gt;}&lt;/p&gt;    &lt;p style="font-family: Consolas, Courier New"&gt;public static object StringWithContentsOfFile(string path, Encoding enc, ref Exception error)        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; // ...         &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;More on id vs object later. Notice that a “pointer pointer” to a reference type is roughly equivalent to the “ref” keyword on a parameter. And clearly, there are static BCL equivalents of these methods in String.Concat(string, string) and File.ReadAllText(string, Encoding) wrapped in a try/catch block.&lt;/p&gt;  &lt;p&gt;To call an instance method in Objective-C, you use an object instance as the first part inside the square brackets:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;NSString* s1 = @&amp;quot;Hello, &amp;quot;;        &lt;br /&gt;NSString* s2 = @&amp;quot;World!&amp;quot;         &lt;br /&gt;NSString* s3 = [s1 stringByAppendingString: s2];&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If the System.String type had a StringByAppendingString method like NSString (rather than the static String.Concat), the previous code would look like this in C#:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;string s1 = &amp;quot;Hello, &amp;quot;;        &lt;br /&gt;string s2 = &amp;quot;World!&amp;quot;;         &lt;br /&gt;string s3 = s1.StringByAppendingString(s2);&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Just like the s1 instance was specified inside the square brackets to make it an instance method call, you can specify the type name instead to make it a static method call. This is almost exactly like C#, where you use the Type identifier before the dot instead of the instance. Here’s a static method call in Objective-C:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;NSString* myPath = // something or other;        &lt;br /&gt;NSStringEncoding myEnc = // something or other;         &lt;br /&gt;NSError* myError = nil;         &lt;br /&gt;NSString* result = [NSString stringWithContentsOfFile: myPath         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; encoding: myEnc         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; error: &amp;amp;myError];&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;We can create our own .NET version of NSString’s stringWithContentsOfFile as follows:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;public static class StringEx        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; public static string StringWithContentsOfFile(string path, Encoding encoding, ref Exception error)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; try         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return System.IO.File.ReadAllText(path, encoding);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; catch (Exception ex)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; error = ex;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return null;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }         &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And now that we have a Cocoa equivalent, we can call it like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;string myPath = &amp;quot;something or other&amp;quot;;        &lt;br /&gt;Encoding myEnc = Encoding.UTF8;         &lt;br /&gt;Exception myError = null;&lt;/p&gt;    &lt;p style="font-family: Consolas, Courier New"&gt;string result = StringEx.StringWithContentsOfFile(myPath, encoding: myEnc, error: ref myError);&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font face="Georgia"&gt;Also notice that nil in Objective-C is roughly equivalent to null in C#, and that in this example we are giving our code a very Objective-C feel by using named parameters with colons.&lt;/font&gt;&lt;/p&gt;  &lt;h2&gt;Nesting Method Calls&lt;/h2&gt;  &lt;p&gt;One thing that threw me off when I started looking at Objective-C is following the trail of nested method calls. Since the first part of a method call is what the message is being sent to, you can use the result of a message as the first part of another message call. In Chapter 3 he uses the following example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;NSArray* arr = [[MPMediaQuery albumsQuery] collections];&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And he points out that this is equivalent to the following code:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;MPMediaQuery* query = [MPMediaQuery albumsQuery];        &lt;br /&gt;NSArray* arr = [query collections];&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;A C# (or any C-style language) developer should be able to easily see that the first version is equivalent to this (although in reality, Array would be replaced with some generic collection):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;Array arr = MPMediaQuery.albumsQuery().collections();&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Also notice that this is a mix of a static/class and instance method call in both cases, but either example could be made from 2 nested instance method calls without a change in syntax (other than using the instance identifier rather than MPMediaQuery).&lt;/p&gt;  &lt;h2&gt;Parameter Lists&lt;/h2&gt;  &lt;p&gt;For methods with a variable number of parameters, Objective-C offers parameter lists, which is equivalent to C#’s params keyword. From the examples in the book, on the NSArray class there is a class (static) method with the following signature:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;+ (id)arrayWithObjects:(id)firstObj, ... ;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The … is the syntax for denoting that the method can receive a variable number of arguments. He then uses this method like so:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;NSArray* pep = [NSArray arrayWithObjects:@&amp;quot;Manny&amp;quot;, @&amp;quot;Moe&amp;quot;, @&amp;quot;Jack&amp;quot;, nil];&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;He points out in the book that the nil terminator is required, and you will get a compiler warning if you forget it. In C#, the NSArray method is equivalent to the following method:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;public static Array ArrayWithObjects(params object[] objects)        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return objects;         &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Since the C# compiler automatically converts variable length arguments into an array instance, this method has a trivial implementation. It can then be called like so (from within the same type), and a nil terminator is not required:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;Array pep = ArrayWithObjects(&amp;quot;Manny&amp;quot;, &amp;quot;Moe&amp;quot;, &amp;quot;Jack&amp;quot;);&lt;/p&gt; &lt;/blockquote&gt;  &lt;h2&gt;ID vs Object&lt;/h2&gt;  &lt;p&gt;This one is a bit tricky. Objective-C has an id keyword which can be used instead of a type identifier to declare a variable or parameter as accepting a pointer to any type of object ever. In C#, you might think this would be equivalent to declaring a variable of the object type. However, my CoWork Jax coworker Mike Glass pointed out that Cocoa also has an equivalent of System.Object, which is NSObject – and all classes that inherit the default allocation and initialization behavior of objects should inherit from it. So if declaring a variable of type System.Object in C# is equivalent to declaring a variable of type NSObject in Objective-C, then what is the .NET equivalent of declaring a variable as id?&lt;/p&gt;  &lt;p&gt;It seems to me like there is no direct equivalent. On one hand, id allows you to send messages to that instance that are checked at runtime rather than compile time. So the following code is not statically checked by the compiler, and may crash at runtime:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;NSString* s = @&amp;quot;Hello, world!&amp;quot;;        &lt;br /&gt;id unknown = s;         &lt;br /&gt;[unknown rockTheCasbah];&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In that sense, you can see where id is not like System.Object, because trying to call rockTheCasbah on a System.Object variable would definitely throw a compiler error. So that makes id behave much like C# 4’s dynamic keyword – it can contain a reference to any object, and method calls at runtime are not statically checked by the compiler.&lt;/p&gt;  &lt;p&gt;However, id in Objective-C also behaves much like the IntPtr type which in .NET refers to an unmanaged memory pointer. I would chalk this up to the difference between Objective-C being an unmanaged language and .NET being a managed language though, and leave the rest of this discussion for one to be had over beers. Bottom line – id seems to be most equivalent to C#’s dynamic keyword (but without the expensive DLR overhead), and NSObject is equivalent to System.Object; but in a true Objective-C to C# conversion you would probably use System.Object for id rather than dynamic.&lt;/p&gt;  &lt;h2&gt;Blocks&lt;/h2&gt;  &lt;p&gt;Blocks in Objective-C are very easily translatable to lambda expressions in C#. A Block, just like a lambda expression, is an anonymous method that has a special syntax, can be stored to a variable inside another method or passed as a parameter to another method, has a strongly typed parameter list, and has an implicitly typed return type. Blocks in Objective-C are declared by starting the block with the caret (^) symbol. In the example in the book, it is passed as a parameter to NSArray’s sortedArrayUsingComparator method (the block portion is in&lt;strong&gt;&lt;font style="background-color: #cccccc"&gt; bold with a gray background&lt;/font&gt;&lt;/strong&gt;):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;NSArray* arr2 = [arr sortedArrayUsingComparator:&lt;font style="background-color: #cccccc"&gt; &lt;/font&gt;&lt;strong&gt;&lt;font style="background-color: #cccccc"&gt;^(id obj1, id obj2) {            &lt;br /&gt;NSString* s1 = (NSString*) obj1;             &lt;br /&gt;NSString* s2 = (NSString*) obj2;             &lt;br /&gt;NSString* string1end = [s1 substringFromIndex:[s1 length] - 1];             &lt;br /&gt;NSString* string2end = [s2 substringFromIndex:[s2 length] - 1];             &lt;br /&gt;return [string1end compare:string2end];             &lt;br /&gt;}&lt;/font&gt;&lt;/strong&gt;];&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Compare that to the following lambda expression equivalent, assuming there was an instance or extension method on System.Array called SortedArrayUsingComparator (and the comparator parameter could be the delegate Comparison&amp;lt;object&amp;gt;, which takes 2 object types and returns an int):&lt;/p&gt;  &lt;blockquote&gt;   &lt;p style="font-family: Consolas, Courier New"&gt;Array arr2 = arr.SortedArrayUsingComparator((obj1, obj2) =&amp;gt;        &lt;br /&gt;{         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string s1 = (string)obj1;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string s2 = (string)obj2;         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string string1end = s1.Substring(s1.Length - 1);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; string string2end = s2.Substring(s2.Length - 1);         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; return string.Compare(string1end, string2end);         &lt;br /&gt;});&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This example shows that Blocks and C# lambdas are extremely similar, and have very similar syntax and usage. Note that I’ve used the static string.Compare method rather than NSString’s instance compare method as in the original example. Also, the original example is implicitly returning NSComparisonResult rather than int, but with similar results.&lt;/p&gt;  &lt;h2&gt;Summary&lt;/h2&gt;  &lt;p&gt;I hope this shows how you can translate some Objective-C to and from C#. I’m still learning this myself, so please give me any feedback you can. Also, make sure you pick up a copy of &lt;a href="http://shop.oreilly.com/product/0636920023562.do"&gt;Programming iOS 5&lt;/a&gt; which these examples came from – it’s an excellent read!&lt;/p&gt;  &lt;p&gt;Next time I hope to cover interfaces vs. implementations, type checking, inheritance, alloc-init, polymorphism, self/this and super/base keywords, properties, and initializers from Chapters 4-5.&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/3996480151170069799/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=3996480151170069799" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3996480151170069799?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3996480151170069799?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2012/05/translating-objective-c-to-c-part-1-of.html" title="Translating Objective-C to C# – Part 1 of n" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;DkAMR3s5fip7ImA9WhVUFkQ.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-8830731905794955703</id><published>2012-05-22T09:53:00.001-04:00</published><updated>2012-05-22T09:53:06.526-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-22T09:53:06.526-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="rest" /><category scheme="http://www.blogger.com/atom/ns#" term="mvc" /><category scheme="http://www.blogger.com/atom/ns#" term="jaxdug" /><category scheme="http://www.blogger.com/atom/ns#" term="azure" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="web api" /><category scheme="http://www.blogger.com/atom/ns#" term="json" /><category scheme="http://www.blogger.com/atom/ns#" term="iOS" /><title>I’m Speaking at JaxDUG! June 6th, 2012</title><content type="html">&lt;p&gt;2012 is all about a reboot of my professional life. I’ve &lt;a href="http://adventuresdotnet.blogspot.com/2012/03/yet-another-adventure.html"&gt;changed jobs&lt;/a&gt;, I’ve finally put up &lt;a href="http://adventuresdotnet.blogspot.com/2012/05/working-with-salesforcecom-observations.html"&gt;a new blog post&lt;/a&gt;, and now I’m finally speaking at a developer group again! &lt;/p&gt;  &lt;p&gt;This time it’s at the monthly &lt;a href="http://www.jaxdug.net/"&gt;JaxDUG&lt;/a&gt; meeting, happening Wednesday, June 6th, 2012 from 6:00 pm to 8:00 pm at the LPS building auditorium, ground floor.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;TL;DR Version&lt;/strong&gt;: &lt;a href="http://jaxdug6-6-12-esli.eventbrite.com/"&gt;register for the JaxDUG meeting&lt;/a&gt; and I’ll see you there!&lt;/p&gt;  &lt;p&gt;(By the way, if you haven’t checked out the new &lt;a href="http://www.jaxdug.net/"&gt;JaxDUG website&lt;/a&gt;, do so now! It looks great, and it’s developed by our community, so hopefully it will grow and thrive into an awesome resource. At the time of this writing, though, they still do not have the June meeting listed on the site.)&lt;/p&gt;  &lt;p&gt;The most exciting thing about this meeting is that I’m not the only one speaking – I’ll be copresenting with Mike Glass, who works out of &lt;a href="http://www.coworkjax.com/"&gt;CoWork Jax&lt;/a&gt; downtown where I work a few days a week. Mike is a Mac and iOS developer who writes probably the cleanest and most readable Objective-C code I’ve ever seen. His company, &lt;a href="http://www.pocketsevens.net/"&gt;Pocket Sevens&lt;/a&gt;, has produced some really awesome apps.&lt;/p&gt;  &lt;p&gt;Our topic will be iPhone App Development using an Azure REST API. Mike will demonstrate how to use the new Storyboard feature in XCode/iOS 5 to easily create a 3-screen iPhone app that will load data from a REST API hosted in Windows Azure. Then, I’ll be demonstrating how to use ASP.NET MVC 4’s Web API feature to easily create a REST API that supports XML as well as JSON, and how to host that in Azure. I’ll then hand it back to Mike and he’ll tie everything together and deploy the app to the emulator. He’ll also explain some Objective-C concepts (that I had to have him explain to me!) like delegates (not the same as .NET delegates!), property synthesizing (roughly analogous to automatic properties in .NET), and how the controllers interact with the views when you use Storyboards. We also might have a few surprises in store!&lt;/p&gt;  &lt;p&gt;The app that we’ll be creating will be a directory of Coworking locations, sorted based on proximity to your current position. It will take your current GPS position, send it to the REST API and get back a list of coworking spaces, and bind that list to a table view, and when a record in the table is selected, it will show a detail view with a map for that entry.&lt;/p&gt;  &lt;p&gt;(If you’re not familiar with Coworking, it’s a new trend of common, shared office space where workers from many different businesses work out of the same space, collaborate, and network. I’ve been working out of CoWork Jax since mid-April, and I love it! It’s great to meet and talk with people that work for other companies, and you can help each other out with problems you might have.)&lt;/p&gt;  &lt;p&gt;To register for the meeting, &lt;a href="http://jaxdug6-6-12-esli.eventbrite.com/"&gt;sign up for free at Eventbrite&lt;/a&gt;. There’s no need to print your ticket. &lt;/p&gt;  &lt;p&gt;Hope to see you there!&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/8830731905794955703/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=8830731905794955703" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/8830731905794955703?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/8830731905794955703?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2012/05/im-speaking-at-jaxdug-june-6th-2012.html" title="I’m Speaking at JaxDUG! June 6th, 2012" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CU8CRH47fyp7ImA9WhVVF0s.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-2090400503401881400</id><published>2012-05-11T15:09:00.001-04:00</published><updated>2012-05-11T15:17:45.007-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-11T15:17:45.007-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="salesforce" /><title>Working with SalesForce.com – Observations and Frustrations</title><content type="html">&lt;p&gt;As I mentioned in &lt;a href="http://adventuresdotnet.blogspot.com/2012/03/yet-another-adventure.html"&gt;Yet Another Adventure&lt;/a&gt;, I started work at a new company in April – &lt;a href="http://www.pottsconsultinggroup.com/"&gt;Potts Consulting Group&lt;/a&gt;. It’s been a wild ride leaving &lt;a href="http://www.fanatics.com/"&gt;Fanatics&lt;/a&gt; and getting acquainted with consulting! I love every minute of it, and it was a great breath of fresh air changing jobs.&lt;/p&gt;  &lt;p&gt;My first client job was a &lt;a href="http://www.salesforce.com/"&gt;SalesForce.com&lt;/a&gt; integration. The client had domain-specific data that needed to be transformed into SalesForce.com (hereafter referred to in shorthand as SFDC) opportunities, accounts, and contacts. This integration is in its final phases now, and along the way I learned some very cool and very frustrating things about working with the SFDC APIs.&lt;/p&gt;  &lt;p&gt;(Note: For the rest of this article, I will assume you have some familiarity with SFDC’s various APIs. You may have come across this post via searching for answers on a search engine, and if so, hopefully I can shed some light on the many “gotchas” you will encounter.)&lt;/p&gt;  &lt;p&gt;SFDC has a few APIs to use for loading data, including the &lt;a href="http://www.salesforce.com/us/developer/docs/api/index_Left.htm#StartTopic=Content/sforce_api_quickstart_steps.htm"&gt;SOAP Web Services API&lt;/a&gt;, the &lt;a href="http://www.salesforce.com/us/developer/docs/api_asynch/index_Left.htm#StartTopic=Content/asynch_api_quickstart.htm"&gt;Bulk API&lt;/a&gt;, and the &lt;a href="http://www.salesforce.com/us/developer/docs/dataLoader/index.htm"&gt;Data Loader application&lt;/a&gt;, which uses either the SOAP or Bulk API through a command-line interface. The Data Loader also has a GUI, but that is not helpful for scheduled loading of data – but it turned out to be very valuable when debugging issues with the CLI version.&lt;/p&gt;  &lt;p&gt;I initially looked at the SOAP API, thinking it would be the easiest to integrate with a .NET application. That turned out to be far from the truth – adding a service reference to their SOAP endpoint worked, but when the code was executed I could not get past XML deserialization errors. After a little bit of trial and error, I decided to pursue the Bulk API.&lt;/p&gt;  &lt;p&gt;The bulk API was more what I was looking for, but required a good deal of manual work for mapping. Since the Data Loader can use the bulk API and handles the mapping for you, I eventually ended up on the decision to use the CLI version of the Data Loader.&lt;/p&gt;  &lt;p&gt;I created a Windows Service application that, based on a schedule or manual invocation, will pass to the Data Loader the information needed to load the data into SFDC. This was invoked by using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx"&gt;System.Diagnostics.Process&lt;/a&gt; class.&lt;/p&gt;  &lt;p&gt;The Data Loader is a Java application that can load data either from CSV files or from a database query. Since my source data was already in SQL Server 2008, I decided to use the database query approach rather than CSV. This brings us to the first frustration.&lt;/p&gt;  &lt;h2&gt;The SQL Server JDBC Driver&lt;/h2&gt;  &lt;p&gt;In order to have the database queries from the Data Loader access Microsoft SQL Server, you must use the &lt;a href="http://msdn.microsoft.com/en-us/sqlserver/aa937724"&gt;SQL Server JDBC driver&lt;/a&gt; (and if you’re using SQL Server 2012, make sure you get the 4.0 version). However, this is not as easy as it appears – the driver is a .jar file that must be “included” when the Data Loader is run. To do so, you must manually modify the process.bat file in the Data Loader /bin/ folder to include the JDBC driver. I placed the SQL Server JDBC Driver .jar file in the /Java/lib/ folder so it was alongside the other .jar files. &lt;/p&gt;  &lt;p&gt;I modified the process.bat line that executes Java to look like the following (I also put absolute file paths there that weren’t there before):&lt;/p&gt;  &lt;p&gt;&amp;quot;C:\Program Files (x86)\salesforce.com\Data Loader\Java\bin\java.exe&amp;quot; -cp &amp;quot;C:\Program Files (x86)\salesforce.com\Data Loader\&lt;strong&gt;Java\lib\sqljdbc4.jar&lt;/strong&gt;;C:\Program Files (x86)\salesforce.com\Data Loader\dataloader-24.0.0-jar-with-dependencies.jar&amp;quot; -Dsalesforce.config.dir=%1 com.salesforce.dataloader.process.ProcessRunner %PROCESS_OPTION%&lt;/p&gt;  &lt;p&gt;Once the JDBC driver is included in the .bat file, you can modify your connection in the database-conf.xml file to look like the following:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;lt;bean id=&amp;quot;dbDataSource&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; class=&amp;quot;org.apache.commons.dbcp.BasicDataSource&amp;quot;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; destroy-method=&amp;quot;close&amp;quot;&amp;gt;      &lt;br /&gt;&amp;#160; &amp;lt;property name=&amp;quot;driverClassName&amp;quot; value=&amp;quot;&lt;strong&gt;com.microsoft.sqlserver.jdbc.SQLServerDriver&lt;/strong&gt;&amp;quot;/&amp;gt;      &lt;br /&gt;&amp;#160; &amp;lt;property name=&amp;quot;url&amp;quot; value=&amp;quot;jdbc:sqlserver://{server};database={database}&amp;quot;/&amp;gt;      &lt;br /&gt;&amp;#160; &amp;lt;property name=&amp;quot;username&amp;quot; value=&amp;quot;{username}&amp;quot;/&amp;gt;      &lt;br /&gt;&amp;#160; &amp;lt;property name=&amp;quot;password&amp;quot; value=&amp;quot;{password}&amp;quot;/&amp;gt;      &lt;br /&gt;&amp;lt;/bean&amp;gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And replace {server}, {database}, {username}, and {password} with the values that are correct for your database server.&lt;/p&gt;  &lt;h2&gt;Passing Custom Parameters to SQL&lt;/h2&gt;  &lt;p&gt;One thing that isn’t well documented in the Data Loader documentation is that you can pass custom parameters to your SQL query through the command line. All you have to do is add on the variables after the %PROCESS_OPTION%. Say you had a variable called CustomerID and you wanted to pass the customer ID value in through the command line as the third argument (after process name). You could add the following to the .bat line above after %PROCESS_OPTION%:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;CustomerID=%3&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Then you can specify a parameter in your database-conf.xml file’s query, for example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;SELECT {fields here} FROM Customers WHERE CustomerID = @CustomerID@&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Just make sure you add the CustomerID parameter in the sqlParams bean property:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;lt;property name=&amp;quot;sqlParams&amp;quot;&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;map&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;entry key=&amp;quot;CustomerID&amp;quot; value=&amp;quot;java.lang.String&amp;quot;/&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/map&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/property&amp;gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now you can execute your process.bat like this: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;process.bat ../configs/myconfig myLoadOperation 123&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And the value 123 will be inserted into your database query as the @CustomerID@ parameter.&lt;/p&gt;  &lt;h2&gt;Exponential Notation&lt;/h2&gt;  &lt;p&gt;If you are trying to load a number into a Number type field in SFDC, make sure the number will never exceed 10 million. After 10 million, the number values are sent in exponential notation and you will get an error. I was trying to load an integer identity column value into a Number type ExternalID field, and once the IDs reached 10 million I received errors. While not the most elegant solution, I had to get around this problem by making the ExternalID field a Text type instead and cast the int as a string in the database query.&lt;/p&gt;  &lt;h2&gt;No Default External ID field&lt;/h2&gt;  &lt;p&gt;As you may know, objects in SFDC have their own internal ID. In order to match a SFDC object to one in your database, you must store your ID in the SFDC object. Unfortunately there’s no default External ID field created for you, so you must create one for every object. Like in the past section, I recommend using the Text type as it will save you much frustration.&lt;/p&gt;  &lt;h2&gt;DatabaseWrite and Stored Procedures&lt;/h2&gt;  &lt;p&gt;One cool thing I discovered is that you can execute stored procedures for databaseWrite operations, which gives you greater flexibility by being able to have complex code inside a T-SQL stored procedure rather than your database-conf.xml file. This combined with the ability to pass in parameters from the command line makes for very powerful extractions of data from SFDC into your SQL database.&lt;/p&gt;  &lt;h2&gt;5MB Developer Account Restriction&lt;/h2&gt;  &lt;p&gt;An annoyance I discovered is that the free Developer account only allots you 5 MB of data usage. After loading a few thousand records I hit this limit very quickly, and once you hit it, you can not load any data before you delete something else to free up space. 5 MB is not nearly enough to exhaustively test an integration, so you can only really use your developer account for testing a small number of records to make sure the mapping is correct. I hope they increase this limit in the future.&lt;/p&gt;  &lt;h2&gt;Cannot Delete Opportunities In Bulk&lt;/h2&gt;  &lt;p&gt;Under the Setup section of the site, you can bulk delete accounts and contacts. However, for some reason, you can not bulk delete opportunities. When testing data loading, you need to repeatedly load bulk data then delete all that data so you can test again. But since there’s no easy way to delete opportunities in bulk on the site, you have to bulk delete them using the GUI version of Data Loader (or, I guess you really could use the CLI version, but that’d be painful) by extracting all the opportunities to CSV then performing a bulk delete with that extracted CSV so it knows which IDs to delete. Very frustrating for testing by trial and error (which, since SFDC mappings can be very flaky, is a common strategy)!&lt;/p&gt;  &lt;h2&gt;Null, Empty String, and Overwrites&lt;/h2&gt;  &lt;p&gt;If you pass NULL to a field mapping on an object that already has a value in that field, it will not overwrite the value. This is helpful if you want to run the integration multiple times with different columns each time. I was pleased that this was the case. However, I was surprised to also find that passing empty string (“”) to a Text field will not overwrite the value, either. So if a value is present at one point in time but you want to blank it out, you can not easily do that as far as I can tell using the Data Loader and the databaseRead operation. You could replace it with space (“ “), but that’s an inelegant solution. I would have preferred if NULL did not overwrite but empty string did overwrite the value.&lt;/p&gt;  &lt;h2&gt;Field Names in Mapping Files are Case Sensitive&lt;/h2&gt;  &lt;p&gt;When creating a .sdl mapping file, ensure that you log in to the Setup section of the site and view the API field names for the object you’re loading, and make sure the casing of the field name on the SFDC side is correct, as they are case sensitive. Despite them being case-sensitive on input, the Data Loader GUI application exports field names as all upper case. Go figure.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Well, that sums up my observations and frustrations with SalesForce.com. Hopefully my trials and aggravations have resulted in helpful information for someone else, as there is a lack of good information online about how to effectively use Data Loader for real world automation of SFDC imports.&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/2090400503401881400/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=2090400503401881400" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/2090400503401881400?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/2090400503401881400?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2012/05/working-with-salesforcecom-observations.html" title="Working with SalesForce.com – Observations and Frustrations" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;DUYDSX87eSp7ImA9WhVSEEk.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-190340173967602782</id><published>2012-03-06T10:19:00.002-05:00</published><updated>2012-03-06T10:19:38.101-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-03-06T10:19:38.101-05:00</app:edited><title>Yet Another Adventure</title><content type="html">I am exceptionally embarrassed that I only made one (ONE!) post in all of 2011. I sincerely apologize for that. I am not going to make another empty promise that I'll post more, but perhaps I'll make a declaration of my intention to post more in 2012.&lt;br /&gt;&lt;br /&gt;But I'm writing today to announce that I'm starting yet another adventure, not unlike my post from &lt;a href="http://adventuresdotnet.blogspot.com/2009/08/jacksonville-here-i-come.html"&gt;August 13, 2009 titled Jacksonville, Here I Come!&lt;/a&gt;. I have just accepted a new position at a small consulting company in St. Augustine, and I will be leaving my position at Fanatics on Friday, March 23, 2012. It's been a crazy two and a half years, which has given me experience with a highly scalable e-commerce application on two different platforms (one ASP.NET WebForms, the other ASP.NET MVC), as well as a wealth of additional experience in areas like web analytics, multivariant testing, performance testing, WCF and AppFabric, MSMQ, jQuery and AJAX, algorithms, metaprogramming, database tuning and advanced querying, and many, many more. I'm extremely grateful for the opportunity to make software solutions to keep up with the company's rapid growth and I will always look back on this position with good memories.&lt;br /&gt;&lt;br /&gt;I'm excited about the new position, and looking forward to using this new experience (and gaining even more) working on a wide variety of projects. Catch you on the flip side!</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/190340173967602782/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=190340173967602782" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/190340173967602782?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/190340173967602782?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2012/03/yet-another-adventure.html" title="Yet Another Adventure" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkAMQXw6eCp7ImA9WhZTEk4.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-3241881118181352756</id><published>2011-03-15T19:13:00.001-04:00</published><updated>2011-03-15T19:13:00.210-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-15T19:13:00.210-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="inversion of control" /><category scheme="http://www.blogger.com/atom/ns#" term="static" /><category scheme="http://www.blogger.com/atom/ns#" term="unit testing" /><category scheme="http://www.blogger.com/atom/ns#" term="dependency injection" /><category scheme="http://www.blogger.com/atom/ns#" term="c#" /><category scheme="http://www.blogger.com/atom/ns#" term="moles" /><category scheme="http://www.blogger.com/atom/ns#" term="moq" /><category scheme="http://www.blogger.com/atom/ns#" term="mocking" /><title>Mocking Static Methods for Unit Testing</title><content type="html">&lt;p&gt;As you probably know, unit tests are supposed to be fast, lightweight tests of your code logic. They are not supposed to do any disk I/O, network communication, or long-running CPU calculations. If you have cleanly decoupled code that doesn’t call any static methods and everything is programmed to the interface, you can mock the intensive operations very easily.&lt;/p&gt;  &lt;p&gt;Although if you’ve tried unit testing a class that calls static methods that perform such intensive operations, you quickly realize that you must separate them out from your code so that they can be mocked.&amp;#160; But if the methods are in a library you can’t modify, you usually end up very frustrated. There are a few easy ways to mock static methods, and you can choose which option works best for your needs.&lt;/p&gt;  &lt;h2&gt;Background&lt;/h2&gt;  &lt;p&gt;In our example, we have a class that has a single static method. Let’s pretend that this does some intensive activity that you don’t want to do while running your unit test suite. This class is in a library that you can’t modify and make non-static – we’re just looking at the code here as an example.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; SomeLibraryYouCantModify&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SomeStaticClass&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; SomeStaticMethod(&lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="rem"&gt;// Let's pretend this method hits a database or service.&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; !&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(input);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;So we have one method that takes a string and returns bool. Keep this in mind.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, here’s an example of the static method being used. Note that we can not (by default) mock the static call – it’s a tight coupling that can not be easily broken.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; SomeLibraryYouCanModify&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SomeUntestableClass&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; SomeMethod(&lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;        {&lt;br /&gt;            var &lt;span class="kwrd"&gt;value&lt;/span&gt; = SomeStaticClass.SomeStaticMethod(input);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; ? 1 : 0;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Below you will find a variety of options to make this class unit testable without the static call.&lt;/p&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Option 1 – Wrapper Class&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The first option which is extremely easy to do and very Inversion-of-Control-friendly is to make a wrapper class that has the exact same signature as the static methods you’re needing to call, but the only difference is that they are non-static. Each of these methods calls the static version and returns the value (if not void). Then, you can extract an interface to use for wiring up with IoC and mocking (such as with Moq).&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In this example, we take the same signature as our static class above, but create a non-static wrapper class.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; SomeLibraryYouCanModify&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; StaticWrapper : IStaticWrapper&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; SomeStaticMethod(&lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; SomeStaticClass.SomeStaticMethod(input);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;With our static wrapper in place, we now extract an interface:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; SomeLibraryYouCanModify&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IStaticWrapper&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;bool&lt;/span&gt; SomeStaticMethod(&lt;span class="kwrd"&gt;string&lt;/span&gt; input);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Perfect. Now we have a non-static wrapper class to our static methods that can be mocked. Here’s how to use it in the caller class:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; SomeLibraryYouCanModify&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WrapperMethod&lt;br /&gt;    {&lt;br /&gt;        IStaticWrapper _wrapper;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; WrapperMethod(IStaticWrapper wrapper)&lt;br /&gt;        {&lt;br /&gt;            _wrapper = wrapper;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; SomeMethod(&lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;        {&lt;br /&gt;            var &lt;span class="kwrd"&gt;value&lt;/span&gt; = _wrapper.SomeStaticMethod(input);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; ? 1 : 0;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;This class uses constructor injection, but you can do any Inversion-of-Control style you prefer (property injection, etc.). The main point is that we now have a hook where we can insert a mocked wrapper that does &lt;em&gt;not&lt;/em&gt; call the static method. Here’s how our test class might look:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;    [TestClass]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WrapperMethodTests&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TestWrapper : IStaticWrapper&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; SomeStaticMethod(&lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; !&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(input);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        [TestMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SomeMethod_GivenNull_ShouldReturnZero()&lt;br /&gt;        {&lt;br /&gt;            var wrapper = &lt;span class="kwrd"&gt;new&lt;/span&gt; TestWrapper();&lt;br /&gt;&lt;br /&gt;            var wm = &lt;span class="kwrd"&gt;new&lt;/span&gt; WrapperMethod(wrapper);&lt;br /&gt;&lt;br /&gt;            var output = wm.SomeMethod(&lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            Assert.AreEqual(0, output);&lt;br /&gt;        }&lt;br /&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;Notice how the TestWrapper is a private class just used for unit testing, and it does not do any time or I/O intensive operations. This allows our unit tests to execute quickly, providing quick turn-around when things break.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You could also use a mocking framework like Moq to create a mocked IStaticWrapper in the test example above.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Option 2 – Delegates&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If the amount of static operations you’re trying to mock is small, say one or two method calls, you may want to go the delegate route. In this approach, you take optional delegates into the constructor so that by default the class calls the static method, but in your unit tests you can specify a custom delegate (such as a lambda method) that is called instead.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Here’s our caller class using the delegate method:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; SomeLibraryYouCanModify&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DelegateMethod&lt;br /&gt;    {&lt;br /&gt;        Func&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; _delegate;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; DelegateMethod()&lt;br /&gt;        {&lt;br /&gt;            _delegate = SomeStaticClass.SomeStaticMethod;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; DelegateMethod(Func&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; method)&lt;br /&gt;        {&lt;br /&gt;            _delegate = method;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; SomeMethod(&lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;        {&lt;br /&gt;            var &lt;span class="kwrd"&gt;value&lt;/span&gt; = _delegate(input);&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt; ? 1 : 0;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If you aren’t familiar with generic delegates, Func&amp;lt;T, TResult&amp;gt; used above is a delegate for a method that takes a parameter of type T and returns a result of type TResult. So now our class, by default, sets the private delegate field to the static method, but in our unit tests we can specify a custom method instead. Here’s a unit test using this approach that passes a lambda into the constructor:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;    [TestClass]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DelegateMethodTests&lt;br /&gt;    {&lt;br /&gt;        [TestMethod]&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SomeMethod_GivenNull_ShouldReturnZero()&lt;br /&gt;        {&lt;br /&gt;            var dm = &lt;span class="kwrd"&gt;new&lt;/span&gt; DelegateMethod(i =&amp;gt; !&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(i));&lt;br /&gt;&lt;br /&gt;            var output = dm.SomeMethod(&lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            Assert.AreEqual(0, output);&lt;br /&gt;        }&lt;br /&gt;    }&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;br /&gt;&lt;/style&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We get the same result as the wrapper method – the test executes quickly, does not call the static method, and we can verify that the logic inside SomeMethod is correct.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Option 3 – Moles&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Moles is an isolation framework from Microsoft that allows you to mock static method calls without rewriting any implementation code. You can even mock calls to .NET framework methods and properties, like DateTime.Now. The benefits are easy to write unit tests and less refactoring time, although at the expense of maintaining tightly-coupled code. Another drawback of Moles I’ve experienced is that it does not work well in a team environment with Team Foundation Server, because it creates proxy DLLs that are hosted in your unit test project, and multiple users checking out and checking in these DLLs becomes a pain very quickly, especially since the default checkout action is an exclusive lock. However, if you’re a sole developer working on a project, it is a worthwhile choice.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can download Moles (with or without Pex) from &lt;a href="http://research.microsoft.com/en-us/projects/moles/" target="_blank"&gt;Microsoft Research’s Moles Project site&lt;/a&gt;. Make sure that you download the correct version for your CPU (x86 vs x64).&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;After installing, you can right click the reference to the library you’d like to mole in your unit test project, and select Add Moles Assembly.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/TX_yesQTsCI/AAAAAAAAAYk/UGRZ06gRpLE/s1600-h/image%5B3%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/TX_yeyTWhZI/AAAAAAAAAYo/ffoNWEUqyCE/image_thumb%5B4%5D.png?imgmax=800" width="404" height="243" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now that a Moles assembly has been added to the project, you can now create a unit test and mock that call. You must make sure to import the “.Moles” version of the namespace as seen in the example below. All types in the assembly that can be mocked begin with the letter “M”, and for static methods, the method becomes a property (named with the method name followed by all of the parameter types) that is of a delegate type. Then you can “set” that property to a new delegate, such as a lambda in the example below:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;strong&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; SomeLibraryYouCantModify.Moles;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; LibraryTestProject&lt;br /&gt;{&lt;br /&gt;    [TestClass]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MoledTests&lt;br /&gt;    {&lt;br /&gt;        [TestMethod]&lt;br /&gt;        &lt;strong&gt;[HostType(&lt;span class="str"&gt;&amp;quot;Moles&amp;quot;&lt;/span&gt;)]&lt;/strong&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SomeMethod_GivenNull_ShouldReturnZero()&lt;br /&gt;        {&lt;br /&gt;            var c = &lt;span class="kwrd"&gt;new&lt;/span&gt; SomeUntestableClass();&lt;br /&gt;&lt;br /&gt;            &lt;strong&gt;MSomeStaticClass.SomeStaticMethodString&lt;/strong&gt; = i =&amp;gt; !&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(i);&lt;br /&gt;&lt;br /&gt;            var output = c.SomeMethod(&lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            Assert.AreEqual(0, output);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;I’ve bolded the interesting lines. First, you have to import the “.Moles” namespace, as I mentioned earlier. Second, you must add the HostType(“Moles”) to any methods that are to be run in the Moles host. If a test method is not run in the Moles host, it will not be able to use the Moled type, and will let you know with an exception. Finally, the “SomeStaticClass” has a moled “MSomeStaticClass” that provides the mockable methods. The “SomeStaticMethod” method becomes “SomeStaticMethodString”, with the String being added to the name to represent the type of the first and only parameter to the method. This allows for method overloading, as different parameter types will cause different property names on the moled type. Then, the property is set to the given lambda, which is identical to the Delegates example above.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Out of the three methods, I end up preferring the Wrapper method the most, because it is easy to use with IoC containers and Moq. However, it is probably the most amount of work, because you have to create 2 new types, one for the wrapper class and the other for the interface. Also, it means that you have an entire type that has zero coverage, because you can not write unit tests for this wrapper class as it would call the static method.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The delegate method is great for one or two method isolation, as it’s easy to do, doesn’t need to involve the IoC container, and can be easily mocked with constructor overloads. However, it’s the least intuitive code to read, and is clunky to use for more than two method calls.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The Moles method is certainly clever and very impressive on first use. It works great for small projects with one developer, and can allow you to achieve greater code coverage very quickly. However, it is slower to start the unit test run due to the Moles host, it doesn’t help you decouple your code, and it is a pain for multiple developers working on the same project due to source control conflicts.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Obviously, the ideal situation is to have a non-static method to call. If you can get the developer of the library to do it for you, they can create a non-static version of the type/methods that are called by the static version, so that their existing static API is maintained and you get the benefit of being able to wire up the dependency with dependency injection. Clearly, though, this isn’t always possible.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Hope this helps!&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/3241881118181352756/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=3241881118181352756" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3241881118181352756?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3241881118181352756?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2011/03/mocking-static-methods-for-unit-testing.html" title="Mocking Static Methods for Unit Testing" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_xvXvGivrKF0/TX_yeyTWhZI/AAAAAAAAAYo/ffoNWEUqyCE/s72-c/image_thumb%5B4%5D.png?imgmax=800" height="72" width="72" /><thr:total>4</thr:total></entry><entry gd:etag="W/&quot;C0YBQXw_eip7ImA9Wx9QFEs.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-3408567981746800326</id><published>2010-12-27T09:45:00.001-05:00</published><updated>2010-12-27T09:45:50.242-05:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-27T09:45:50.242-05:00</app:edited><title>ObservableStack&lt;T&gt;</title><content type="html">&lt;p&gt;I had been looking around in the .NET framework for a Stack&amp;lt;T&amp;gt; equivalent of ObservableCollection&amp;lt;T&amp;gt; to use in a WPF application. Couldn’t find one, so I implemented one based on those two classes. Hope this helps!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:aa4e2d90-400a-4d92-a8f0-c4fd8a42b04c" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: true; first-line: 1; tab-size: 4;  toolbar: true;  width: 400px; height: 400px;" style=" width: 400px; height: 400px;overflow: auto;"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Collections.Specialized;&lt;br /&gt;using System.ComponentModel;&lt;br /&gt;using System.Collections;&lt;br /&gt;&lt;br /&gt;namespace AdventuresDotNet&lt;br /&gt;{&lt;br /&gt;    public class ObservableStack&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;, ICollection, INotifyCollectionChanged, INotifyPropertyChanged&lt;br /&gt;    {&lt;br /&gt;        T[] _array;&lt;br /&gt;        const int _defaultCapacity = 4;&lt;br /&gt;        static T[] _emptyArray;&lt;br /&gt;        int _size;&lt;br /&gt;        object _syncRoot;&lt;br /&gt;        int _version;&lt;br /&gt;&lt;br /&gt;        static ObservableStack()&lt;br /&gt;        {&lt;br /&gt;            _emptyArray = new T[0];&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public ObservableStack()&lt;br /&gt;        {&lt;br /&gt;            _array = _emptyArray;&lt;br /&gt;            _size = 0;&lt;br /&gt;            _version = 0;&lt;br /&gt;            _syncRoot = new object();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public ObservableStack(IEnumerable&amp;lt;T&amp;gt; collection)&lt;br /&gt;        {&lt;br /&gt;            if (collection == null)&lt;br /&gt;                throw new ArgumentNullException(&amp;quot;collection&amp;quot;);&lt;br /&gt;&lt;br /&gt;            var is2 = collection as ICollection&amp;lt;T&amp;gt;;&lt;br /&gt;&lt;br /&gt;            if (is2 != null)&lt;br /&gt;            {&lt;br /&gt;                int count = is2.Count;&lt;br /&gt;                _array = new T[count];&lt;br /&gt;                is2.CopyTo(_array, 0);&lt;br /&gt;                _size = count;&lt;br /&gt;            }&lt;br /&gt;            else&lt;br /&gt;            {&lt;br /&gt;                _size = 0;&lt;br /&gt;                _array = new T[4];&lt;br /&gt;                using (var enumerator = collection.GetEnumerator())&lt;br /&gt;                {&lt;br /&gt;                    while (enumerator.MoveNext())&lt;br /&gt;                    {&lt;br /&gt;                        this.Push(enumerator.Current);&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public ObservableStack(int capacity)&lt;br /&gt;        {&lt;br /&gt;            if (capacity &amp;lt; 0)&lt;br /&gt;                throw new ArgumentException(&amp;quot;Invalid capacity.&amp;quot;);&lt;br /&gt;&lt;br /&gt;            _array = new T[capacity];&lt;br /&gt;            _size = 0;&lt;br /&gt;            _version = 0;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void Push(T item)&lt;br /&gt;        {&lt;br /&gt;            if (_size == _array.Length)&lt;br /&gt;            {&lt;br /&gt;                T[] dest = new T[(_array.Length == 0) ? 4 : (2 * _array.Length)];&lt;br /&gt;                Array.Copy(_array, 0, dest, 0, _size);&lt;br /&gt;                _array = dest;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            _array[_size++] = item;&lt;br /&gt;            _version++;&lt;br /&gt;&lt;br /&gt;            OnCollectionAdded(item);&lt;br /&gt;            OnPropertyChanged(&amp;quot;Count&amp;quot;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public T Pop()&lt;br /&gt;        {&lt;br /&gt;            if (_size == 0)&lt;br /&gt;                throw new InvalidOperationException(&amp;quot;Empty stack, can not pop.&amp;quot;);&lt;br /&gt;&lt;br /&gt;            _version++;&lt;br /&gt;            T local = _array[--_size];&lt;br /&gt;            _array[_size] = default(T);&lt;br /&gt;&lt;br /&gt;            OnCollectionRemoved(local, _size);&lt;br /&gt;            OnPropertyChanged(&amp;quot;Count&amp;quot;);&lt;br /&gt;&lt;br /&gt;            return local;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public T Peek()&lt;br /&gt;        {&lt;br /&gt;            if (_size == 0)&lt;br /&gt;                throw new InvalidOperationException(&amp;quot;Empty stack, can not peek.&amp;quot;);&lt;br /&gt;&lt;br /&gt;            return _array[_size - 1];&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public event NotifyCollectionChangedEventHandler CollectionChanged;&lt;br /&gt;&lt;br /&gt;        public event PropertyChangedEventHandler PropertyChanged;&lt;br /&gt;&lt;br /&gt;        void OnCollectionReset()&lt;br /&gt;        {&lt;br /&gt;            var c = CollectionChanged;&lt;br /&gt;&lt;br /&gt;            if (c != null)&lt;br /&gt;                c(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        void OnCollectionAdded(object item)&lt;br /&gt;        {&lt;br /&gt;            var c = CollectionChanged;&lt;br /&gt;&lt;br /&gt;            if (c != null)&lt;br /&gt;                c(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        void OnCollectionRemoved(object item, int index)&lt;br /&gt;        {&lt;br /&gt;            var c = CollectionChanged;&lt;br /&gt;&lt;br /&gt;            if (c != null)&lt;br /&gt;                c(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        void OnPropertyChanged(string propertyName)&lt;br /&gt;        {&lt;br /&gt;            var p = PropertyChanged;&lt;br /&gt;&lt;br /&gt;            if (p != null)&lt;br /&gt;                p(this, new PropertyChangedEventArgs(propertyName));&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        public IEnumerator&amp;lt;T&amp;gt; GetEnumerator()&lt;br /&gt;        {&lt;br /&gt;            return new Enumerator(this);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()&lt;br /&gt;        {&lt;br /&gt;            return new Enumerator(this);&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        public void Clear()&lt;br /&gt;        {&lt;br /&gt;            Array.Clear(_array, 0, _size);&lt;br /&gt;            _size = 0;&lt;br /&gt;            _version++;&lt;br /&gt;&lt;br /&gt;            OnCollectionReset();&lt;br /&gt;            OnPropertyChanged(&amp;quot;Count&amp;quot;);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public bool Contains(T item)&lt;br /&gt;        {&lt;br /&gt;            int index = _size;&lt;br /&gt;&lt;br /&gt;            var comparer = EqualityComparer&amp;lt;T&amp;gt;.Default;&lt;br /&gt;&lt;br /&gt;            while (index-- &amp;gt; 0)&lt;br /&gt;            {&lt;br /&gt;                if (item == null)&lt;br /&gt;                {&lt;br /&gt;                    if (_array[index] == null)&lt;br /&gt;                        return true;&lt;br /&gt;                }&lt;br /&gt;                else if (_array[index] != null &amp;amp;&amp;amp; comparer.Equals(_array[index], item))&lt;br /&gt;                {&lt;br /&gt;                    return true;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            return false;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void CopyTo(T[] array, int arrayIndex)&lt;br /&gt;        {&lt;br /&gt;            if (array == null)&lt;br /&gt;                throw new ArgumentNullException(&amp;quot;array&amp;quot;);&lt;br /&gt;&lt;br /&gt;            if (arrayIndex &amp;lt; 0 || arrayIndex &amp;gt; array.Length)&lt;br /&gt;                throw new ArgumentOutOfRangeException(&amp;quot;arrayIndex&amp;quot;);&lt;br /&gt;&lt;br /&gt;            if ((array.Length - arrayIndex) &amp;lt; _size)&lt;br /&gt;                throw new ArgumentException();&lt;br /&gt;&lt;br /&gt;            Array.Copy(_array, 0, array, arrayIndex, _size);&lt;br /&gt;            Array.Reverse(array, arrayIndex, _size);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public int Count&lt;br /&gt;        {&lt;br /&gt;            get { return _size; }&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        public bool IsSynchronized&lt;br /&gt;        {&lt;br /&gt;            get { return false; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public object SyncRoot&lt;br /&gt;        {&lt;br /&gt;            get { return _syncRoot; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        void ICollection.CopyTo(Array array, int index)&lt;br /&gt;        {&lt;br /&gt;            Array.Copy(this._array, 0, array, index, this._size);&lt;br /&gt;            Array.Reverse(array, index, this._size);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public struct Enumerator : IEnumerator&amp;lt;T&amp;gt;, IDisposable, IEnumerator&lt;br /&gt;        {&lt;br /&gt;            ObservableStack&amp;lt;T&amp;gt; _stack;&lt;br /&gt;            int _index;&lt;br /&gt;            int _version;&lt;br /&gt;            T currentElement;&lt;br /&gt;&lt;br /&gt;            internal Enumerator(ObservableStack&amp;lt;T&amp;gt; stack)&lt;br /&gt;            {&lt;br /&gt;                _stack = stack;&lt;br /&gt;                _version = _stack._version;&lt;br /&gt;                _index = -2;&lt;br /&gt;                currentElement = default(T);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            public T Current&lt;br /&gt;            {&lt;br /&gt;                get&lt;br /&gt;                {&lt;br /&gt;                    if (_index &amp;lt; 0)&lt;br /&gt;                        throw new InvalidOperationException();&lt;br /&gt;&lt;br /&gt;                    return currentElement;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            public void Dispose()&lt;br /&gt;            {&lt;br /&gt;                _index = -1;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            object IEnumerator.Current&lt;br /&gt;            {&lt;br /&gt;                get&lt;br /&gt;                {&lt;br /&gt;                    if (_index &amp;lt; 0)&lt;br /&gt;                        throw new InvalidOperationException();&lt;br /&gt;&lt;br /&gt;                    return currentElement;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            public bool MoveNext()&lt;br /&gt;            {&lt;br /&gt;                bool flag;&lt;br /&gt;&lt;br /&gt;                if (_version != _stack._version)&lt;br /&gt;                    throw new InvalidOperationException(&amp;quot;Version mismatch.&amp;quot;);&lt;br /&gt;&lt;br /&gt;                if (_index == -2)&lt;br /&gt;                {&lt;br /&gt;                    _index = _stack._size - 1;&lt;br /&gt;                    flag = _index &amp;gt;= 0;&lt;br /&gt;&lt;br /&gt;                    if (flag)&lt;br /&gt;                        currentElement = _stack._array[_index];&lt;br /&gt;&lt;br /&gt;                    return flag;&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                if (_index == -1)&lt;br /&gt;                    return false;&lt;br /&gt;&lt;br /&gt;                flag = --_index &amp;gt;= 0;&lt;br /&gt;&lt;br /&gt;                if (flag)&lt;br /&gt;                {&lt;br /&gt;                    currentElement = _stack._array[_index];&lt;br /&gt;                    return flag;&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                currentElement = default(T);&lt;br /&gt;                return flag;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            public void Reset()&lt;br /&gt;            {&lt;br /&gt;                if (_version != _stack._version)&lt;br /&gt;                    throw new InvalidOperationException(&amp;quot;Version mismatch.&amp;quot;);&lt;br /&gt;&lt;br /&gt;                _index = -2;&lt;br /&gt;                currentElement = default(T);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/3408567981746800326/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=3408567981746800326" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3408567981746800326?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3408567981746800326?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/12/observablestack.html" title="ObservableStack&amp;lt;T&amp;gt;" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A0IEQnw6eyp7ImA9Wx5VEkw.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-195429077527859430</id><published>2010-10-04T14:51:00.003-04:00</published><updated>2010-10-04T15:11:43.213-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-10-04T15:11:43.213-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Threading" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="multithreading" /><title>HttpContext.Current and Threads with QueueUserWorkItem</title><content type="html">&lt;p&gt;If you’ve got an ASP.NET (including MVC) application that uses ThreadPool.QueueUserWorkItem with a reset event or anything of the like, you can not access HttpContext.Current (or rather, it is null) on those threads. This makes exception logging difficult, especially if you want to log the URL that is currently being requested.&lt;/p&gt;  &lt;p&gt;After a little digging, I discovered that the current HttpContext is actually in thread-local storage, which explains why child threads don’t have access to it. Luckily, you can pass a reference to it in your child thread. Include a reference to HttpContext in the “state” object of your callback method, and then you can store it to HttpContext.Current on that thread. If you notice, HttpContext.Current is not a read-only property – it has a setter as well. Set HttpContext.Current = myStateObj.HttpContextReference where myStateObj is your state object.&lt;/p&gt;  &lt;p&gt;I feel like this post is a bit self-explanatory, but here’s an obligatory example if you don’t follow:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:8364e5d5-945e-4769-a60f-b749736bc14f" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: false; first-line: 1; tab-size: 4;  toolbar: true;  width: 400px; height: 314px;" style=" width: 400px; height: 314px;overflow: auto;"&gt;private AutoResetEvent s_reset = new AutoResetEvent(false);&lt;br /&gt;&lt;br /&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;    var state = new WorkerState() { HttpContextReference = HttpContext.Current };&lt;br /&gt;    ThreadPool.QueueUserWorkItem(new WaitCallback(Worker), state);&lt;br /&gt;&lt;br /&gt;    try&lt;br /&gt;    {&lt;br /&gt;        s_reset.WaitOne();&lt;br /&gt;    }&lt;br /&gt;    finally&lt;br /&gt;    {&lt;br /&gt;        s_reset.Close();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void Worker(object state)&lt;br /&gt;{&lt;br /&gt;    var mystate = state as WorkerState;&lt;br /&gt;&lt;br /&gt;    if (mystate != null &amp;amp;&amp;amp; mystate.HttpContextReference != null)&lt;br /&gt;    {&lt;br /&gt;        HttpContext.Current = mystate.HttpContextReference;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    HttpContext.Current.Response.Write(&amp;quot;Threading!&amp;quot;);&lt;br /&gt;&lt;br /&gt;    s_reset.Set();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private class WorkerState&lt;br /&gt;{&lt;br /&gt;    public HttpContext HttpContextReference { get; set; }&lt;br /&gt;}&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;p&gt;And, of course, it works:&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/TKoiL0XMQKI/AAAAAAAAAXU/kuQH6RlNBGA/s1600-h/image%5B3%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/TKoiMed4SiI/AAAAAAAAAXY/ST-qnGBLz0M/image_thumb%5B1%5D.png?imgmax=800" width="318" height="207" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Hope this helps!&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/195429077527859430/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=195429077527859430" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/195429077527859430?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/195429077527859430?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/10/httpcontextcurrent-and-threads-with.html" title="HttpContext.Current and Threads with QueueUserWorkItem" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_xvXvGivrKF0/TKoiMed4SiI/AAAAAAAAAXY/ST-qnGBLz0M/s72-c/image_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>4</thr:total></entry><entry gd:etag="W/&quot;D0AHRng4fyp7ImA9Wx5WEE0.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-4238262179151706595</id><published>2010-09-20T14:00:00.002-04:00</published><updated>2010-09-20T14:02:17.637-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-20T14:02:17.637-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="generics" /><category scheme="http://www.blogger.com/atom/ns#" term="c#" /><title>Combining and Abusing Delegates</title><content type="html">&lt;p&gt;I had never before thought much about combining delegates until I came across some code that combined them using lambdas, and it got me thinking about other ways to use them. Consider the following example. Note: crazy code ahead!&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:03a74474-ec83-4b11-8171-454ac60e08f3" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: false; first-line: 1; tab-size: 4;  toolbar: false;  width: 400px; height: 300px;" style=" width: 400px; height: 300px;overflow: auto;"&gt;public class Programmer&lt;br /&gt;{&lt;br /&gt;    Action&amp;lt;string&amp;gt; a;&lt;br /&gt;&lt;br /&gt;    public Programmer(string name)&lt;br /&gt;    {&lt;br /&gt;        this.Name = name;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public string Name { get; set; }&lt;br /&gt;&lt;br /&gt;    public bool ShouldDesign&lt;br /&gt;    {&lt;br /&gt;        set&lt;br /&gt;        {&lt;br /&gt;            if (value)&lt;br /&gt;                a += s =&amp;gt; { Console.WriteLine(Name + &amp;quot; should design &amp;quot; + s); };&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public bool ShouldDevelop&lt;br /&gt;    {&lt;br /&gt;        set&lt;br /&gt;        {&lt;br /&gt;            if (value)&lt;br /&gt;                a += s =&amp;gt; { Console.WriteLine(Name + &amp;quot; should develop &amp;quot; + s); };&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public bool ShouldDeploy&lt;br /&gt;    {&lt;br /&gt;        set&lt;br /&gt;        {&lt;br /&gt;            if (value)&lt;br /&gt;                a += s =&amp;gt; { Console.WriteLine(Name + &amp;quot; should deploy &amp;quot; + s); };&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void Work(string programName)&lt;br /&gt;    {&lt;br /&gt;        a(programName);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;p&gt;And then used in a console app like this:&lt;/p&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ca22a207-ef8a-4e77-bb04-fae9a657f02f" class="wlWriterEditableSmartContent"&gt;&lt;pre class="brush: csharp; gutter: false; first-line: 1; tab-size: 4;  toolbar: false;  width: 400px; height: 238px;" style=" width: 400px; height: 238px;overflow: auto;"&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;    var paul = new Programmer(&amp;quot;Paul&amp;quot;);&lt;br /&gt;    paul.ShouldDesign = true;&lt;br /&gt;    paul.ShouldDevelop = true;&lt;br /&gt;    paul.ShouldDeploy = false;&lt;br /&gt;&lt;br /&gt;    paul.Work(&amp;quot;my application&amp;quot;);&lt;br /&gt;&lt;br /&gt;    Console.ReadKey();&lt;br /&gt;}&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;p&gt;And the output…&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/TJehOfpfC0I/AAAAAAAAAXM/3rP04R6af0w/s1600-h/image%5B3%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/TJehOuAOX0I/AAAAAAAAAXQ/qos3Ybsoj0E/image_thumb%5B1%5D.png?imgmax=800" width="283" height="50" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;I do not encourage the kind of code I’ve created here, but maybe this will get you thinking!&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/4238262179151706595/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=4238262179151706595" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/4238262179151706595?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/4238262179151706595?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/09/combining-and-abusing-delegates.html" title="Combining and Abusing Delegates" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_xvXvGivrKF0/TJehOuAOX0I/AAAAAAAAAXQ/qos3Ybsoj0E/s72-c/image_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A0YNRno8fCp7ImA9Wx5XFUw.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-881311949244639492</id><published>2010-09-14T22:53:00.001-04:00</published><updated>2010-09-14T22:53:17.474-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-14T22:53:17.474-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="c#" /><title>Thoughts on the Microsoft Ecosystem</title><content type="html">&lt;p&gt;I had the great opportunity to touch a Windows Phone 7 device at the Jacksonville Code Camp a few weeks ago, and it sold me – I’ll likely give up my iPhone 4 for a Windows Phone 7 device when they ship this fall. The biggest selling point as a C# developer is that I can develop both data-driven apps in Silverlight and games in XNA for Windows Phone 7 in the language I use every day – C#. So that got me thinking about where else C# and the .NET platform can be used, both at work and at home.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Windows 7 client apps in WPF (with multi-touch for tablets)&lt;/li&gt;    &lt;li&gt;Rich Internet Apps with Silverlight 4 (again with multi-touch)&lt;/li&gt;    &lt;li&gt;Websites with ASP.NET and ASP.NET MVC&lt;/li&gt;    &lt;li&gt;Scaffolding data-driven sites with ASP.NET Dynamic Data&lt;/li&gt;    &lt;li&gt;Windows Phone 7 data-driven apps in Silverlight&lt;/li&gt;    &lt;li&gt;Games for Windows Phone 7 with XNA&lt;/li&gt;    &lt;li&gt;Games for PC with XNA&lt;/li&gt;    &lt;li&gt;Games for Xbox 360 with XNA&lt;/li&gt;    &lt;li&gt;Middleware real-time services with WCF&lt;/li&gt;    &lt;li&gt;Middleware queued services with WCF, WF, and MSMQ&lt;/li&gt;    &lt;li&gt;SQL Server integration with CLR stored procedures&lt;/li&gt;    &lt;li&gt;Windows Media Center apps with the Media Center SDK&lt;/li&gt;    &lt;li&gt;Embedded device apps with WPF (Windows Embedded)&lt;/li&gt;    &lt;li&gt;Small-scale embedded devices with .NET Micro Framework (including Netduino)&lt;/li&gt;    &lt;li&gt;Windows Home Server add-ins with the WHS SDK&lt;/li&gt;    &lt;li&gt;Cloud apps with the Windows Azure SDK&lt;/li&gt;    &lt;li&gt;SharePoint parts and sites&lt;/li&gt;    &lt;li&gt;Office add-ins for Outlook, Excel, Word, and others&lt;/li&gt;    &lt;li&gt;Surface development with the Surface SDK (which will be in our homes within a few years)&lt;/li&gt;    &lt;li&gt;… and probably a dozen other things I’m forgetting.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;That’s a huge list, and I’ve heard complaints from people that the ecosystem has gotten too big, and therefore too much to keep track of and learn. I don’t look at it that way – I view all of these varied and important areas as all using the same foundation, C# and the .NET framework. If you know C# and the framework, you can jump from one to the other with a minimal learning curve. A WPF developer can jump to Silverlight and then to Surface and then to Windows Media Center and then to Windows Phone 7 with Silverlight without much to learn. An ASP.NET developer can jump to ASP.NET MVC and to Windows Azure and to SharePoint, again with minimal learning. Compared to jumping to another framework or another language, jumping between any of these technologies is relatively painless.&lt;/p&gt;  &lt;p&gt;It’s a great time to be a .NET developer, and the potential with all of these technologies excites me!&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/881311949244639492/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=881311949244639492" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/881311949244639492?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/881311949244639492?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/09/thoughts-on-microsoft-ecosystem.html" title="Thoughts on the Microsoft Ecosystem" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;D0AGQHk5eCp7ImA9Wx5RE0k.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-5995559387354432451</id><published>2010-08-20T19:08:00.001-04:00</published><updated>2010-08-20T19:08:41.720-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-20T19:08:41.720-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="VS2010" /><category scheme="http://www.blogger.com/atom/ns#" term="WF" /><title>My First Workflow Foundation (WF) 4 App</title><content type="html">&lt;p&gt;I came across a book in our company’s “library” today – Microsoft Windows Workflow Foundation, Step by Step – and I started thinking about whether or not I should try to learn WF. Frankly, I haven’t been interested so far. Being a front-end developer, I haven’t really developed many service-level applications or processes, and of the ones I’ve touched, it seems like remoting, WCF, and/or MSMQ works rather well for those tasks. However, I came across NServiceBus this past week and it got me thinking more in the mindset of a service designer, and WF piqued my interest. A good developer tries to be well-rounded and knowledgeable about other technologies, even if he or she doesn’t use them on a daily basis.&lt;/p&gt;  &lt;p&gt;I started flipping through the first chapter of this book. I quickly realized that it’s a 3 year old book (a lifetime in software technologies) and is about .NET 3.0 – the red-headed stepchild release of .NET. WF, of course, has seen 2 revisions since, with .NET 3.5 and again with 4.0. Nonetheless, I enjoyed the first chapter, and I started to see where WF could be useful. (Despite, that is, a question I posted on &lt;a href="http://twitter.com/paulirwin"&gt;twitter&lt;/a&gt; asking if anyone has used WF, to which I got zero responses.)&lt;/p&gt;  &lt;p&gt;I came home, fired up my beast of a work laptop (Core i7 4core/8thread, 8GB RAM), and launched Visual Studio 2010. File – New Project. Immediately I noticed a difference from the first chapter of this book. In the .NET 3 version, you have to choose between a Sequential or State Machine workflow type and then between a console app or library, or an “activity library”, or an empty workflow project. In VS2010, you only have 2 non-library Workflow project types – WCF Workflow Service, and Workflow Console App. I chose the Workflow Console App because it more closely matched the example. Then the similarities came to a screeching halt.&lt;/p&gt;  &lt;p&gt;I hadn’t kept up with WF, otherwise I should have known this. WF in 4.0 uses XAML to describe workflows, and the only “code” in the workflow are “expressions” in… wait for it… VB. Yes, even though I created a C# Workflow Console App, the expressions are in Visual Basic. Furthermore, the example in the book is a .cs file (not .xaml), and has an accompanying .Designer.cs file as a “code-behind”. Better still, when you create “Code” activities in the older WF, they are methods in a C# file. The only equivalent to the “Code” activity I found in WF 4 is the “InvokeMethod” activity, which can only call a method on a type &lt;em&gt;not&lt;/em&gt; defined in the workflow.&lt;/p&gt;  &lt;p&gt;Still, I was able to accomplish a fairly basic If/Else workflow in WF4. Here’s what it looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/TG8K8CLl9HI/AAAAAAAAAWk/hvCVBNcM7kI/s1600-h/image%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/TG8K8Vez9bI/AAAAAAAAAWo/2hgz2tTV3Js/image_thumb%5B1%5D.png?imgmax=800" width="488" height="240" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Fairly trivial. If a “PostalCodeIsValid”, then I write a line to the console saying it’s valid, and if not, I write a line saying it’s not. Notice the VB ampersands for string concatenation in the “Text” fields.&lt;/p&gt;  &lt;p&gt;Look at the Condition field. I’m executing a method on a “Workflow1Helper” static class I created for simplicity. That method simply returns “PostalCode.Length == 5” which is not a true postal code validation routine, but could be replaced with one. Luckily, the Workflow1Helper class is written in C#.&lt;/p&gt;  &lt;p&gt;It seems like this is the pattern for WF4 workflows – define your workflow in XAML, and farm out your code to other types. Makes since, especially from a separation of concerns aspect, but it’s hardly similar to WF in .NET 3.0.&lt;/p&gt;  &lt;p&gt;So where does that PostalCode field come from? You must define it as an “Argument”. If you look at the bottom of the workflow designer, you’ll see this pane:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/TG8K8uCs0rI/AAAAAAAAAWs/ARPYC0H13Ek/s1600-h/image%5B6%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/TG8K9KojSUI/AAAAAAAAAWw/pR80qS53T4c/image_thumb%5B2%5D.png?imgmax=800" width="214" height="77" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The Arguments tab brings up this pane:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/TG8K9Wgw4tI/AAAAAAAAAW0/HQffU-M1ASk/s1600-h/image%5B10%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/TG8K9vVEX2I/AAAAAAAAAW4/IPy3U7qm8cE/image_thumb%5B4%5D.png?imgmax=800" width="504" height="127" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;That’s where I created the PostalCode argument. You can define any type and number of arguments you want, think of them as “parameters” to your workflow when you invoke it (more on invocation later). Also note you can assign a Default Value, i.e. “New MyType()” (alas, &lt;em&gt;sigh&lt;/em&gt;, again in VB.)&lt;/p&gt;  &lt;p&gt;Lastly, the invocation. There’s a Program.cs file that gets created as part of the project, and it’s a standard console app entry point class. I just had to modify the invocation part to pass the PostalCode argument:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/TG8K94oPb9I/AAAAAAAAAW8/6qHPLBxjnis/s1600-h/image%5B14%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/TG8K-Gy1uBI/AAAAAAAAAXA/frjx2KoR1ec/image_thumb%5B6%5D.png?imgmax=800" width="542" height="112" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;(I apologize for the code-screenshot, I am lazy and haven’t installed the PreCode plug-in yet.)&lt;/p&gt;  &lt;p&gt;Obviously, hitting F5 will run the app, and we get a message saying it’s valid. Yay.&lt;/p&gt;  &lt;p&gt;Now time for the dramatic conclusion. WF4 seems much more polished than WF in .NET 3 appears in the book. I love that it’s XAML (although you really don’t want to go in and modify it, it’s pretty ugly), and I love that the code is separated from the workflow logic. It’s all very professional – easy to understand, self-documenting processes, and very easy to grasp. I also created my second WF app, this time as a WCF service, and I think that’s a much more useful paradigm than a console app, especially if it’s possible to use with a MSMQ binding (which I haven’t tried). &lt;/p&gt;  &lt;p&gt;For the negatives, I dislike the fact that a C# WF app needs to use VB expressions, because I dislike having to switch gears. I don’t mind coding in VB if I have to, but would it have killed them to allow C# expressions?&lt;/p&gt;  &lt;p&gt;I want to read up more on WF, as well as it’s migration from 3 –&amp;gt; 3.5 –&amp;gt; 4, as well as play around more with the WCF version of it before writing another blog post on the subject. &lt;/p&gt;  &lt;p&gt;Bottom line, fire up Visual Studio 2010 and create your first WF app and go to town. Come up with some cool real-world ways to use it, and let me know, will you?&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/5995559387354432451/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=5995559387354432451" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/5995559387354432451?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/5995559387354432451?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/08/my-first-workflow-foundation-wf-4-app.html" title="My First Workflow Foundation (WF) 4 App" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_xvXvGivrKF0/TG8K8Vez9bI/AAAAAAAAAWo/2hgz2tTV3Js/s72-c/image_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkUMRn48eSp7ImA9Wx5TGEk.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-3742808247853310266</id><published>2010-08-03T08:50:00.003-04:00</published><updated>2010-08-03T08:58:07.071-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-03T08:58:07.071-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="jaxcc" /><title>Jax Code Camp - August 28th, 2010</title><content type="html">Last year about this time I was just in the process of moving to Jacksonville, but I made sure that despite living out of boxes, I made it to the Jax Code Camp 2009. It was a great event, tons of people (somewhere north of 600?), and had a great after party where you get valuable time socializing with the community.&lt;br /&gt;&lt;br /&gt;This year, it's happening again, and I'm a speaker. It's on Saturday, August 28th, 2010 at the UNF campus. You can get more details at &lt;a href="http://www.jaxcodecamp.com"&gt;http://www.jaxcodecamp.com&lt;/a&gt;. Registration is free, and there's many sessions and tracks available, including one all-day session about Windows Phone 7 that you can pop in and out of.&lt;br /&gt;&lt;br /&gt;My talk is titled "Dynamic Code with IL and Expressions" and from the IL side I'll be covering how to use DynamicMethod to create functions and methods at runtime with IL emit, as well as how to use AssemblyBuilder to create entire types and assemblies at runtime (such as creating derived "proxy" classes for aspect-oriented programming). From the Expression side, I'll show how to parse Expressions for simple key selectors to advanced LINQ providers, and how to create methods (much like you would in IL) with Expressions to do a number of common tasks.&lt;br /&gt;&lt;br /&gt;Hope to see you there! Again, it's a free, all-day conference-style event with breakfast, lunch, and an after-party. You won't want to miss it!</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/3742808247853310266/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=3742808247853310266" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3742808247853310266?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3742808247853310266?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/08/jax-code-camp-august-28th-2010.html" title="Jax Code Camp - August 28th, 2010" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;D08MQno5cSp7ImA9WxFUGU8.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-8981168736452130661</id><published>2010-06-30T15:23:00.002-04:00</published><updated>2010-06-30T15:24:43.429-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-30T15:24:43.429-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ienumerable" /><category scheme="http://www.blogger.com/atom/ns#" term="generics" /><category scheme="http://www.blogger.com/atom/ns#" term="extension methods" /><category scheme="http://www.blogger.com/atom/ns#" term="c#" /><title>A better, more type-safe, alternative to Cast&lt;T&gt;()</title><content type="html">&lt;p&gt;I ran into a limitation in the Cast extension method included with LINQ (Enumerable.Cast&amp;lt;TResult&amp;gt;(this IEnumerable source)), specifically that it does not allow for explicit/implicit conversion operators. The reason for this is that it is an extension method on IEnumerable, not IEnumerable&amp;lt;T&amp;gt;. Therefore the “Current” property (see previous post on IEnumerable&amp;lt;T&amp;gt;) is of type Object, and it can’t perform the cast from type A to Object to type B if there’s a conversion operator on either type.&lt;/p&gt;  &lt;p&gt;I wrote a quick extension method to handle this:&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;public static IEnumerable&amp;lt;TDest&amp;gt; CastAll&amp;lt;TItem, TDest&amp;gt;(this IEnumerable&amp;lt;TItem&amp;gt; items)&lt;br /&gt;{&lt;br /&gt;    var p = Expression.Parameter(typeof(TItem), &amp;quot;i&amp;quot;);&lt;br /&gt;    var c = Expression.Convert(p, typeof(TDest));&lt;br /&gt;    var ex = Expression.Lambda&amp;lt;Func&amp;lt;TItem, TDest&amp;gt;&amp;gt;(c, p).Compile();&lt;br /&gt;&lt;br /&gt;    foreach (var item in items)&lt;br /&gt;    {&lt;br /&gt;        yield return ex(item);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Basically it builds an Expression that takes TItem, performs a type-safe Convert expression to TDest, and compiles that expression to a lambda of type Func&amp;lt;TItem, TDest&amp;gt; once per call (this could be cached outside of the method), and calls this new function on each item. Now it does a direct cast from TItem to TDest, instead of the type-unsafe A to Object to B casting that Cast&amp;lt;T&amp;gt; does.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Hope this helps!&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/8981168736452130661/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=8981168736452130661" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/8981168736452130661?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/8981168736452130661?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/06/better-more-type-safe-alternative-to.html" title="A better, more type-safe, alternative to Cast&amp;lt;T&amp;gt;()" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;DE8NQ3wzfCp7ImA9WxFVEUo.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-1696343094391308494</id><published>2010-06-10T09:33:00.001-04:00</published><updated>2010-06-10T09:34:52.284-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-10T09:34:52.284-04:00</app:edited><title>My Apologies</title><content type="html">I accidentally recently marked many valid comments as rejected when going through them (which was long overdue!). I apologize, there were some good posts there that are now lost. I was attempting to reject just one spam one but accidentally had others checked and Blogger decided I wanted to reject all of them.&lt;br /&gt;&lt;br /&gt;Meanwhile, I recently discovered that one of my previous posts on extension methods was incorrect. I'll follow up soon (hopefully) with a corrected version.</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/1696343094391308494/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=1696343094391308494" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/1696343094391308494?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/1696343094391308494?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/06/my-apologies.html" title="My Apologies" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CEYFSXY-eyp7ImA9WxFVEks.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-8572763756655870622</id><published>2010-06-01T16:10:00.006-04:00</published><updated>2010-06-11T09:15:18.853-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-11T09:15:18.853-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ienumerable" /><category scheme="http://www.blogger.com/atom/ns#" term="generics" /><category scheme="http://www.blogger.com/atom/ns#" term="extension methods" /><category scheme="http://www.blogger.com/atom/ns#" term="c#" /><title>Understanding C#’s “yield” Keyword and IEnumerable&lt;T&gt;</title><content type="html">One of the interview questions I usually give candidates for .NET developer positions is around IEnumerable&amp;lt;T&amp;gt;. What is it? What pattern does it represent under the hood? How would you use it? You might be surprised to see how many candidates have no idea what it even is, yet they probably use it everyday, or at least it’s older brother, IEnumerable.&lt;br /&gt;&lt;br /&gt;IEnumerable and IEnumerable&amp;lt;T&amp;gt; represent a sequence of items that can be iterated over (forward only), and may be lazy evaluated upon iteration. Note that I carefully didn’t use the term “collection”, rather “sequence”, because collection implies that the items are known ahead of time, before evaluation. With IEnumerable and IEnumerable&amp;lt;T&amp;gt;, you can generate objects on the fly as you iterate over the sequence if you wish.&lt;br /&gt;&lt;br /&gt;Before we get to IEnumerable&amp;lt;T&amp;gt;, let’s look at IEnumerable. Bring back your .NET 1.1 hats, ladies and gentlemen. IEnumerable, as the “I” in the name suggests, is an interface only. It does not provide any functionality. IEnumerable only defines one method, called GetEnumerator(), that returns an IEnumerator. The purpose of separating IEnumerable and IEnumerator, I believe, is to logically separate the functions of a sequence of items as a whole, and performing the actual iteration over them.&lt;br /&gt;&lt;br /&gt;Let’s create a class that implements IEnumerable, called OldEnumerable (since this is before generics in .NET 2.0).&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;public class OldEnumerable : IEnumerable&lt;br /&gt;{&lt;br /&gt;    public IEnumerator GetEnumerator()&lt;br /&gt;    {&lt;br /&gt;        return new OldEnumerator();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Very simple, right? All we’re doing is returning an instance of OldEnumerator. Do note that an actual class that implements IEnumerable would probably be doing a lot more than this, such as providing utility functions or such. Also, a real-world implementation would probably pass some information about the sequence’s state or identity to the constructor of OldEnumerator, which is not only valid but encouraged, since interfaces can not define the required signatures of any constructors.&lt;br /&gt;&lt;br /&gt;Now let’s look at OldEnumerator, before we look at a sample of how to use it.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;public class OldEnumerator : IEnumerator&lt;br /&gt;{&lt;br /&gt;    int x = 0;&lt;br /&gt;&lt;br /&gt;    public object Current&lt;br /&gt;    {&lt;br /&gt;        get { return x; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public bool MoveNext()&lt;br /&gt;    {&lt;br /&gt;        return x++ &amp;lt; 100;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void Reset()&lt;br /&gt;    {&lt;br /&gt;        x = 0;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This class does one thing and one thing well – return numbers from 1 to 100. Notice that it implements the IEnumera&lt;strong&gt;tor&lt;/strong&gt; interface, not IEnumera&lt;strong&gt;ble&lt;/strong&gt;. This interface dictates the property and two methods you see above – namely the read-only property Current (note the return type of System.Object), MoveNext() which returns boolean, and Reset(). The names should be pretty self explanatory – Current returns the current item, MoveNext() tries to move to the next item (and returns false if it can not), and Reset(), which resets the enumerator to it’s (hopefully) original state.&lt;br /&gt;&lt;br /&gt;In this example, we have a private variable x, which is simply returned by Current whenever it is requested. Note that a real-world example would probably throw an exception if MoveNext() had not yet been called. (And this is an important note: MoveNext() must be called to access the first item before Current can be called, according to the way that IEnumerable is used throughout the framework.)&lt;br /&gt;&lt;br /&gt;Our MoveNext() method returns whether the value of x is less than 100, then increments x by 1. This allows 100 to be an inclusive maximum value of this sequence. By initializing x to 0, since MoveNext() should be called before any calls to Current, this ensures that the sequence will start at 1. Finally, the Reset() method sets x to 0, and upon the next MoveNext() call, x will start the sequence over again at 1.&lt;br /&gt;&lt;br /&gt;So let’s look at an example of how to use our new OldEnumerable and OldEnumerator classes. (This is in the Main() method of a console application.)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;var old = new OldEnumerable();&lt;br /&gt;&lt;br /&gt;var oldenum = old.GetEnumerator();&lt;br /&gt;&lt;br /&gt;while (oldenum.MoveNext())&lt;br /&gt;{&lt;br /&gt;    object i = oldenum.Current;&lt;br /&gt;&lt;br /&gt;    Console.WriteLine(i);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Console.ReadKey();&lt;/pre&gt;&lt;br /&gt;As you can see, we first create our IEnumerable class, then call GetEnumerator to get our IEnumerator class. We don’t actually know that we’re working with GetEnumerator here, we’re merely working by contract with IEnumerator. Next, we have a while loop with a precondition of oldenum.MoveNext(), which returns whether or not we can access the next element in the sequence. If we can, we get the Current element (again of type Object), and implicitly call its ToString() method in the call to Console.WriteLine(). We now get the following output:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/TAVpI5RE3rI/AAAAAAAAAVU/RrmAsQdJsZ4/s1600-h/image%5B4%5D.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/TAVpJuuvDzI/AAAAAAAAAVY/1PTtyv346wo/image_thumb%5B2%5D.png?imgmax=800" border="0" height="285" width="532" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;(I’m assuming you can trust me when I say it starts at 1. In retrospect, going 1 to 10 would have been a better example.)&lt;br /&gt;&lt;br /&gt;When you look at IEnumerable/IEnumerator this way, you quickly realize that the values of x were not pre-calculated. Instead, they were lazy evaluated and calculated on the fly. This is why I chose to use the term “sequence” instead of “collection” earlier.&lt;br /&gt;&lt;br /&gt;Obviously, calling GetEnumerator and storing it’s output into a new variable, and calling MoveNext() and Current from a “while” loop is not the cleanest way of looping over items. We’d much rather do this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;var old = new OldEnumerable();&lt;br /&gt;&lt;br /&gt;foreach (var i in old)&lt;br /&gt;    Console.WriteLine(i);&lt;br /&gt;&lt;br /&gt;Console.ReadKey();&lt;/pre&gt;&lt;br /&gt;Much better. And, best of all, it still works as expected. Why? There’s a compiler trick going on here. To find what it is, we first step through the code.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/TAVpKOQiy7I/AAAAAAAAAVc/8jlB7WyrORo/s1600-h/image%5B7%5D.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/TAVpKdmIpkI/AAAAAAAAAVg/gEtjV3H0EbA/image_thumb%5B3%5D.png?imgmax=800" border="0" height="117" width="232" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;After highlighting the foreach keyword and hitting F10 to continue stepping, it first highlights the “old” in the statement as seen above. Next step it highlights “in”:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/TAVpKqbWIaI/AAAAAAAAAVk/1y2VGizXh0k/s1600-h/image%5B10%5D.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/TAVpLH-jc1I/AAAAAAAAAVo/EHvxM42K0eU/image_thumb%5B4%5D.png?imgmax=800" border="0" height="119" width="231" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;And then on the next step, it highlights “var i”:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/TAVpLfUU9qI/AAAAAAAAAVs/0f04VfybclY/s1600-h/image%5B13%5D.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/TAVpLs3f1cI/AAAAAAAAAVw/gSm8PhTcsJU/image_thumb%5B5%5D.png?imgmax=800" border="0" height="114" width="228" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;What’s going on here? Why is it moving backwards? Actually, it’s performing GetEnumerator(), MoveNext(), and Current in those three steps, in that order. When “old” is highlighted, it calls GetEnumerator() and stores the enumerator in a hidden variable. When “in” is highlighted, it calls MoveNext(), which upon the first iteration is very convenient because it’s called before Current is referenced. Then, when “var i” is highlighted, it calls Current’s getter method and stores the output in a variable (here inferred to be type Object) called “i”.&lt;br /&gt;&lt;br /&gt;Thanks to Visual Studio 2010’s new Intellitrace feature, we can see this in action:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/TAVpMGBoNBI/AAAAAAAAAV0/8lStfjp-9U8/s1600-h/image%5B17%5D.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/TAVpMUwAjkI/AAAAAAAAAV4/JKXUK6qUOs8/image_thumb%5B7%5D.png?imgmax=800" border="0" height="276" width="371" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The blue arrows indicate code that has been automatically stepped over, and you can see at each stop point (indicated by red Stop-button icons) that the functions mentioned above are called. Furthermore, when we go another iteration through, we see the following:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/TAVpM9FZ2SI/AAAAAAAAAV8/pAuRaYDH2uk/s1600-h/image%5B22%5D.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/TAVpNFfHbMI/AAAAAAAAAWA/DTZNo2ndALI/image_thumb%5B10%5D.png?imgmax=800" border="0" height="260" width="336" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;We see the first call to Console.WriteLine() in the body of our foreach loop, and then MoveNext() is called again followed by Current. This will continue ad nauseum until MoveNext() returns false and giving us an escape from our iteration. If MoveNext() never returns false, the iteration could continue infinitely, or until an exception is thrown.&lt;br /&gt;&lt;br /&gt;Also of note is on our second iteration, “old” is not highlighted. This is because we no longer need a new enumerator, since we already have one. So first “in” is highlighted (MoveNext) followed by “var i” (setting the value of Current to “i”).&lt;br /&gt;&lt;br /&gt;At the beginning of this post, I mentioned a question I might ask to a candidate: what pattern does this represent under the hood? While it’s not the most straight-forward question (or the most straight-forward answer), it’s an interesting question to ask because it gets the candidate to think. The answer is a “state machine” pattern, where the object operates based on its state, which may be constantly changing, but the state is persisted in the object for a given period of time (such as the object’s lifespan). The “Current” value of x is our state (which could be the current index of an array, for example), and MoveNext() alters our state.&lt;br /&gt;&lt;br /&gt;Now let’s look at the generic version of IEnumerable, IEnumerable&amp;lt;T&amp;gt;. It also has a generic enumerator counterpart, IEnumerator&amp;lt;T&amp;gt;. Interestingly enough, the namespaces that contain the generic versions are imported by default with a new project in Visual Studio 2010 / .NET 4, while the IEnumerable/IEnumerator non-generic versions are in a namespace that is not imported by default, System.Collections. I take this to mean that Microsoft is encouraging the adoption of the generic, type-safe versions of them instead of the non-generic versions. In our example, we get for free the added benefit of type inference as well as we no longer need to box the Int32 variable when we return it in the Current getter, probably resulting in better performance due to less IL being generated.&lt;br /&gt;&lt;br /&gt;Like IEnumerable, we have GetEnumerator() in IEnumerable&amp;lt;T&amp;gt;. However, it now has an overload, one which returns IEnumerator&amp;lt;T&amp;gt;, the other which returns IEnumerator. The reason for this is simpler than it may appear on the surface -- IEnumerable&amp;lt;T&amp;gt; inherits from IEnumerable for compatibility reasons. You can safely cast an IEnumerable&amp;lt;T&amp;gt; type to IEnumerable for use with legacy code. However, with value types such as our example, you’ll end up adding boxing as well.&lt;br /&gt;&lt;br /&gt;Here’s our IEnumerable&amp;lt;T&amp;gt; implementation:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;public class NewEnumerable : IEnumerable&amp;lt;int&amp;gt;&lt;br /&gt;{&lt;br /&gt;    public IEnumerator&amp;lt;int&amp;gt; GetEnumerator()&lt;br /&gt;    {&lt;br /&gt;        return new NewEnumerator();&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    IEnumerator IEnumerable.GetEnumerator()&lt;br /&gt;    {&lt;br /&gt;        return new NewEnumerator();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;As you can see, we have the new, generic GetEnumerator as well as the old, non-generic one. In both cases we’re returning a new instance of the NewEnumerator() class, since it inherits from IEnumerator&amp;lt;T&amp;gt; as well as IEnumerator, making it a safe implicit cast for both. Here’s our IEnumerator&amp;lt;T&amp;gt; implementation:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;public class NewEnumerator : IEnumerator&amp;lt;int&amp;gt;&lt;br /&gt;{&lt;br /&gt;    int x = 0;&lt;br /&gt;&lt;br /&gt;    public int Current&lt;br /&gt;    {&lt;br /&gt;        get { return x; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void Dispose()&lt;br /&gt;    {&lt;br /&gt;        // do nothing&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    object IEnumerator.Current&lt;br /&gt;    {&lt;br /&gt;        get { return x; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public bool MoveNext()&lt;br /&gt;    {&lt;br /&gt;        return x++ &amp;lt; 100;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void Reset()&lt;br /&gt;    {&lt;br /&gt;        x = 0;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Note that IEnumerator&amp;lt;T&amp;gt; not only inherits from IEnumerator but also IDisposable as well, meaning you can use any IEnumerator&amp;lt;T&amp;gt; inside a “using” block if you wish. Hence, we now have a Dispose method along for the ride. In our implementation, we don’t have any unmanaged resources we need to dispose of, so we do nothing.&lt;br /&gt;&lt;br /&gt;We also now have two Current properties – one returns T (in this case, int), the other returns object. When working with the generic version, we’ll be using the T version of Current, and when working this enumerator as IEnumerator, we’ll be using the object version of Current. Since IEnumerator&amp;lt;T&amp;gt; only defines Current, we inherit MoveNext() and Reset() from IEnumerator, and so there’s no change there. Also, here you can see that since Current is now generic and returns int, there is no longer a need to box “x” to an object to return it.&lt;br /&gt;&lt;br /&gt;We can now use the NewEnumerator class in exactly the same way as OldEnumerator, but now we don’t need to cast the value to use it as an int. (If we wanted to – we didn’t need to in the previous example as we were only implicitly using ToString() on System.Object.)&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;var newenum = new NewEnumerable();&lt;br /&gt;&lt;br /&gt;foreach (int i in newenum)&lt;br /&gt;    Console.WriteLine(i);&lt;br /&gt;&lt;br /&gt;Console.ReadKey();&lt;/pre&gt;&lt;br /&gt;So that’s all well and good. Now, how do we use the “yield” keyword, and why did I tie it in with a post on IEnumerable&amp;lt;T&amp;gt;?&lt;br /&gt;&lt;br /&gt;Because &lt;em&gt;it builds for you an automatic IEnumerator&amp;lt;T&amp;gt;.&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;That’s pretty much all it does. That – and it maintains the state of the code around it until the next iteration. This may not be the best explanation of the “yield” keyword, but I think it fits.&lt;br /&gt;&lt;br /&gt;Here’s a handy extension method to make your code feel more fun and expressive (or confusing and hard-to-maintain, depending on if you’re an optimist or a pessimist).&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;public static IEnumerable&amp;lt;int&amp;gt; To(this int value, int inclusiveMax)&lt;br /&gt;{&lt;br /&gt;    for (int i = value; i &amp;lt;= inclusiveMax; i++)&lt;br /&gt;        yield return i;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This extension method, when placed in a static class in your project will allow you to this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;var ten = 1.To(10);&lt;br /&gt;&lt;br /&gt;foreach (var i in ten)&lt;br /&gt;    Console.WriteLine(i);&lt;/pre&gt;&lt;br /&gt;Since 1 is an int, it gets the extension method To() which returns an IEnumerable&amp;lt;int&amp;gt;. Notice that we have not created a class that inherits from IEnumerable&amp;lt;int&amp;gt;, nor a class that inherits from IEnumerator&amp;lt;int&amp;gt;. The C# compiler does this for us automatically with an anonymous, compiler-generated type. We can pull it up in &lt;a href="http://www.red-gate.com/products/reflector/" target="_blank"&gt;Reflector&lt;/a&gt; and see this type:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/TAVpNRJBy8I/AAAAAAAAAWE/WHztmnM0uyM/s1600-h/image%5B26%5D.png"&gt;&lt;img style="border: 0px none; display: inline;" title="image" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/TAVpNo1LibI/AAAAAAAAAWI/qoLse2xIGP4/image_thumb%5B12%5D.png?imgmax=800" border="0" height="478" width="543" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Notice that the method starts with &amp;lt;To&amp;gt; – that’s the name of the method that uses this enumerator. Also notice the d__0 identifier – that’s how the .NET runtime knows how to access this compiler-generated type. And finally, notice that it inherits from both IEnumerable&amp;lt;T&amp;gt; and IEnumerator&amp;lt;T&amp;gt;, making it both an enumerable and enumerator at the same time. Our previous examples could have done this, but I separated them out for clarity. If you wanted to do this in the previous examples, just have GetEnumerator() return “this”.&lt;br /&gt;&lt;br /&gt;There’s a bunch of thread-safe code going on underneath those method signatures, so try it on your own to see what’s happening – I’ll spare the details here.&lt;br /&gt;&lt;br /&gt;Nonetheless, the code does what you would expect – it prints 1 through 10 and stops. As you can see, the yield keyword can save you a ton of code. Here’s another extension method to multiply each element so you can better see the benefits of yield:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;public static IEnumerable&amp;lt;int&amp;gt; Multiply(this IEnumerable&amp;lt;int&amp;gt; value, int multiplier)&lt;br /&gt;{&lt;br /&gt;    foreach (int v in value)&lt;br /&gt;        yield return v * multiplier;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Now we can use the two together like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;var dbl = 1.To(10).Multiply(2);&lt;br /&gt;&lt;br /&gt;foreach (var i in dbl)&lt;br /&gt;    Console.WriteLine(i);&lt;/pre&gt;&lt;br /&gt;I hope this helps your understanding of IEnumerable, IEnumerable&amp;lt;T&amp;gt;, IEnumerator, IEnumerator&amp;lt;T&amp;gt;, and the “yield” keyword.&lt;br /&gt;&lt;br /&gt;Aside: Sadly the “yield” keyword has no equivalent in VB. It’s a feature that’s been sorely missing, and is still missing in .NET 4.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Update:&lt;/span&gt; I realized that in the IDisposable.Dispose() method in the above examples, it should be calling Reset(). The reason for this is that many times IEnumerator&amp;lt;T&amp;gt; is used in a "using" block such that once it is disposed, it should be reset.</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/8572763756655870622/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=8572763756655870622" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/8572763756655870622?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/8572763756655870622?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2010/06/understanding-cs-yield-keyword-and.html" title="Understanding C#’s “yield” Keyword and IEnumerable&amp;lt;T&amp;gt;" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_xvXvGivrKF0/TAVpJuuvDzI/AAAAAAAAAVY/1PTtyv346wo/s72-c/image_thumb%5B2%5D.png?imgmax=800" height="72" width="72" /><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;DkANQnw6eyp7ImA9WxNVEUs.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-2337315334862989511</id><published>2009-10-21T18:36:00.002-04:00</published><updated>2009-10-21T18:39:53.213-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-21T18:39:53.213-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="VS2010" /><title>VS2010: A First Look at Beta 2 for ASP.NET – Part 1</title><content type="html">&lt;p&gt;If you haven’t yet downloaded Visual Studio 2010 Beta 2, &lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;what are you waiting for?&lt;/a&gt; Until then, here’s a first look at what’s new for ASP.NET developers with .NET 4 and Visual Studio 2010.&lt;/p&gt;  &lt;h2&gt;Visual Changes&lt;/h2&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/St-MwRbi2wI/AAAAAAAAAT4/inI9GhnJFaU/s1600-h/image%5B4%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/St-MxOT2DII/AAAAAAAAAUA/6o6evUWZKTE/image_thumb%5B2%5D.png?imgmax=800" border="0" height="393" width="538" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;First off, there’s a new splash screen and icon! I know, I know, this really isn’t important for day-to-day use. But I feel it reflects Microsoft’s renewed commitment to great user experience. Personally, I absolutely love it. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/St-MxYhPcCI/AAAAAAAAAUE/ixsNB496EHM/s1600-h/image%5B7%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/St-Mxs4SSAI/AAAAAAAAAUI/8JhkWKrbqjI/image_thumb%5B3%5D.png?imgmax=800" border="0" height="43" width="71" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This is the new icon that shows up in the taskbar.&lt;/p&gt;  &lt;p&gt; &lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/St-MyCeqqaI/AAAAAAAAAUM/mc0p1g1EWm0/s1600-h/image%5B11%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/St-MyUWxk8I/AAAAAAAAAUQ/obgP4YbNj4M/image_thumb%5B5%5D.png?imgmax=800" border="0" height="452" width="538" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Also there’s a new theme for the overall UI. A bit darker, and 100% more awesome. Even the menus are in WPF, so it feels more “2010” and less “2003”.&lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;New ASP.NET Default Template&lt;/h2&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/St-MynQlTzI/AAAAAAAAAUU/JBDI0Dbm1LQ/s1600-h/image%5B16%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/St-Myk_9NMI/AAAAAAAAAUY/rmM8wZkWtCE/image_thumb%5B8%5D.png?imgmax=800" border="0" height="317" width="536" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;When you do File –&amp;gt; New Project and choose the ASP.NET Web Application project template, you now get a much more robust project template by default for new web forms projects, complete with authentication, much like the default ASP.NET MVC project template.&lt;/p&gt;  &lt;h2&gt;Snippets for ASPX Markup&lt;/h2&gt;  &lt;p&gt; &lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/St-My-_BOFI/AAAAAAAAAUc/rNXT4sBgi9w/s1600-h/image%5B26%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/St-MzOZhvLI/AAAAAAAAAUg/3YqzDJxm9xE/image_thumb%5B12%5D.png?imgmax=800" border="0" height="101" width="306" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The awesome snippets that make coding in C# and VB more fun have made their way to ASPX markup. Start typing “&amp;lt;button” and press tab twice to get a button snippet like this. One annoying issue I’ve noticed is that trying to type a normal &amp;lt;a&amp;gt; tag is painful, because it tries to put in an &amp;lt;accessdatasource&amp;gt; snippet instead. Hopefully this will be resolved by the next Beta/RC.&lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;Client ID Mode&lt;/h2&gt;  &lt;p&gt;There is a new ClientIDMode attribute that can be applied to most server controls that changes the output ID to make it more JavaScript and CSS friendly. Given the following ASPX code:&lt;/p&gt;  &lt;pre class="brush: xml;"&gt;&amp;lt;asp:Panel ID="pnlContent" runat="server" ClientIDMode="Static"&amp;gt;&lt;br /&gt;  This is some content&lt;br /&gt;&amp;lt;/asp:Panel&amp;gt;&lt;/pre&gt;The following HTML is outputted:&lt;br /&gt;&lt;pre class="brush: xml;"&gt;&amp;lt;div id="pnlContent"&amp;gt;&lt;br /&gt;  This is some content  &lt;br /&gt;&amp;lt;/div&amp;gt;&lt;/pre&gt;As compared to the following HTML output without ClientIDMode=”Static”:&lt;br /&gt;&lt;pre class="brush: xml;"&gt;&amp;lt;div id="MainContent_pnlContent"&amp;gt;&lt;br /&gt;  This is some content&lt;br /&gt;&amp;lt;/div&amp;gt;&lt;/pre&gt;Which, as you know, if that pnlContent panel was nested 5 layers deep in controls, you would see each of those layers’ IDs present in its ID. This makes the IDs of the elements predictable for use in JavaScript and CSS.&lt;br /&gt;&lt;h2&gt;HTML Encoding Block Syntax&lt;/h2&gt;Especially when dealing with ASP.NET MVC, but also with WebForms, you end up needing to HTML encode output when using the ASP-style blocks. With the following code-behind:&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;  FooterContent = "Hello &amp;lt;World&amp;gt; !";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public string FooterContent { get; set; }&lt;/pre&gt;And the following ASPX page:&lt;br /&gt;&lt;pre class="brush: xml;"&gt;&amp;lt;%= FooterContent %&amp;gt;&lt;/pre&gt;We get the following output:&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/St-MzH89hVI/AAAAAAAAAUk/ydBiUemiXSk/s1600-h/image%5B29%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/St-MzVa8ZkI/AAAAAAAAAUo/JmoKnXzvYXY/image_thumb%5B13%5D.png?imgmax=800" border="0" height="91" width="187" /&gt;&lt;/a&gt; &lt;/p&gt;But if we wanted to HTML encode that output, we previously would need to do this:&lt;br /&gt;&lt;pre class="brush: xml;"&gt;&amp;lt;%= Server.HtmlEncode(FooterContent) %&amp;gt;&lt;/pre&gt;Now, we can use a new block syntax to do the same, without the function call:&lt;br /&gt;&lt;pre class="brush: xml;"&gt;&amp;lt;%: FooterContent %&amp;gt;&lt;/pre&gt;And now we get properly escaped HTML output:&lt;br /&gt;&lt;pre class="brush: xml;"&gt;Hello &amp;amp;lt;World&amp;amp;gt; !&lt;/pre&gt;&lt;span style="font-family:Georgia;"&gt;Which shows up properly in our browser as we would expect:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/St-Mzl3zg5I/AAAAAAAAAUs/13OA0Xo55Qc/s1600-h/image%5B32%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/St-M0NYl24I/AAAAAAAAAUw/yVkHaIityxU/image_thumb%5B14%5D.png?imgmax=800" border="0" height="85" width="161" /&gt;&lt;/a&gt;</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/2337315334862989511/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=2337315334862989511" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/2337315334862989511?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/2337315334862989511?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2009/10/vs2010-first-look-at-beta-2-for-aspnet.html" title="VS2010: A First Look at Beta 2 for ASP.NET – Part 1" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_xvXvGivrKF0/St-MxOT2DII/AAAAAAAAAUA/6o6evUWZKTE/s72-c/image_thumb%5B2%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C0INQnY7eip7ImA9WxNXFE0.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-6295539124135623707</id><published>2009-09-30T18:30:00.002-04:00</published><updated>2009-10-01T08:53:13.802-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-01T08:53:13.802-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dependency injection" /><category scheme="http://www.blogger.com/atom/ns#" term="mvc" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="ninject" /><title>Creating Your Own IoC Container: Part 2</title><content type="html">&lt;p&gt;In the &lt;a href="http://adventuresdotnet.blogspot.com/2009/09/creating-your-own-ioc-container.html"&gt;first part&lt;/a&gt; of this post series (which, honestly, I didn’t intend to need a Part 2), I discussed how you can easily create your own container for basic inversion of control (IoC) needs. However, many people may need to use Dependency Injection (DI) as well, so I’ve modified the project to support DI.&lt;/p&gt;  &lt;p&gt;Also, a colleague pointed out that there’s another reason why you may want to do this – to simply see how it all works. Sure, you can plop Ninject into a project and start using DI/IoC, but creating your own makes you realize what’s really involved and going on behind-the-scenes.&lt;/p&gt;  &lt;p&gt;Here I’ve modified the previous example to support DI, and it took less than an hour. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; I have only briefly tested this code, and have not created unit tests. Before using in production, I’d recommend testing it thoroughly. You’ll also need to get the previous post working on your end before integrating these features. I also noticed a bug after publishing this post reading through the code that if you request an IFoo, and Foo : IFoo has a constructor that is asking for an IFoo parameter, you'll end up in an endless loop and probably stack overflow. Ideally this should detect a circular Resolve() call, and throw an exception.&lt;br /&gt;&lt;/p&gt;  &lt;h2&gt;The IServiceResolver Interface&lt;/h2&gt;  &lt;p&gt;This bit is not required, however I realized as I was writing the DependencyInjector class below that I was tightly coupling it with ServiceResolver from the previous post. Since this is very much an anti-pattern, I decided to make ServiceResolver implement an interface, so that you can use your own IServiceResolver implementation with this DependencyInjector class. Here’s the interface:&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace Core&lt;br /&gt;{&lt;br /&gt;   public interface IServiceResolver&lt;br /&gt;   {&lt;br /&gt;       void Register&amp;lt;TFrom, TTo&amp;gt;();&lt;br /&gt;       T Resolve&amp;lt;T&amp;gt;();&lt;br /&gt;       object Resolve(Type fromType);&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Notice that I’ve added a non-generic version of this class. This was done for compatibility, and now the ServiceResolver class looks like the following. The major changes are it now implements the IServiceResolver interface, and there is the new non-generic method which is called by the generic method. Also, it does not throw an exception if there has not been a registration for the given type. Instead, it creates a new object and looks for any injections. This is to remove the need to bind items to themselves. Note that there is still a tight coupling in the default constructor to DependencyInjector. You may wish to remove this. &lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;&lt;br /&gt;namespace Core&lt;br /&gt;{&lt;br /&gt;   /// &amp;lt;summary&amp;gt;&lt;br /&gt;   /// Resolves abstract types or interfaces to concrete implementations. Use &amp;lt;see cref="ServiceLocator" /&amp;gt; for static/shared access.&lt;br /&gt;   /// &amp;lt;/summary&amp;gt;&lt;br /&gt;   public class ServiceResolver : IServiceResolver&lt;br /&gt;   {&lt;br /&gt;       private Dictionary&amp;lt;Type, object&amp;gt; _store;&lt;br /&gt;       private Dictionary&amp;lt;Type, Type&amp;gt; _bindings;&lt;br /&gt;      &lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Default constructor; instantiates a new ServiceResolver object.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       public ServiceResolver()&lt;br /&gt;       {&lt;br /&gt;           this.DependencyInjector = new DependencyInjector(this);&lt;br /&gt;           _store = new Dictionary&amp;lt;Type, object&amp;gt;();&lt;br /&gt;           _bindings = new Dictionary&amp;lt;Type, Type&amp;gt;();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Creates a new ServiceResolver with a given IDependencyInjector.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;param name="injector"&amp;gt;The IDependencyInjector to use for dependency injection.&amp;lt;/param&amp;gt;&lt;br /&gt;       public ServiceResolver(IDependencyInjector injector)&lt;br /&gt;       {&lt;br /&gt;           this.DependencyInjector = injector;&lt;br /&gt;           _store = new Dictionary&amp;lt;Type, object&amp;gt;();&lt;br /&gt;           _bindings = new Dictionary&amp;lt;Type, Type&amp;gt;();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets an implementation object of a registered abstract type or interface.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;typeparam name="T"&amp;gt;The registered abstract type or interface to look up.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;       /// &amp;lt;returns&amp;gt;Returns an object of the given type.&amp;lt;/returns&amp;gt;&lt;br /&gt;       public T Resolve&amp;lt;T&amp;gt;()&lt;br /&gt;       {&lt;br /&gt;           return (T)Resolve(typeof(T));&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets an implementation object of a registered abstract type or interface.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;param name="fromType"&amp;gt;The registered abstract type or interface to look up.&amp;lt;/param&amp;gt;&lt;br /&gt;       /// &amp;lt;returns&amp;gt;Returns an object of the given type.&amp;lt;/returns&amp;gt;&lt;br /&gt;       public object Resolve(Type fromType)&lt;br /&gt;       {&lt;br /&gt;           // check for registration&lt;br /&gt;           if (!_bindings.ContainsKey(fromType))&lt;br /&gt;               return DependencyInjector.GetInjectedInstance(fromType);&lt;br /&gt;              &lt;br /&gt;           // get destination type&lt;br /&gt;           Type dest = _bindings[fromType];&lt;br /&gt;&lt;br /&gt;           // check for already requested object&lt;br /&gt;           if (_store.ContainsKey(dest))&lt;br /&gt;               return _store[dest];&lt;br /&gt;&lt;br /&gt;           // create a new instance of this type&lt;br /&gt;           object obj = DependencyInjector.GetInjectedInstance(dest);&lt;br /&gt;&lt;br /&gt;           // add to store for future use&lt;br /&gt;           _store.Add(dest, obj);&lt;br /&gt;&lt;br /&gt;           return obj;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Registers a type with its corresponding implementation type.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;typeparam name="TFrom"&amp;gt;The abstract type or interface to use as a key.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;       /// &amp;lt;typeparam name="TTo"&amp;gt;The implementation type to use as a value.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;       public void Register&amp;lt;TFrom, TTo&amp;gt;()&lt;br /&gt;       {&lt;br /&gt;           _bindings.Add(typeof(TFrom), typeof(TTo));&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets or sets a dependency injector to use for types that need injection.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       public IDependencyInjector DependencyInjector { get; set; }&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;The InjectAttribute class&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In order to tell our DependencyInjector class that we want to inject something, we need to decorate either a constructor or some properties (or both) with an [Inject] attribute. If you’ve never created your own attributes, all you have to do is create a new class with a name that ends in Attribute (the “Attribute” bit is cut off on use) and make it inherit from Attribute. Here’s our empty InjectAttribute class:&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;&lt;br /&gt;namespace Core&lt;br /&gt;{&lt;br /&gt;   [AttributeUsage(AttributeTargets.Property | AttributeTargets.Constructor)]&lt;br /&gt;   public class InjectAttribute : Attribute&lt;br /&gt;   {&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;h2&gt;the IDependencyInjector Interface&lt;/h2&gt;&lt;br /&gt;&lt;p&gt;Since we want to make our DependencyInjector loosely coupled with any use of it, we want to make it implement an interface.&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace Core&lt;br /&gt;{&lt;br /&gt;   public interface IDependencyInjector&lt;br /&gt;   {&lt;br /&gt;       T GetInjectedInstance&amp;lt;T&amp;gt;() where T : class;&lt;br /&gt;       object GetInjectedInstance(Type fromType);&lt;br /&gt;       IServiceResolver ServiceResolver { get; set; }&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;h2&gt;The DependencyInjector Class&lt;/h2&gt;&lt;br /&gt;&lt;p&gt;Now that we have all of the plumbing out of the way, we can show off our new DependencyInjector class.&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Reflection;&lt;br /&gt;&lt;br /&gt;namespace Core&lt;br /&gt;{&lt;br /&gt;   /// &amp;lt;summary&amp;gt;&lt;br /&gt;   /// A class for getting instances of objects that need dependencies injected to function.&lt;br /&gt;   /// &amp;lt;/summary&amp;gt;&lt;br /&gt;   public class DependencyInjector : IDependencyInjector&lt;br /&gt;   {&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Creates a new DependencyInjector with a default IServiceResolver for resolving types.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       public DependencyInjector() : this(new ServiceResolver())&lt;br /&gt;       { }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Creates a new DependencyInjector with a given IServiceResolver for resolving types.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;param name="resolver"&amp;gt;The IServiceResolver to use for resolving types to implementations.&amp;lt;/param&amp;gt;&lt;br /&gt;       public DependencyInjector(IServiceResolver resolver)&lt;br /&gt;       {&lt;br /&gt;           this.ServiceResolver = resolver;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets or sets an IServiceResolver to use for resolving types to implementations.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       public IServiceResolver ServiceResolver { get; set; }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets an injected instance of a given type.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;typeparam name="T"&amp;gt;The type to instantiate and inject.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;       /// &amp;lt;returns&amp;gt;Returns a new instance of the given type.&amp;lt;/returns&amp;gt;&lt;br /&gt;       public virtual T GetInjectedInstance&amp;lt;T&amp;gt;() where T : class&lt;br /&gt;       {&lt;br /&gt;           return (T)GetInjectedInstance(typeof(T));&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets an injected instance of a given type.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;param name="fromType"&amp;gt;The type to instantiate and inject.&amp;lt;/param&amp;gt;&lt;br /&gt;       /// &amp;lt;returns&amp;gt;Returns a new instance of the given type.&amp;lt;/returns&amp;gt;&lt;br /&gt;       public virtual object GetInjectedInstance(Type fromType)&lt;br /&gt;       {&lt;br /&gt;           object obj = null;&lt;br /&gt;&lt;br /&gt;           foreach (var constructor in fromType.GetConstructors())&lt;br /&gt;           {&lt;br /&gt;               // look for inject attribute&lt;br /&gt;               var attr = GetConstructorInjectAttribute(constructor);&lt;br /&gt;&lt;br /&gt;               if (attr != null)&lt;br /&gt;               {&lt;br /&gt;                   // get parameters to inject&lt;br /&gt;                   var parmValues = GetResolvedParameterValues(constructor);&lt;br /&gt;&lt;br /&gt;                   if (parmValues.Count &amp;gt; 0)&lt;br /&gt;                       obj = Activator.CreateInstance(fromType, parmValues.ToArray());&lt;br /&gt;&lt;br /&gt;                   break;&lt;br /&gt;               }&lt;br /&gt;           }&lt;br /&gt;&lt;br /&gt;           // handle case where no constructor injections&lt;br /&gt;           if (obj == null)&lt;br /&gt;               obj = Activator.CreateInstance(fromType);&lt;br /&gt;&lt;br /&gt;           foreach (var prop in fromType.GetProperties())&lt;br /&gt;           {&lt;br /&gt;               // look for inject attribute&lt;br /&gt;               var attr = GetPropertyInjectAttribute(prop);&lt;br /&gt;&lt;br /&gt;               if (attr != null)&lt;br /&gt;                   prop.SetValue(obj, ServiceResolver.Resolve(prop.PropertyType), new object[] { });&lt;br /&gt;           }&lt;br /&gt;&lt;br /&gt;           return obj;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets an InjectAttribute (if any) for the given constructor.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;param name="constructor"&amp;gt;The constructor to inspect for an InjectAttribute.&amp;lt;/param&amp;gt;&lt;br /&gt;       /// &amp;lt;returns&amp;gt;Returns an InjectAttribute if exists, or null if not.&amp;lt;/returns&amp;gt;&lt;br /&gt;       protected virtual InjectAttribute GetConstructorInjectAttribute(ConstructorInfo constructor)&lt;br /&gt;       {&lt;br /&gt;           var attrs = constructor.GetCustomAttributes(typeof(InjectAttribute), true);&lt;br /&gt;           return attrs.OfType&amp;lt;InjectAttribute&amp;gt;().FirstOrDefault();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets an InjectAttribute (if any) for the given property.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;param name="prop"&amp;gt;The property to inspect for an InjectAttribute.&amp;lt;/param&amp;gt;&lt;br /&gt;       /// &amp;lt;returns&amp;gt;Returns an InjectAttribute if exists, or null if not.&amp;lt;/returns&amp;gt;&lt;br /&gt;       protected virtual InjectAttribute GetPropertyInjectAttribute(PropertyInfo prop)&lt;br /&gt;       {&lt;br /&gt;           var attrs = prop.GetCustomAttributes(typeof(InjectAttribute), true);&lt;br /&gt;           return attrs.OfType&amp;lt;InjectAttribute&amp;gt;().FirstOrDefault();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       /// &amp;lt;summary&amp;gt;&lt;br /&gt;       /// Gets a list of parameter values for the given constructor, resolved by the ServiceResolver.&lt;br /&gt;       /// &amp;lt;/summary&amp;gt;&lt;br /&gt;       /// &amp;lt;param name="constructor"&amp;gt;The constructor to resolve parameters for.&amp;lt;/param&amp;gt;&lt;br /&gt;       /// &amp;lt;returns&amp;gt;Returns a new list of parameter values.&amp;lt;/returns&amp;gt;&lt;br /&gt;       protected virtual List&amp;lt;object&amp;gt; GetResolvedParameterValues(ConstructorInfo constructor)&lt;br /&gt;       {&lt;br /&gt;           var parms = constructor.GetParameters();&lt;br /&gt;           List&amp;lt;object&amp;gt; parmValues = new List&amp;lt;object&amp;gt;();&lt;br /&gt;&lt;br /&gt;           foreach (var parm in parms)&lt;br /&gt;           {&lt;br /&gt;               if (parm.ParameterType.IsInterface || parm.ParameterType.IsAbstract)&lt;br /&gt;               {&lt;br /&gt;                   Type parmType = parm.ParameterType;&lt;br /&gt;                   object parmValue = ServiceResolver.Resolve(parmType);&lt;br /&gt;                   parmValues.Add(parmValue);&lt;br /&gt;               }&lt;br /&gt;               else&lt;br /&gt;                   throw new InvalidOperationException("Unable to inject a parameter that is not an interface or abstract type.");&lt;br /&gt;           }&lt;br /&gt;&lt;br /&gt;           return parmValues;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;I’m not going to cover line-by-line what this class does, but basically it looks for any InjectAttributes decorating a constructor or properties, and if it finds them, it resolves instances automatically using the ServiceLocator (another tight coupling) and fills them in.&lt;/p&gt;&lt;br /&gt;&lt;h2&gt;Usage&lt;/h2&gt;&lt;br /&gt;&lt;p&gt;Back in our ASP.NET MVC test app, we have created a new interface to use for testing, ILogger. It has one implementation (but could have many), and this one is WebLogger which writes out the log message to the current HttpResponse.&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace CustomIoCMvc.Models&lt;br /&gt;{&lt;br /&gt;   public interface ILogger&lt;br /&gt;   {&lt;br /&gt;       void Log(string message);&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;And here’s our WebLogger:&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;using System.Web;&lt;br /&gt;&lt;br /&gt;namespace CustomIoCMvc.Models&lt;br /&gt;{&lt;br /&gt;   public class WebLogger : ILogger&lt;br /&gt;   {&lt;br /&gt;       public void Log(string message)&lt;br /&gt;       {&lt;br /&gt;           HttpContext.Current.Response.Write(message);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-family:Georgia;"&gt;We, of course, need to wire up this implementation in our Global.asax.cs:&lt;/span&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;protected void Application_Start()&lt;br /&gt;{&lt;br /&gt;   RegisterRoutes(RouteTable.Routes);&lt;br /&gt;  &lt;br /&gt;   // register types&lt;br /&gt;   ServiceLocator.Register&amp;lt;ICustomerRepository, CustomerRepository&amp;gt;();&lt;br /&gt;   ServiceLocator.Register&amp;lt;ILogger, WebLogger&amp;gt;();&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Our CustomerRepository is going to now use an ILogger for all of its calls. Here it is with a Constructor injection:&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Web;&lt;br /&gt;using System.Web.Security;&lt;br /&gt;using System.Web.UI;&lt;br /&gt;using System.Web.UI.HtmlControls;&lt;br /&gt;using System.Web.UI.WebControls;&lt;br /&gt;using System.Web.UI.WebControls.WebParts;&lt;br /&gt;using System.Xml.Linq;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using Core;&lt;br /&gt;&lt;br /&gt;namespace CustomIoCMvc.Models&lt;br /&gt;{&lt;br /&gt;   public class CustomerRepository : ICustomerRepository&lt;br /&gt;   {&lt;br /&gt;       [Inject]&lt;br /&gt;       public CustomerRepository(ILogger logger)&lt;br /&gt;       {&lt;br /&gt;           this.Logger = logger;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       public Customer Find(int id)&lt;br /&gt;       {&lt;br /&gt;           Logger.Log("Find called with param value " + id.ToString());&lt;br /&gt;           return FindAll().Where(c =&amp;gt; c.Id == id).FirstOrDefault();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       public List&amp;lt;Customer&amp;gt; FindAll()&lt;br /&gt;       {&lt;br /&gt;           Logger.Log("FindAll called.");&lt;br /&gt;           var data = new List&amp;lt;Customer&amp;gt;();&lt;br /&gt;           data.Add(new Customer() { Id = 1, Name = "Joe" });&lt;br /&gt;           data.Add(new Customer() { Id = 2, Name = "Steve" });&lt;br /&gt;           data.Add(new Customer() { Id = 3, Name = "Karen" });&lt;br /&gt;           return data;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       public ILogger Logger { get; set; }&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;And here’s our CustomerRepository with Property injection:&lt;/p&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;using System;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Configuration;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Web;&lt;br /&gt;using System.Web.Security;&lt;br /&gt;using System.Web.UI;&lt;br /&gt;using System.Web.UI.HtmlControls;&lt;br /&gt;using System.Web.UI.WebControls;&lt;br /&gt;using System.Web.UI.WebControls.WebParts;&lt;br /&gt;using System.Xml.Linq;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using Core;&lt;br /&gt;&lt;br /&gt;namespace CustomIoCMvc.Models&lt;br /&gt;{&lt;br /&gt;   public class CustomerRepository : ICustomerRepository&lt;br /&gt;   {&lt;br /&gt;&lt;br /&gt;       public Customer Find(int id)&lt;br /&gt;       {&lt;br /&gt;           Logger.Log("Find called with param value " + id.ToString());&lt;br /&gt;           return FindAll().Where(c =&amp;gt; c.Id == id).FirstOrDefault();&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       public List&amp;lt;Customer&amp;gt; FindAll()&lt;br /&gt;       {&lt;br /&gt;           Logger.Log("FindAll called.");&lt;br /&gt;           var data = new List&amp;lt;Customer&amp;gt;();&lt;br /&gt;           data.Add(new Customer() { Id = 1, Name = "Joe" });&lt;br /&gt;           data.Add(new Customer() { Id = 2, Name = "Steve" });&lt;br /&gt;           data.Add(new Customer() { Id = 3, Name = "Karen" });&lt;br /&gt;           return data;&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;       [Inject]&lt;br /&gt;       public ILogger Logger { get; set; }&lt;br /&gt;   }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Both have been tested with the same result. But first, a little background about what’s going on here.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Our CustomersController is requesting from the service locator an instance of ICustomerRepository. It finds that we’ve bound ICustomerRepository to CustomerRepository, and goes to instantiate that class. What we’ve modified our code to do is to look, before we instantiate, if there’s a constructor with an [Inject] attribute. If there is, we fill in the constructor parameters with the needed types. If not, we go ahead and create the object with the default constructor.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Next, after we’ve instantiated the object, it looks for any properties with an [Inject] attribute, and does the same. Finally, it returns the object.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Therefore, in this case, our DependencyInjector is getting a request for a new CustomerRepository, it looks for a constructor with [Inject], sees one (in the first example) and resolves ILogger to the bound WebLogger type, gets an instance of WebLogger, and fills it in to the constructor. In the second example, it uses a default constructor but finds the [Inject] decorated Logger property, resolves ILogger to WebLogger, and sets it.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;So here’s our output. You can’t really see it (because it’s a cheap quick hack), but theres a “FindAll called.” string at the top of the page:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/SsPcF5Y7ugI/AAAAAAAAATw/l_ExVJeGFyY/s1600-h/image%5B3%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/SsPcGLYlEJI/AAAAAAAAAT0/l0bw6Q_H77I/image_thumb%5B1%5D.png?imgmax=800" border="0" height="282" width="340" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Let me know in the comments if you have any issues or constructive criticism of this code. Hope this helps!&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/6295539124135623707/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=6295539124135623707" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/6295539124135623707?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/6295539124135623707?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2009/09/creating-your-own-ioc-container-part-2.html" title="Creating Your Own IoC Container: Part 2" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_xvXvGivrKF0/SsPcGLYlEJI/AAAAAAAAAT0/l0bw6Q_H77I/s72-c/image_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;DUQNQ307cSp7ImA9WxNQF0g.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-2681770129599488502</id><published>2009-09-23T21:55:00.002-04:00</published><updated>2009-09-23T21:56:32.309-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-23T21:56:32.309-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="dependency injection" /><category scheme="http://www.blogger.com/atom/ns#" term="mvc" /><title>Creating Your Own IoC Container</title><content type="html">&lt;p&gt;A few posts ago I covered how to take your &lt;a href="http://adventuresdotnet.blogspot.com/2009/08/first-steps-into-dependency-injection.html"&gt;first steps into Dependency Injection with Ninject&lt;/a&gt;. Continuing on the DI/IoC theme, let’s now look at how you can create your own Inversion of Control (IoC) container.&lt;/p&gt;  &lt;p&gt;Suppose that your organization has very strict limits on what outside software you can use due to licensing, but you still want to do IoC. Or, perhaps you don’t need your IoC container to do very much other than the basics and you don’t want the weight of another full-featured DLL. This is what we will be creating today, with 2 classes: a &lt;strong&gt;ServiceResolver&lt;/strong&gt; and a &lt;strong&gt;ServiceLocator&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;(Aside: For those that feel I’m using the term “Service Locator” inappropriately, please forgive me. For this example that’s the name I came up with.)&lt;/p&gt;  &lt;p&gt;A basic IoC container needs to do the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Register abstract types or interfaces to concrete implementation types &lt;/li&gt;    &lt;li&gt;Resolve abstract types or interfaces to implementation objects &lt;/li&gt;    &lt;li&gt;Keep track of the registered types &lt;/li&gt;    &lt;li&gt;Keep already-resolved objects available for re-use &lt;/li&gt;    &lt;li&gt;Create instances of the implementation types behind-the-scenes &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Our two classes will accomplish that, but only one has most of the logic – the &lt;strong&gt;ServiceResolver&lt;/strong&gt;. The reason for the ServiceLocator class is to provide static methods around the ServiceResolver, and maintain a static instance of the resolver. Since static methods are difficult to test, this allows you to test the functionality of the ServiceResolver class, and trust that the basic wrapper functionality of the ServiceLocator just works.&lt;/p&gt;  &lt;h2&gt;The Light At The End&lt;/h2&gt;  &lt;p&gt;Sometimes it’s fun to see what we’re aiming to do, instead of starting from the beginning, right? Basically we’re going to have a simple ASP.NET MVC site that is going to have a Customers controller. The Index action will ask the service locator for an implementation of ICustomerRepository so that it is loosely coupled. In our Global.asax class, we will register the binding of ICustomerRepository to CustomerRepository. The reason for this is we are pretending that CustomerRepository hits the database, so that for our unit tests we could create a mock repository that does not.&lt;/p&gt;  &lt;p&gt;Our Global.asax.cs file looks like this:&lt;/p&gt;  &lt;div style="padding: 5px; width: 549px; display: block; float: none; margin-left: auto; margin-right: auto;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f3e814e1-f196-430e-8b28-807bd34932bd" class="wlWriterEditableSmartContent"&gt;&lt;br /&gt;&lt;div   style="border: 1px solid rgb(0, 0, 128);font-family:'Courier New',Courier,Monospace;font-size:10pt;"&gt;&lt;br /&gt;&lt;div style="padding: 2px 5px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; overflow: auto; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; max-height: 300px;"&gt;&lt;br /&gt;&lt;ol style="margin: 0pt; background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;li&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;protected&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Application_Start()&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         {&lt;/li&gt;&lt;li&gt;             RegisterRoutes(&lt;span style="color: rgb(43, 145, 175);"&gt;RouteTable&lt;/span&gt;.Routes);&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 128, 0);"&gt;// register types&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;span style="color: rgb(43, 145, 175);"&gt;ServiceLocator&lt;/span&gt;.Register&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ICustomerRepository&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;CustomerRepository&lt;/span&gt;&amp;gt;();&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         }&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;  &lt;p&gt;Note that in a real-world app, you may wish to register types on each page request, not on application start.&lt;/p&gt;  &lt;p&gt;And our Index action looks like this:&lt;/p&gt;  &lt;div style="padding: 5px; width: 549px; display: block; float: none; margin-left: auto; margin-right: auto;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0596177b-ea61-4a2d-8cc4-d1aa1a92162a" class="wlWriterEditableSmartContent"&gt;&lt;br /&gt;&lt;div   style="border: 1px solid rgb(0, 0, 128);font-family:'Courier New',Courier,Monospace;font-size:10pt;"&gt;&lt;br /&gt;&lt;div style="padding: 2px 5px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; overflow: auto; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; max-height: 300px;"&gt;&lt;br /&gt;&lt;ol style="margin: 0pt; background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;li&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ActionResult&lt;/span&gt; Index()&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         {&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; repo = &lt;span style="color: rgb(43, 145, 175);"&gt;ServiceLocator&lt;/span&gt;.Resolve&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ICustomerRepository&lt;/span&gt;&amp;gt;();&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             ViewData[&lt;span style="color: rgb(163, 21, 21);"&gt;"customers"&lt;/span&gt;] = repo.FindAll();&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; View();&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         }&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;  &lt;h2&gt;The ServiceResolver Class&lt;/h2&gt;  &lt;p&gt;Let’s look at the ServiceResolver class that does all the heavy lifting (although there really isn’t much).&lt;/p&gt;  &lt;p&gt;   &lt;/p&gt;&lt;div style="padding: 5px; width: 549px; display: block; float: none; margin-left: auto; margin-right: auto;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7f137dd7-854b-4cee-b3a3-07370b6d1a7a" class="wlWriterEditableSmartContent"&gt;&lt;br /&gt;&lt;div   style="border: 1px solid rgb(0, 0, 128);font-family:'Courier New',Courier,Monospace;font-size:10pt;"&gt;&lt;br /&gt;&lt;div style="padding: 2px 5px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; overflow: auto; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; max-height: 500px;"&gt;&lt;br /&gt;&lt;ol style="margin: 0pt; background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;li&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt;&lt;li&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Linq;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Text;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;namespace&lt;/span&gt; Core&lt;/li&gt;&lt;li&gt; {&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;     &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Resolves abstract types or interfaces to concrete implementations. Use &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;see cref="ServiceLocator" /&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; for static/shared access.&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ServiceResolver&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     {&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&amp;gt; _store;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;&amp;gt; _bindings;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Default constructor; instantiates a new ServiceResolver object.&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; ServiceResolver()&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         {&lt;/li&gt;&lt;li&gt;             _store = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;&amp;gt;();&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             _bindings = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;&amp;gt;();&lt;/li&gt;&lt;li&gt;         }&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Gets an implementation object of a registered abstract type or interface.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;The registered abstract type or interface to look up.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;Returns an object of the given type.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;()&lt;/li&gt;&lt;li&gt;         {&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;span style="color: rgb(0, 128, 0);"&gt;// check for registration&lt;/span&gt;&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (!_bindings.ContainsKey(&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;(T)))&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;                 &lt;span style="color: rgb(0, 0, 255);"&gt;throw&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;InvalidOperationException&lt;/span&gt;(&lt;span style="color: rgb(0, 0, 255);"&gt;string&lt;/span&gt;.Format(&lt;span style="color: rgb(163, 21, 21);"&gt;"Requested type {0} has not been registered."&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;(T).ToString()));&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;span style="color: rgb(0, 128, 0);"&gt;// get destination type&lt;/span&gt;&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; dest = _bindings[&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;(T)];&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 128, 0);"&gt;// check for already requested object&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_store.ContainsKey(dest))&lt;/li&gt;&lt;li&gt;                 &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; (T)_store[dest];&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 128, 0);"&gt;// create a new instance of this type&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             T obj = (T)&lt;span style="color: rgb(43, 145, 175);"&gt;Activator&lt;/span&gt;.CreateInstance(dest);&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;span style="color: rgb(0, 128, 0);"&gt;// add to store for future use&lt;/span&gt;&lt;/li&gt;&lt;li&gt;             _store.Add(dest, obj);&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; obj;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         }&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Registers a type with its corresponding implementation type.&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;typeparam name="TFrom"&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;The abstract type or interface to use as a key.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;typeparam name="TTo"&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;The implementation type to use as a value.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Register&amp;lt;TFrom, TTo&amp;gt;()&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         {&lt;/li&gt;&lt;li&gt;             _bindings.Add(&lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;(TFrom), &lt;span style="color: rgb(0, 0, 255);"&gt;typeof&lt;/span&gt;(TTo));&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         }&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     }&lt;/li&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt; &lt;p&gt;&lt;/p&gt;  &lt;p&gt;First, notice the _store and _bindings members. Those are our ways of keeping track of registered types (_bindings) and reusable already-resolved objects (_store). &lt;/p&gt;  &lt;p&gt;The Register&amp;lt;TFrom, TTo&amp;gt;() method adds to the _bindings dictionary the “from” and “to” types to use later, whenever Resolve&amp;lt;T&amp;gt;() is called. Since we’re just storing types in the dictionary, there is little overhead and it’s okay to register all our types in advance.&lt;/p&gt;  &lt;p&gt;Finally, the Resolve&amp;lt;T&amp;gt;() method looks for a pre-registered type binding, and throws an error if it doesn’t find one. Then, it looks to see if an object has already been created for this type. If so, it returns it. If not, it creates an instance of the type and adds it to the _store dictionary before returning it.&lt;/p&gt;  &lt;p&gt;You can certainly use the ServiceResolver class by itself for IoC, but I prefer using static methods for conciseness. That’s why we need a static wrapper class, called ServiceLocator.&lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;h2&gt;The ServiceLocator Class&lt;/h2&gt;  &lt;p&gt;Here’s our ServiceLocator static wrapper class.&lt;/p&gt;  &lt;div style="padding: 5px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:53dc0c17-2ae3-4b2c-88ff-9368add6fcd9" class="wlWriterEditableSmartContent"&gt;&lt;br /&gt;&lt;div   style="border: 1px solid rgb(0, 0, 128);font-family:'Courier New',Courier,Monospace;font-size:10pt;"&gt;&lt;br /&gt;&lt;div style="padding: 2px 5px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; overflow: auto; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; max-height: 300px;"&gt;&lt;br /&gt;&lt;ol style="margin: 0pt; background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;li&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt;&lt;li&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Linq;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;using&lt;/span&gt; System.Text;&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;namespace&lt;/span&gt; Core&lt;/li&gt;&lt;li&gt; {&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;     &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; A service locator to resolve abstract types or interfaces to concrete implementations.&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ServiceLocator&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     {&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ServiceResolver&lt;/span&gt; _instance = &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt; _lock = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;object&lt;/span&gt;();&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Gets a shared instance of the ServiceResolver.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;Returns the shared instance of a ServiceResolver, or a new one if it has not yet been accessed.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ServiceResolver&lt;/span&gt; GetInstance()&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         {&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;lock&lt;/span&gt; (_lock)&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             {&lt;/li&gt;&lt;li&gt;                 &lt;span style="color: rgb(0, 0, 255);"&gt;if&lt;/span&gt; (_instance == &lt;span style="color: rgb(0, 0, 255);"&gt;null&lt;/span&gt;)&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;                     _instance = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ServiceResolver&lt;/span&gt;();&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;                 &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; _instance;&lt;/li&gt;&lt;li&gt;             }&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         }&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Binds an abstract type to a concrete implementation.&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;typeparam name="TFrom"&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;The abstract type or interface to use as a key.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;typeparam name="TTo"&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;The implementation type to use as a value.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;void&lt;/span&gt; Register&amp;lt;TFrom, TTo&amp;gt;()&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         {&lt;/li&gt;&lt;li&gt;             GetInstance().Register&amp;lt;TFrom, TTo&amp;gt;();&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         }&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; Gets an implementation of the given type.&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;The abstract type or interface to look up.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(128, 128, 128);"&gt;///&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;Returns an implementation of the given type.&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128);"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;static&lt;/span&gt; T Resolve&amp;lt;T&amp;gt;()&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         {&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; GetInstance().Resolve&amp;lt;T&amp;gt;();&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         }&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     }&lt;/li&gt;&lt;li&gt; }&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;  &lt;p&gt;Notice that we’ve got one static instance of the service resolver, and a GetInstance() method that instantiates it if it doesn’t exist. Also notice our Resolve and Register methods that have the same signature (albeit static) as our ServiceResolver class.&lt;/p&gt;  &lt;p&gt;This enables us to simply call ServiceLocator.Resolve&amp;lt;T&amp;gt;() without needing to find an instantiated resolver or create one.&lt;/p&gt;  &lt;p&gt;Just to show you that this was tested, here’s our view:&lt;/p&gt;  &lt;div style="padding: 5px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:79c2b56a-a624-4595-97e7-0f65ec34b36d" class="wlWriterEditableSmartContent"&gt;&lt;br /&gt;&lt;div   style="border: 1px solid rgb(0, 0, 128);font-family:'Courier New',Courier,Monospace;font-size:10pt;"&gt;&lt;br /&gt;&lt;div style="padding: 2px 5px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; overflow: auto; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; max-height: 300px;"&gt;&lt;br /&gt;&lt;ol style="margin: 0pt; background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;li&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;asp&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Content&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;ID&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="Content2"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="MainContent"&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;runat&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;="server"&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;h2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;Customers&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;h2&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &lt;/li&gt;&lt;li&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ul&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt; c &lt;span style="color: rgb(0, 0, 255);"&gt;in&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;&amp;gt;)ViewData[&lt;span style="color: rgb(163, 21, 21);"&gt;"customers"&lt;/span&gt;])&lt;/li&gt;&lt;li&gt;        { &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;li&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;=&lt;/span&gt; c.Name &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;li&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;     &lt;/li&gt;&lt;li&gt;     &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="background: rgb(255, 238, 98) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;ul&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;     &lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;asp&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;:&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;Content&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;  &lt;p&gt;Here’s our CustomerRepository:&lt;/p&gt;  &lt;div style="padding: 5px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c99d0f7e-8f3d-4eae-8a44-dd893ceb534c" class="wlWriterEditableSmartContent"&gt;&lt;br /&gt;&lt;div   style="border: 1px solid rgb(0, 0, 128);font-family:'Courier New',Courier,Monospace;font-size:10pt;"&gt;&lt;br /&gt;&lt;div style="padding: 2px 5px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; overflow: auto; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; max-height: 300px;"&gt;&lt;br /&gt;&lt;ol style="margin: 0pt; background: rgb(255, 255, 255) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;li&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;namespace&lt;/span&gt; CustomIoCMvc.Models&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt; {&lt;/li&gt;&lt;li&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;CustomerRepository&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;ICustomerRepository&lt;/span&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     {&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt; Find(&lt;span style="color: rgb(0, 0, 255);"&gt;int&lt;/span&gt; id)&lt;/li&gt;&lt;li&gt;         {&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; FindAll().Where(c =&amp;gt; c.Id == id).FirstOrDefault();&lt;/li&gt;&lt;li&gt;         }&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;         &lt;span style="color: rgb(0, 0, 255);"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;&amp;gt; FindAll()&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         {&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;var&lt;/span&gt; data = &lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;&amp;gt;();&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             data.Add(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;() { Id = 1, Name = &lt;span style="color: rgb(163, 21, 21);"&gt;"Joe"&lt;/span&gt; });&lt;/li&gt;&lt;li&gt;             data.Add(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;() { Id = 2, Name = &lt;span style="color: rgb(163, 21, 21);"&gt;"Steve"&lt;/span&gt; });&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;             data.Add(&lt;span style="color: rgb(0, 0, 255);"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;() { Id = 3, Name = &lt;span style="color: rgb(163, 21, 21);"&gt;"Karen"&lt;/span&gt; });&lt;/li&gt;&lt;li&gt;             &lt;span style="color: rgb(0, 0, 255);"&gt;return&lt;/span&gt; data;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;         }&lt;/li&gt;&lt;li&gt;&lt;br /&gt;&lt;/li&gt;&lt;li style="background: rgb(243, 243, 243) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     }&lt;/li&gt;&lt;li&gt; }&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;  &lt;p&gt;And here’s the output:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/SrrRiC_iTiI/AAAAAAAAATo/lyY8ygHa8j4/s1600-h/image%5B2%5D.png"&gt;&lt;img style="border: 0px none ; display: inline;" title="image" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SrrRjwdgDFI/AAAAAAAAATs/39mPWdIaVmQ/image_thumb.png?imgmax=800" border="0" height="217" width="244" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Hope this helps! Please leave constructive comments below. Also note that I'm still playing around with formatting code, please forgive me if it doesn't render correctly!&lt;br /&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/2681770129599488502/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=2681770129599488502" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/2681770129599488502?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/2681770129599488502?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2009/09/creating-your-own-ioc-container.html" title="Creating Your Own IoC Container" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_xvXvGivrKF0/SrrRjwdgDFI/AAAAAAAAATs/39mPWdIaVmQ/s72-c/image_thumb.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;DUMMRX49fip7ImA9WxNQFk4.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-8092806437523572168</id><published>2009-09-22T08:45:00.003-04:00</published><updated>2009-09-22T12:38:04.066-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-22T12:38:04.066-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="pair programming" /><title>Pair Programming in the News</title><content type="html">&lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SrjHADdDIhI/AAAAAAAAATg/K61J57IiXyU/s1600-h/20pre600.1%5B3%5D.jpg"&gt;&lt;img style="border: 0px none ; display: inline; margin-left: 0px; margin-right: 0px;" title="20pre600.1" alt="20pre600.1" src="http://lh3.ggpht.com/_xvXvGivrKF0/SrjHAaNVcYI/AAAAAAAAATk/CCO4kouu8Tk/20pre600.1_thumb%5B1%5D.jpg?imgmax=800" align="right" border="0" height="136" width="244" /&gt;&lt;/a&gt;I’ve been a huge fan of pair programming for a while now.  It really is more productive, and it effectively is a real-time code review as you write it. I’ve had the chance to do some pair programming here at the new gig, and it’s worked out really well. A new favorite quote of mine when paraphrased goes something like “You don’t learn from people that agree with you” and with pair programming that is definitely the case. You learn from the mistakes you make when your partner disagrees with what you just wrote.&lt;/p&gt;  &lt;p&gt;So I was very excited to find &lt;a href="http://www.nytimes.com/2009/09/20/jobs/20pre.html?src=tptw" target="_blank"&gt;an article about pair programming in the New York Times&lt;/a&gt;. Check it out and let me know in the comments what your thoughts are.&lt;/p&gt;  &lt;p&gt;I’ve got a few new discoveries with ASP.NET MVC and Dependency Injection that I’m gearing up some posts about, and as you may have noticed, I’m still trying to figure out the best way to get code into this blog as a formatted, copy-and-paste-able format.    &lt;/p&gt;&lt;p&gt;&lt;/p&gt;    &lt;p&gt;I’m looking forward to the next meetings (and my first) of &lt;a href="http://www.jaxdug.com/" target="_blank"&gt;JaxDUG&lt;/a&gt; (Jacksonville .NET User Group),  &lt;a href="http://jaxarcsig.spaces.live.com/default.aspx?sa=811254021" target="_blank"&gt;ArcSIG&lt;/a&gt; (for Architects), and the &lt;a href="http://jacksonville.sqlpass.org/" target="_blank"&gt;Jacksonvile SQL Server Users Group&lt;/a&gt;. I’m a big fan of community, and if you’ve never been to a local .NET user group, I strongly encourage you to give it a try.&lt;/p&gt;&lt;p&gt;&lt;span style="font-weight: bold;"&gt;Update:&lt;/span&gt; It appears that at least one person is &lt;a href="http://blog.obiefernandez.com/content/2009/09/10-reasons-pair-programming-is-not-for-the-masses.html"&gt;asserting a more pessimistic view&lt;/a&gt; of this article. I really am not a proponent of that kind of negative, borderline arrogant language, as I try to make this blog positive and forward-thinking for beginners as well as experts -- but it's an interesting read nonetheless.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/8092806437523572168/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=8092806437523572168" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/8092806437523572168?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/8092806437523572168?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2009/09/pair-programming-in-news.html" title="Pair Programming in the News" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_xvXvGivrKF0/SrjHAaNVcYI/AAAAAAAAATk/CCO4kouu8Tk/s72-c/20pre600.1_thumb%5B1%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkIHSX88cSp7ImA9WxNTF00.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-515813715962676330</id><published>2009-08-19T10:23:00.001-04:00</published><updated>2009-08-19T12:48:58.179-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-19T12:48:58.179-04:00</app:edited><title>ASP.NET: WebForms Validation with Data Annotations</title><content type="html">&lt;p&gt;Last night at my last regular trip to the &lt;a href="http://www.lakelandug.net" target="_blank"&gt;Lakeland .NET User Group&lt;/a&gt; I had a discussion with someone about how it’s great that Data Annotations is being used by &lt;a href="http://blogs.msdn.com/jimoneil/archive/2008/07/08/dynamic-data-annotations.aspx" target="_blank"&gt;Dynamic Data&lt;/a&gt;, &lt;a href="http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/04/14/Net-RIA-Services-DataForm-Validation.aspx" target="_blank"&gt;RIA services&lt;/a&gt;, and now &lt;a href="http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-preview-1-released.aspx" target="_blank"&gt;ASP.NET MVC 2&lt;/a&gt;, but that there isn’t a WebForms validation control (like the PropertyProxyValidator in the Validation block of &lt;a href="http://www.codeplex.com/entlib" target="_blank"&gt;Enterprise Library&lt;/a&gt;) to auto-validate against Data Annotations. I suggested we write one. I did so this morning.&lt;/p&gt;  &lt;p&gt;If you aren’t familiar with Data Annotations, it’s a &lt;em&gt;really&lt;/em&gt; cool feature of .NET 3.5 SP1. Check out those 3 links above (Dynamic Data, RIA, and MVC) for more info of how they’re used elsewhere.&lt;/p&gt;  &lt;h2&gt;The DataAnnotationValidator Class&lt;/h2&gt;  &lt;p&gt;Here’s the code for the Data Annotations Validator that I built for WebForms. I just created this as a class file in the App_Code folder of my web project for testing, but you’d probably want to move it into your shared library (if you have one). You’ll need to add a reference to System.ComponentModel.DataAnnotations first.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/SowK3UX9szI/AAAAAAAAARo/LCHS6IVsEyg/s1600-h/image%5B12%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/SowK4HpINiI/AAAAAAAAARs/qW7RLfFt1kw/image_thumb%5B6%5D.png?imgmax=800" width="530" height="373" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And there’s just 2 properties in this class:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/SowK4YvcSkI/AAAAAAAAARw/eC44QV4uAdA/s1600-h/image%5B7%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/SowK48jTFDI/AAAAAAAAAR0/HhoqWepsLrE/image_thumb%5B3%5D.png?imgmax=800" width="322" height="282" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;h2&gt;Data Annotating Your Class&lt;/h2&gt;  &lt;p&gt;Here’s a test class I wrote using some common annotations:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SowK5dqmNfI/AAAAAAAAAR4/b8huTMG24tE/s1600-h/image%5B13%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/SowK5_wxOqI/AAAAAAAAAR8/VqklhGjc7Qw/image_thumb%5B7%5D.png?imgmax=800" width="517" height="354" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here you see you can add multiple validators together (such as Required and StringLength in this example). And there’s also a RegularExpression validator:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/SowK6XPxN8I/AAAAAAAAASA/m7DCNnDGvQg/s1600-h/image%5B17%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SowK6qFznJI/AAAAAAAAASE/2Hmzn9temQE/image_thumb%5B9%5D.png?imgmax=800" width="520" height="264" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;Usage&lt;/h2&gt;  &lt;p&gt;To use this DataAnnotationValidator on your page, you must first register the namespace. (Note: you can also do this in the web.config to force it on all pages in your site.)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/SowK7IrSgHI/AAAAAAAAASI/FWw7TbVndfo/s1600-h/image%5B21%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/SowK7rUCAKI/AAAAAAAAASM/11BM0iRhCFk/image_thumb%5B11%5D.png?imgmax=800" width="455" height="137" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The namespace value must match the namespace of DataAnnotationValidator. &lt;/p&gt;  &lt;p&gt;To add a validator to a form field, just create a new &amp;lt;val:DataAnnotationValidator&amp;gt; tag for each field:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/SowK7xkGHtI/AAAAAAAAASQ/4MY4ODD7dXM/s1600-h/image%5B25%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SowK8TnQklI/AAAAAAAAASU/yj9MkEIXgMs/image_thumb%5B13%5D.png?imgmax=800" width="512" height="154" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Notice the SourceType property? That value must match the full type name of the object you’re validating.&lt;/p&gt;  &lt;p&gt;Now when you try and submit your form you get this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SowK82vY2CI/AAAAAAAAASY/-WMNudrmA1k/s1600-h/image%5B29%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SowK9QnwZkI/AAAAAAAAASc/7evWrpIu_4o/image_thumb%5B15%5D.png?imgmax=800" width="317" height="203" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;(In this example, the Address 2 field is not marked as required.)&lt;/p&gt;  &lt;p&gt;You can also simply add a ValidationSummary to the page to show what is wrong:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/SowK9u7HhbI/AAAAAAAAASg/jOEgERRmrGk/s1600-h/image%5B33%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SowK98RmdQI/AAAAAAAAASk/nF1j2fjvWok/image_thumb%5B17%5D.png?imgmax=800" width="422" height="98" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And now we get a helpful list of errors:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/SowK-fIU-fI/AAAAAAAAASo/khNB6BMFUGk/s1600-h/image%5B41%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SowK-pDXONI/AAAAAAAAASs/4eI2porj5rE/image_thumb%5B21%5D.png?imgmax=800" width="298" height="249" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You’re probably thinking, though, “what’s so special about this? I could just use a RequiredFieldValidator.” True, but we now have multiple validators per property with just one validator tag, and we don’t have to make the front end logic match the back end annotations. Separation of concerns!&lt;/p&gt;  &lt;p&gt;Here’s the RegularExpression data annotations validator at work:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/SowK_D2SLvI/AAAAAAAAASw/Sdv24kZicZM/s1600-h/image%5B45%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SowK_chojhI/AAAAAAAAAS0/SXzbvu9gIZY/image_thumb%5B23%5D.png?imgmax=800" width="374" height="341" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can see where this becomes handy – you annotate your class once, and you can then use it in RIA services, ASP.NET MVC 2, and now WebForms, and you don’t have to have any validation logic in your front end code. This idea could easily be modified for Windows Forms or WPF as well. You could also easily modify the code to set a CSS style for the textbox that would change the background color on error.&lt;/p&gt;  &lt;p&gt;Hope this helps! Let me know any questions or concerns in the comments.&lt;/p&gt;  &lt;p&gt;Also, I couldn’t have done this without some guidance from the Enterprise Library PropertyProxyValidator source code, and &lt;a href="http://stackoverflow.com/questions/370826/how-to-use-data-annotation-validators-in-winforms" target="_blank"&gt;this Stack Overflow question/answer&lt;/a&gt;.&lt;/p&gt;  &lt;h3&gt;Update: Using DisplayName&lt;/h3&gt;  &lt;p&gt;I realized that I was forgetting a key part of data annotations – that it supports the DisplayNameAttribute! This removes all of the string error messages from the class definition and generates them for you. Below are the modifications to support DisplayName. If a display name is not given, it will use the property name instead, which is great for single word property names like “City”.&lt;/p&gt;  &lt;p&gt;In your class file that you’re annotating, import System.ComponentModel.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/Sows5ywerHI/AAAAAAAAAS4/BO0D_x8fxT0/s1600-h/image%5B4%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/Sows6ZOo_NI/AAAAAAAAAS8/KKLzXK4NHa0/image_thumb%5B1%5D.png?imgmax=800" width="330" height="66" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;In the DataAnnotationValidator class, modify the routine to look up the DisplayName or property name like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/Sows6zb4V8I/AAAAAAAAATA/4umeUsI-ll4/s1600-h/image%5B8%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/Sows7exI4-I/AAAAAAAAATE/AozMqSlb-8c/image_thumb%5B3%5D.png?imgmax=800" width="537" height="204" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Also make sure you modify the Me.ErrorMessage line to use attr.FormatErrorMessage(displayName) like above.&lt;/p&gt;  &lt;p&gt;Now, you can yank out all of those error message strings! If you’ve got a single word property name, like Id, you can just do this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/Sows70Z7_SI/AAAAAAAAATI/3OeoCQe1Eqo/s1600-h/image%5B13%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/Sows8l7mNcI/AAAAAAAAATM/94FdQ-Hgj9U/image_thumb%5B6%5D.png?imgmax=800" width="231" height="138" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And if you need to add a display name, you can add the DisplayName() attribute:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/Sows82yKQjI/AAAAAAAAATQ/6B1yhJciwKo/s1600-h/image%5B17%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/Sows9Yz6YzI/AAAAAAAAATU/mv2bR5fxKyM/image_thumb%5B8%5D.png?imgmax=800" width="388" height="151" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Once we’ve made these modifications (which make the class definition much more concise), we get the following output on our form, without having to write our own error messages:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/Sows9pOsTVI/AAAAAAAAATY/dpsJKz_uysA/s1600-h/image%5B21%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/Sows-F1DwJI/AAAAAAAAATc/C1gZ_tSQmCM/image_thumb%5B10%5D.png?imgmax=800" width="305" height="209" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;You’ll still want to keep the error message specified for the RegularExpression annotation validator, because it actually shows the RegEx expression in the error message, which will confuse your users.&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/515813715962676330/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=515813715962676330" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/515813715962676330?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/515813715962676330?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2009/08/aspnet-webforms-validation-with-data.html" title="ASP.NET: WebForms Validation with Data Annotations" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_xvXvGivrKF0/SowK4HpINiI/AAAAAAAAARs/qW7RLfFt1kw/s72-c/image_thumb%5B6%5D.png?imgmax=800" height="72" width="72" /><thr:total>6</thr:total></entry><entry gd:etag="W/&quot;A0QCQXYzfyp7ImA9WxNTFUk.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-934836491316143880</id><published>2009-08-17T18:49:00.001-04:00</published><updated>2009-08-17T18:49:20.887-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-17T18:49:20.887-04:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="vb.net" /><category scheme="http://www.blogger.com/atom/ns#" term="dependency injection" /><category scheme="http://www.blogger.com/atom/ns#" term="ninject" /><title>First Steps into Dependency Injection with Ninject</title><content type="html">&lt;p&gt;I recently started playing around with dependency injection, but it was very much a by-hand process using constructor injection without any kind of framework. It worked well for my proof of concepts, but I had a breakthrough today when I tried Ninject. If you’re looking to get into DI/IoC, this hopefully will be a good place to start. I’m right there with you, I’m a newbie to DI as well.&lt;/p&gt;  &lt;p&gt;If you’re already confused by the first paragraph, Dependency Injection (hereafter called DI) and Inversion of Control (hereafter called IoC) are &lt;em&gt;almost&lt;/em&gt; synonyms for a development methodology where you aim to reduce coupling between classes as much as possible. Coupling means that one class depends on another, which increases complexity for large projects and almost ruins testability. For example, if your class needs to use a database connection, you might not want to couple it with SqlClient.SqlConnection, you might just want to couple it to the interface IDbConnection instead, so that it can accept any type of connection. (SqlConnection implements IDbConnection.) This makes your code more flexible for testing, because you can create a mock connection that implements IDbConnection and perform unit tests without hitting your database. This is just one example, although there are many reasons to use DI.&lt;/p&gt;  &lt;p&gt;While there are other methods of injection, I’m only going to talk about 2 in this post – &lt;strong&gt;constructor injection&lt;/strong&gt; and &lt;strong&gt;property injection&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;In this example, let’s say you have a Customer class that needs to use a database connection. Since you want to roll your own database connection wrapper classes, you also want to implement logging so you can keep track of your database requests.&lt;/p&gt;  &lt;p&gt;Let’s start out with a console application. &lt;/p&gt;  &lt;p&gt;&lt;em&gt;Disclaimer:&lt;/em&gt; This is demo code only. It doesn’t actually connect to a database, and does not implement best practices for security or anything else. Please keep that in mind!&lt;/p&gt;  &lt;h2&gt;Downloading Ninject&lt;/h2&gt;  &lt;p&gt;You can download Ninject and take a short tutorial at &lt;a href="http://www.ninject.org"&gt;www.ninject.org&lt;/a&gt;.&amp;#160; The download is a .zip file with all of the Ninject DLLs that you’ll ever need, but really we’re just concerned with the Ninject.Core assembly.&lt;/p&gt;  &lt;h2&gt;Add Ninject as a Reference&lt;/h2&gt;  &lt;p&gt;Create a new console application, and from the My Project pages, add the extracted Ninject.Core.dll file as a reference.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SoneQVn9x2I/AAAAAAAAAPo/n8kTEBIu-dE/s1600-h/image%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/SoneQ2t67wI/AAAAAAAAAPs/ahj2IDrzvT0/image_thumb%5B1%5D.png?imgmax=800" width="368" height="342" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;It’ll also make it easier later on to go ahead and import the Ninject.Core namespace from the References property page:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/SoneRPtx0GI/AAAAAAAAAPw/ivt7XUeG4Kw/s1600-h/image%5B6%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/SoneRhLpWTI/AAAAAAAAAP0/t-YO-XqvDzU/image_thumb%5B2%5D.png?imgmax=800" width="244" height="231" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;The Old Way&lt;/h2&gt;  &lt;p&gt;First, let’s look at how we might previously have implemented the Customer class without DI.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SoneR79_X_I/AAAAAAAAAP4/ULLnqHmhZ7k/s1600-h/image%5B11%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/SoneScM5yjI/AAAAAAAAAP8/HrEde69MgWQ/image_thumb%5B5%5D.png?imgmax=800" width="511" height="370" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here you can see that we’ve got a private SqlConnection object that is created in the constructor. There’s no way here to swap that out for a mock connection for testing, and we’ve got a coupling between Customer and SqlConnection.&lt;/p&gt;  &lt;p&gt;Now let’s look at how SqlConnection does its logging (note that this class is obviously just for demo purposes):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/SoneSpJZdQI/AAAAAAAAAQA/AsIK0qcJ8wA/s1600-h/image%5B15%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/SoneT0jGyHI/AAAAAAAAAQE/6cBsJiJlGcE/image_thumb%5B7%5D.png?imgmax=800" width="517" height="285" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As you might have guessed, our SqlConnection object now has a tight coupling with ConsoleLogger. What if we wanted to log to a database or email instead? With this method, we can’t.&lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;Dependency Injection By Hand&lt;/h2&gt;  &lt;p&gt;As I mentioned earlier, I started out by doing DI by hand, so let’s see what that might look like.&lt;/p&gt;  &lt;p&gt;First let’s make an interface for our database connection:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/SoneUC_zchI/AAAAAAAAAQI/jtEajJARcYE/s1600-h/image%5B20%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/SoneUu4F6DI/AAAAAAAAAQM/-49rVwOM2LA/image_thumb%5B10%5D.png?imgmax=800" width="500" height="165" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now, here’s what our SqlConnection class looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/SoneU7lwaFI/AAAAAAAAAQQ/KdkmYB2Wn0Q/s1600-h/image%5B24%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SoneVRRvs8I/AAAAAAAAAQU/JIomqAQ9cDc/image_thumb%5B12%5D.png?imgmax=800" width="491" height="399" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We still have a dependency on ConsoleLogger, but we’ll remove that later.&lt;/p&gt;  &lt;p&gt;Now our customer class no longer has a dependency on SqlConnection:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/SoneVsbFYWI/AAAAAAAAAQY/rbI7dx_l2Ko/s1600-h/image%5B28%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/SoneWLdydtI/AAAAAAAAAQc/-9Iiyz-HT_8/image_thumb%5B14%5D.png?imgmax=800" width="503" height="352" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And here’s how we use it in our console app:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/SoneWav6R4I/AAAAAAAAAQg/jhHP5dWtYPg/s1600-h/image%5B32%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/SoneWvD39nI/AAAAAAAAAQk/IGJnd90wnGM/image_thumb%5B16%5D.png?imgmax=800" width="455" height="308" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And the result is (with our ConsoleLogger output):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SoneWxFHhVI/AAAAAAAAAQo/daliders30Q/s1600-h/image%5B36%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SoneXblPOjI/AAAAAAAAAQs/qfJCNaJruwM/image_thumb%5B18%5D.png?imgmax=800" width="462" height="248" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We still have to inject the SqlConnection by hand, which would work in front end code, but is not ideal. &lt;/p&gt;  &lt;p&gt;So, proceeding onto removing the dependency on ConsoleLogger, how would we accomplish this with the method above? Well, we’d probably have to pass in a new ConsoleLogger (implementing ILogger) into our customer object as well. However, then we’d have to pass that logger from the customer into the SqlConnection by a constructor or property. And what if we have to create multiple objects that use the same connection or logger? It becomes a mess very quickly. This is where Ninject steps in to help.&lt;/p&gt;  &lt;p&gt;Let’s wrap up a few more things, such as creating an interface for our loggers:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_xvXvGivrKF0/SoneXmYrMYI/AAAAAAAAAQw/TQmReOIvkOI/s1600-h/image%5B41%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_xvXvGivrKF0/SoneX1z0LqI/AAAAAAAAAQ0/hE0zfHmaqg4/image_thumb%5B21%5D.png?imgmax=800" width="280" height="132" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Then we’d make ConsoleLogger implement ILogger.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/SoneYShDZuI/AAAAAAAAAQ4/m5VkllXQXy0/s1600-h/image%5B45%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SoneYq12LoI/AAAAAAAAAQ8/ZLTNxm9X358/image_thumb%5B23%5D.png?imgmax=800" width="512" height="266" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h2&gt;Setting Up our Injections&lt;/h2&gt;  &lt;p&gt;How Ninject and other DI solutions work is by automatically wiring up our dependencies for us. For example, we need to wire up 2 dependencies: anytime we want an IDataConnection in this app we want to use a SqlConnection, and anytime we want an ILogger we want a ConsoleLogger. This makes the code testable because you can wire up in your tests to use a mock IDataConnection instead, with another type of logger if you wish.&lt;/p&gt;  &lt;p&gt;We’re going to use those two methods of injection as mentioned here before. First, &lt;strong&gt;constructor injection&lt;/strong&gt;. We want to use the constructor of the Customer object to inject a dependency on SqlConnection. Here’s how we mark that constructor for injection:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SoneYzlD2pI/AAAAAAAAARA/YQHo6QnItBg/s1600-h/image%5B49%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_xvXvGivrKF0/SoneZdxAE4I/AAAAAAAAARE/FinCFZr71ew/image_thumb%5B25%5D.png?imgmax=800" width="419" height="255" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Next, we want to use &lt;strong&gt;property injection&lt;/strong&gt; to inject a dependency of ConsoleLogger into the SqlConnection. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SoneZlMEhDI/AAAAAAAAARI/qlVvKZyaes4/s1600-h/image%5B53%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SoneZy6VHkI/AAAAAAAAARM/U_xGSHSUSfQ/image_thumb%5B27%5D.png?imgmax=800" width="361" height="312" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Note that we’re accomplishing the same goal, but using a Property instead of a Constructor.&lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;p&gt;&lt;/p&gt;  &lt;h2&gt;Tying It All Together&lt;/h2&gt;  &lt;p&gt;Ninject uses the idea of a “kernel” to generate all the dependencies. And the kernel can have passed into it multiple “modules” to wire them all together. First we’ll create the DataModule, although you can call it whatever you like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SoneaLWiVNI/AAAAAAAAARQ/_FjyA6SycAQ/s1600-h/image%5B57%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/SoneamgcYaI/AAAAAAAAARU/B8bn9QV307E/image_thumb%5B29%5D.png?imgmax=800" width="495" height="228" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Note that it must inherit from StandardModule for this to work. &lt;/p&gt;  &lt;p&gt;Those 2 lines of code inside the Load method are pretty powerful. They’re saying, using generics, that anytime a class requests an IDataConnection, we want a SqlConnection, and anytime a class requests an ILogger, we get a ConsoleLogger. This is dependency injection at work!&lt;/p&gt;  &lt;p&gt;So now that we have our loosely coupled classes (because no class depends on another) and our module for wiring up our connection and logger, we need to actually use the kernel.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_xvXvGivrKF0/Sonea70EqeI/AAAAAAAAARY/iszSW3qPI-E/s1600-h/image%5B61%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SonebQmLkRI/AAAAAAAAARc/3PTBc20V5DQ/image_thumb%5B31%5D.png?imgmax=800" width="492" height="488" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_xvXvGivrKF0/Sonebmx2UCI/AAAAAAAAARg/H0wUugWYdaI/s1600-h/image%5B65%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_xvXvGivrKF0/Soneb4mjaBI/AAAAAAAAARk/eAabVMQl4fk/image_thumb%5B33%5D.png?imgmax=800" width="501" height="279" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Hooray for DI! Now, our front end code doesn’t need to know that it’s using a SqlConnection or ConsoleLogger, it’s happening for us behind the scenes. We could even make the SqlConnection or ConsoleLogger a singleton so that there’s only one instance of it in our application, just by adding the &amp;lt;Singleton()&amp;gt; attribute to the class.&lt;/p&gt;  &lt;p&gt;I hope this shows off the basics of using Ninject in an application. Beyond this, I’m not an expert. I only recently got involved in DI, and only today with Ninject. But I’m really impressed so far, and I think once you play around with it you will be too.&lt;/p&gt;  &lt;p&gt;Please do not hesitate to (kindly) let me know your questions or concerns in the comments.&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/934836491316143880/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=934836491316143880" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/934836491316143880?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/934836491316143880?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2009/08/first-steps-into-dependency-injection.html" title="First Steps into Dependency Injection with Ninject" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/_xvXvGivrKF0/SoneQ2t67wI/AAAAAAAAAPs/ahj2IDrzvT0/s72-c/image_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;CUIDSXg-cSp7ImA9WxNTEUQ.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-3075813112078770742</id><published>2009-08-13T15:59:00.001-04:00</published><updated>2009-08-13T15:59:38.659-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-13T15:59:38.659-04:00</app:edited><title>Jacksonville, here I come!</title><content type="html">&lt;p&gt;I am happy to say that I have taken a position as a .NET developer at &lt;a href="http://www.footballfanatics.com/" target="_blank"&gt;Football Fanatics&lt;/a&gt; in Jacksonville! It’s a booming business with a lot of smart developers and they’re really doing some cool things. I’m excited to take on the challenge!&lt;/p&gt;  &lt;p&gt;And, to show that it will be a learning experience, they pointed out in my interview that my last post, &lt;a href="http://adventuresdotnet.blogspot.com/2009/08/net-conditionally-removing-items-from.html" target="_blank"&gt;Conditionally Removing Items from a Generic List&lt;/a&gt;, is probably not the best way to do things. After the interview, a large forehead-slap was in order. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_xvXvGivrKF0/SoRwqG8HqyI/AAAAAAAAAPg/Om5okWsQcHQ/s1600-h/image%5B5%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_xvXvGivrKF0/SoRwqi4LRDI/AAAAAAAAAPk/w0dfs-pZ1PQ/image_thumb%5B3%5D.png?imgmax=800" width="303" height="129" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So yes, you can remove items conditionally from the list by just using the RemoveAll function with a lambda passed in.&lt;/p&gt;  &lt;p&gt;D’oh. &lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/3075813112078770742/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=3075813112078770742" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3075813112078770742?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/3075813112078770742?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2009/08/jacksonville-here-i-come.html" title="Jacksonville, here I come!" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_xvXvGivrKF0/SoRwqi4LRDI/AAAAAAAAAPk/w0dfs-pZ1PQ/s72-c/image_thumb%5B3%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C0cMSXcyfSp7ImA9WxJaGU8.&quot;"><id>tag:blogger.com,1999:blog-7087580115748722533.post-6032161086245144660</id><published>2009-08-10T12:18:00.001-04:00</published><updated>2009-08-10T12:18:08.995-04:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-10T12:18:08.995-04:00</app:edited><title>A New Adventure</title><content type="html">&lt;p&gt;After 2 years of living in Lakeland, my wife and I decided that now was a good point in our lives to make a very risky jump – to move to Jacksonville without any jobs lined up to speak of. With a bad job market, this may end up being a bad decision, but we’re confident that things will work out in one way or another. Jacksonville is more our speed, and we just aren’t really happy here. I let my work know today, and it’s going to be a nice, clean, professional break. The job opportunities I’ve been connected with seem like great ways to advance my career and work with some smart people, so I’m excited!&lt;/p&gt;  &lt;p&gt;In a month or so we’ll be relocating to Jax and starting the next chapter of our lives. In the mean time, I’ll make my last visit to the Lakeland .NET User Group as a resident of Lakeland, try to write at least another blog post (I still need to finish up one draft), and pack everything up. &lt;/p&gt;  &lt;p&gt;I look forward to joining the Jacksonville user groups and I’ll be attending the Jacksonville Code Camp (and helping Russ Fustino with his “It’s All About The Tools” channel9 episode filming) on August 29th. Hope to see you there!&lt;/p&gt;  </content><link rel="replies" type="application/atom+xml" href="http://adventuresdotnet.blogspot.com/feeds/6032161086245144660/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=7087580115748722533&amp;postID=6032161086245144660" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/6032161086245144660?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7087580115748722533/posts/default/6032161086245144660?v=2" /><link rel="alternate" type="text/html" href="http://adventuresdotnet.blogspot.com/2009/08/new-adventure.html" title="A New Adventure" /><author><name>Paul Irwin</name><uri>https://plus.google.com/117077874659650582255</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-Qz7Zx5JHKXA/AAAAAAAAAAI/AAAAAAAAAAA/SziRDe6TD-E/s512-c/photo.jpg" /></author><thr:total>0</thr:total></entry></feed>
