<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Curried Functions</title>
	
	<link>http://blog.efvincent.com</link>
	<description>Development, mostly .NET... but anything cool</description>
	<lastBuildDate>Thu, 08 Sep 2011 02:12:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CurriedFunctions" /><feedburner:info uri="curriedfunctions" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>DI – Constructor Injection, Bootstrapping</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/6qX5NuXyHbU/</link>
		<comments>http://blog.efvincent.com/di-bootstrap/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 06:07:31 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[AutoFac]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[bootstrap]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/?p=249</guid>
		<description><![CDATA[Once you've grokked the basic concept of dependency injection, the next thing to focus on is how and where to configure your DI container. To DI veterans bootstrapping is a no brainer, but you've got to learn it somewhere!]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fdi-bootstrap%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fdi-bootstrap%2F" height="61" width="51" /></a></div><p>I recently blogged about the <a title="A Taste of Dependency Injection," href="http://blog.efvincent.com/practical-di-101">Practical Dependency Injection 101</a>, then <a title="A Taste of Dependency Injection, Testing, and Mocking" href="http://blog.efvincent.com/di-mock/">DI, testing and mocking</a>. In this post I’ll get into the concept of bootstrapping as it relates to dependency injection.</p>
<h2>Constructor Injection</h2>
<p>The idea of dependency injection is that classes are defined such that any dependencies on other classes or services, are <em>injected</em> into the class by some external mechanism, as opposed to being “newed up” directly. The most common form of DI is constructor injection, where a class defines a constructor that has as its parameters the external dependencies required by the class.</p>
<p>There are several benefits to this particular method of injection; the most obvious is that in a well designed system the dependencies of a class are clearly visible in the constructor. In the <a href="http://blog.efvincent.com/practical-di-101">DI 101</a> post a data provider was defined like this:</p>
<pre class="brush: csharp;">

public class DevDataProvider : IDataProvider {
    private readonly IIdentService _identService;
    private readonly ILogService _logSvc;
    private static readonly List&lt;Employee&gt; EmployeeStore = new List&lt;Employee&gt;();

    public DevDataProvider(IIdentService identService, ILogService logSvc) {
        if (identService == null) throw new ArgumentNullException(&quot;identService&quot;);
        if (logSvc == null) throw new ArgumentNullException(&quot;logSvc&quot;);
        _identService = identService;
        _logSvc = logSvc;
    }

    // Remaining implementation omitted for brevity
}

</pre>
<p>The constructor is on line 6. From this constructor we can see that the DevDataProvider has dependencies on an IIdentityService and an ILogService. There should be no other dependencies in the class other than to well known, stable libraries like the <a href="http://msdn.microsoft.com/en-us/library/hfa3fa08.aspx">BCL</a>.</p>
<p>There are other advantages to using constructor injection. Should the list of dependencies get too long, say longer than four parameters, you’ve got a code smell that perhaps the class is doing too much, violating the single responsibility principal.</p>
<h2>Bootstrapping</h2>
<p>In order to be able to resolve dependencies, the DI container must be configured. This set up is done during the <strong>bootstrapping</strong> phase. Typically this only needs to be done once, but changes to the container make sense in some scenarios like when a DI container is being used to support extensions or plug-ins. In that case components might be added or removed from the DI container while the app is running. These scenarios are out of scope for this post.</p>
<p>The container may be configured in several ways – Auto configuring, configuration in code, and configuration files (typically XML / app or web.config files). My current favorite DI framework is AutoFac, and I typically configure in code, but different projects will have different demands, so familiarize yourself with the specifics of your selected framework and understand the tradeoffs involved in the different types of configuration. You can even configure the DI container using more than one method – perhaps Auto configuring for the bulk of the registrations, then code or XML for more specific configuration needs.</p>
<h2>Bootstrapping a Console Application</h2>
<p>Depending on the type of application you’re working on, there are specific places for bootstrapping to take place. The <em>place</em> to do configuration and bootstrapping is sometimes referred to as the <strong>composition root </strong>(you can read about these concepts in more detail in <a title="Dependency Injection in .NET" href="http://www.manning.com/seemann/">Mark Seeman’s Dependency Injection</a> book, published by Manning).</p>
<p>In a console application, the static Main() method is a typical place to configure the container. While we rarely write console apps in production (at least I rarely do), the simplicity makes it easy to see the implications of the bootstrapping procedure.</p>
<p>In the following sequence diagram, in step one [1] the Main() entry point is called on the console application. Main() is serving as the composition root. From there a private Bootstrap() methods is called [2] and the DI container is configured. The exact mechanism varies by framework.</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/06/Capture.png"><img style="display: inline" title="Capture" alt="Capture" src="http://blog.efvincent.com/wp-content/uploads/2011/06/Capture_thumb.png" width="602" height="404" /></a></p>
<p>Once the container is configured, the main entry point requests that the DI container resolve the App type [3]. The DI container creates whatever dependencies are required by the App [4]. This happens hierarchically; dependencies may themselves have dependencies and so on. The DI container sorts all this out and is also responsible for lifetimes of create objects etc. The DI container can create and return the instance off the App [5]. The Main() function can then pass control to the app [6] which will leverage the injected dependencies [7] to do the real work.</p>
<h2>Only Directly Reference the DI Container in the Bootstrapper</h2>
<p>This is an important point, and if you get nothing else from this post, understand this.</p>
<ul>
<li>The DI container is configured in the composition root (Main() in this case) </li>
<li>The DI container is used to resolve or build the App </li>
<li>The app is then run to do the work </li>
</ul>
<p>Once the app is instantiated, it should have all of its dependencies injected. <strong>The app should not have a reference to the DI container!</strong> If we allow the app or any of its dependencies to have access to the container, then several bad things happen:</p>
<h4>We’ve taken on a dependency to the DI Container itself</h4>
<p>Yes its true that the assembly has a dependency on the DI container. But for purposes of this discussion the assembly is not the application. The App class and the services (other classes) it depends on is the application. We don’t want to take a dependency on the DI container in those classes; rather, we should be able to switch to a different DI container if needed and not effect the App and the dependent services.</p>
<p>In any kind of a significant application the app’s classes would be in a different assembly, and services might be scattered across even more assemblies, and those should not have&#160; a dependency on a DI container. They should however be designed and built with the DI pattern in mind – with the dependencies specified in the constructor, with references to abstract types or interfaces, rather than to concrete implementations.</p>
<h4>We’re hiding a dependency inside the App</h4>
<p>Earlier I mentioned that a benefit of constructor injection is that the dependencies are clearly visible (even <em>documented</em> if you will) in the signature of the constructor. We really don’t want to see lines like this buried in the methods of the classes:</p>
<pre class="brush: csharp; gutter: false; toolbar: false;">// Anti-pattern - don't use DI container except
// in composition root

var dal = Container.Resolve&lt;IDataAccessService&gt;();

// And defintely don't do this

var dal = new SqlDataAccessService(connectString);</pre>
<p>A class that that has these lines buried inside somewhere has hidden dependencies on both the DI container and IDataAccessService (or worse, by using the new keyword directly, on the SqlDataAccessService). These hidden dependencies undermine the benefits of using DI containers at all.</p>
<h3>Bootstrapping in other Application Types</h3>
<p>Other types of apps have different places for bootstrapping and application roots. Unlike a console app, an ASP.NET MVC 3 application isn’t top-down linear, the application must respond to web requests. It does so by creating instances of controllers, and calling methods on those controllers to respond to web requests.</p>
<p>A controller in MVC3 is like the app was in our console example above. It will be resolved, or created, by the DI container. Controllers are different in that there will likely be several different controllers in an MVC application. Also, we don’t get to resolve a controller and tell it to run right from the composition root, the ASP.NET MVC framework will be receiving web requests and will need to resolve controllers later, after bootstrapping.</p>
<p>In ASP.NET MVC 3 this is accomplished by providing a <em>hook</em>, or a place where we can supply a DI container for ASP.NET MVC 3 to use when creating controllers. The developer configures the DI container, and then wires that container into the MVC framework via an instance of IControllerActivator. In the case of AutoFac, there’s a <a href="http://nuget.org/List/Packages/Autofac.Mvc3">NuGet package called AutoFac.Mvc3</a> that includes classes to integrate with MVC3. The implementation details are beyond the scope of this post – just <a href="http://duckduckgo.com/">DuckDuckGo</a> AutoFac.Mvc and find a wealth of additional detail. Same goes for WCF, WPF, and Silverlight applications. There are best practices for configuring DI containers for each app type.</p>
<h3>DI Unfriendly Application Types</h3>
<p>Some application types just do not lend themselves very easily to dependency injection patterns. Classic ASP.NET pops into mind immediately. It was written before Microsoft was as willing to accept OSS, community driven concepts such as DI Containers. A big red flag with ASP.NET is that all subclasses to the Page class (which is what all your ASP.NET pages are) must have a parameterless default constructor. Well there goes parameter injection!</p>
<p>There are other mechanisms for implementing DI patterns in this case, but they’re sub-optimal. Again I’d refer you to <a title="Dependency Injection in .NET" href="http://www.manning.com/seemann/">Mark Seeman’s Dependency Injection</a> book, which is far and away the best DI book in the .NET space, for advice and examples in dealing with DI unfriendly application types.</p>
<h3></h3>
<h3>In Summary</h3>
<p>Hopefully this was helpful in your understanding of a couple of key aspects of using DI containers. Practice a few console applications, and write some tests too. Once you get the idea, move on to more interesting application types. Before long you’ll be shocked you ever wrote applications <em>without</em> some degree of dependency injection. Yea – it’s that good for you.</p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/6qX5NuXyHbU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/di-bootstrap/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/di-bootstrap/</feedburner:origLink></item>
		<item>
		<title>A Taste of Dependency Injection, Testing, and Mocking</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/mEO5epVLNqE/</link>
		<comments>http://blog.efvincent.com/di-mock/#comments</comments>
		<pubDate>Sat, 28 May 2011 00:28:53 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[AutoFac]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[FakeItEasy]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Test]]></category>
		<category><![CDATA[basics]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/?p=229</guid>
		<description><![CDATA[My last post provided a brief introduction into dependency injection. To review, the example included a data provider for Employee objects, which included a feature to return the object corresponding to the currently logged in user. In the end the following interfaces were defined:
IDataProvider – the function is obvious from the name. One implementation, the [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fdi-mock%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fdi-mock%2F" height="61" width="51" /></a></div><p><a title="Practical Dependency Injection 101" target="_blank" href="http://blog.efvincent.com/practical-di-101">My last post</a> provided a brief introduction into dependency injection. To review, the example included a data provider for Employee objects, which included a feature to return the object corresponding to the currently logged in user. In the end the following interfaces were defined:</p>
<p><strong>IDataProvider</strong> – the function is obvious from the name. One implementation, the DevDataProvider, uses a static List&lt;Employee&gt; as a data store.</p>
<p><strong>IIdentityService</strong> – describes a service that supplies the <em>current</em> identity. What current is depends on the implementation of course. A concrete WindowsIdentService defines current as the currently logged in Windows user. The TestIdentService implementation always returned the same username, which is useful for testing as we will see.</p>
<p><strong>ILogService</strong> – describes a simple logging service. The ConsoleLogService implementation prints logs to the console.</p>
<h3>Dependency Injection &amp;Testing</h3>
<p>For this post I’ve added a standard MSTest project and a couple of tests for the data provider. The use of dependency injection patterns in the design of this simple example allows us to easily isolate the code under test.</p>
<pre class="brush: csharp;">static IContainer afContainer;

[ClassInitialize]
public static void TestInit(TestContext ctx) {
    var idSvc = A.Fake&lt;IIdentService&gt;();
    A.CallTo(() =&gt; idSvc.GetCurrentUserName())
        .Returns("FAKE-ID");

    var bldr = new ContainerBuilder();
    bldr.RegisterInstance(idSvc);
    bldr.RegisterInstance(A.Fake&lt;ILogService&gt;());
    bldr.RegisterType&lt;DevDataProvider&gt;().As&lt;IDataProvider&gt;();
    afContainer = bldr.Build();
}</pre>
<p>The test class has a static DI container instance, initialized in the class initializer. I’m using <a title="FakeItEasy" target="_blank" href="http://code.google.com/p/fakeiteasy/">FakeItEasy</a> to create a fake IIdentService at line five. Like six tells the FakeItEasy framework what to return when the GetCurrentUserName() method is called on the fake ident service. Having a fixed response makes testing the data provider a piece of cake.</p>
<p>I then register the fake ident service as well as a fake log service. For the log service, we don’t need to specify any behavior for the methods. The FakeItEasy framework will effectively sink any calls to the methods of the fake log service, which is fine for this test.</p>
<p>Lastly the data provider we want to test is registered with the DI container builder, and then container is built. The tests go on to use the DI container to resolve an instance of the data provider. The DI container will configure the data provider’s dependencies for a log service and an identity service with the fakes we built.</p>
<pre  class="brush: csharp;">[TestMethod()]
public void GetCurrentEmployeeTest() {
    var e = new Employee { WindowsUsername = "FAKE-ID" };
    var dal = afContainer.Resolve&lt;IDataProvider&gt;();
    dal.AddEmployee(e);
    var result = dal.GetCurrentEmployee();
    Assert.AreEqual(e.WindowsUsername, result.WindowsUsername);
}</pre>
<p>This is just a small example of using a DI container in combination with a mock / fake framework for testing. The AutoFac DI container can handle much more complex scenarios than what we’ve thrown at it here. The same is true for the FakeItEasy component. Both of these components are well used, well maintained open source projects. You can find lots of documentation and examples for both. Or you can use any number of other DI containers and mocking frameworks to achieve equivalent results.</p>
<p>The source code for the example is available <a target="_blank" href="https://bitbucket.org/efvincent/blog-post-dependency-injection-101">here</a>, and the blog entry the precedes this one is available <a target="_blank" href="http://blog.efvincent.com/practical-di-101/">here</a>.</p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/mEO5epVLNqE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/di-mock/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/di-mock/</feedburner:origLink></item>
		<item>
		<title>Practical Dependency Injection 101</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/KfOhsXosErI/</link>
		<comments>http://blog.efvincent.com/practical-di-101/#comments</comments>
		<pubDate>Fri, 27 May 2011 17:08:27 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[AutoFac]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[CSharp]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/?p=224</guid>
		<description><![CDATA[In this post we take a look at dependency injection (DI). Target audience is competent .NET developers, C# specifically (but VB’ers who read C# can benefit just as much), who’ve heard of DI but haven’t gotten around to figuring out how it fits in their day to day.
What is Dependency Injection
The first question that we [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fpractical-di-101%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fpractical-di-101%2F" height="61" width="51" /></a></div><p>In this post we take a look at dependency injection (DI). Target audience is competent .NET developers, C# specifically (but VB’ers who read C# can benefit just as much), who’ve heard of DI but haven’t gotten around to figuring out how it fits in their day to day.</p>
<h3>What is Dependency Injection</h3>
<p>The first question that we need to address is: What is it that DI does for us? What problem is being solved? DI is about coupling; the degree to which program unit refers to other units. In .NET the units we’re typically worried about are classes, interfaces, components, and assemblies. Dependency injection facilitates reduction these interdependencies. Are DI patterns a silver bullet? Of course not. You can always write bad code regardless of patterns. That being said, if you’re already writing decent code and have good fundamentals, but are not using DI patterns, you’ve got the opportunity to take a leap forward.</p>
<p>How does DI reduce help reduce coupling? The easiest way to describe it is by diving directly into an example.</p>
<h3>Example Scenario</h3>
<p>We’ll work on a hypothetical in-house app where the Windows AD authenticates employees, and their Windows username is used to index a database with Employee information. It’s pretty common to see stuff like this happening in-house with line of business applications.</p>
<p>The example uses a provider pattern – all the data access will go through a data access provider, allowing us to build a simple provider that stores records in memory during this, our prototype phase. Theoretically we’d replace this as development continued with a provider that leverages persistent storage later.</p>
<p>Here’s the base level example program with no consideration for dependency injection:</p>
<pre class="brush: csharp;">class Program {
    static void Main(string[] args) {

        // ** Without using an DI Container approach **

        // Create a new provider aka data access layer
        var dal = new DevDataProvider();

        // New up an employee that's supposed to represent the currently logged in user
        var e = new Employee() {
            WindowsUsername = "thanos\\efvincent",
            EmployeeId = "0001",
            FName = "Eric",
            LName = "Vincent"
        };

        // Add it to the data access layer
        dal.AddEmployee(e);

        // See if the dal can find the current user
        e = dal.GetCurrentEmployee();

        Console.WriteLine(
            "Current logged in person is: {0}", e == null ? "unknown" : e.FName);

        // End
        Console.Write("Press any key...");
        Console.ReadKey(true);

    }
}

public class DevDataProvider {
    private static readonly List&lt;Employee&gt; EmployeeStore = new List&lt;Employee&gt;();

    public Employee GetCurrentEmployee() {
        var emp = EmployeeStore.FirstOrDefault(
            e =&gt; e.WindowsUsername.Equals(GetCurrentUserName(), StringComparison.OrdinalIgnoreCase));
        return emp;
    }

    public void AddEmployee(Employee e) {
        EmployeeStore.Add(e);
    }

    public IQueryable&lt;Employee&gt; Employees() {
        return EmployeeStore.AsQueryable();
    }

    private static string GetCurrentUserName() {
        var wu = WindowsIdentity.GetCurrent();
        return wu == null ? string.Empty : wu.Name;
    }
}

public class Employee {
    public string WindowsUsername { get; set; }
    public string EmployeeId { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }
}</pre>
<p>In Main() we new up the data access layer, create a new employee, and add it to our store using the data access layer. At line 21 we ask the data access layer to retrieve the employee record for the currently logged in user. Looks pretty typical, so how can IoC help? Let’s look at the coupling here – what classes are dependent on what other classes?</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/05/image.png"><img title="image" 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" alt="image" src="http://blog.efvincent.com/wp-content/uploads/2011/05/image_thumb.png" border="0" width="496" height="189" /></a></p>
<p>Our main program depends on the DevDataProvider class, and that depends on System.Security to find the Windows username of the currently logged in user. Asking the data access layer to determine the currently logged in user isn’t the best idea, but this is blog post code created to check out dependency injection, so deal with that for the moment.</p>
<p>Why are these dependencies undesirable? First consider how flexible this software is. Or rather, inflexible. We created a “quick” DevDataProvider that stores stuff in a static list. As we continue to build a system, we’d have to refer to DevDataProvider from more and more classes, creating a brittle, tightly coupled system. Replacing DevDataProvider becomes more of a maintenance problem.</p>
<p>Next think about testability. In real life there are unit tests (there should be anyway). One reason why people find excuses not to unit test is because their code is difficult to test. In this example, if we want to test DevDataProvider.GetCurrentEmployee() we have to consider that under the covers it’s calling the Windows API to get the current username. This makes that method harder to than it needs to be.</p>
<h3>Step One – Leveraging Interfaces</h3>
<p>In this version, we’ve factored out an interface called IDataProvider, and one called IIdentService. The IDataProvider should be pretty obvious – but IIdentService? The idea here is to decouple from the Windows API itself. A developer should understand <em>everywhere </em>that the application makes contact with <em>any</em> external modules, including the operating system, and then consider what the repercussions of that contact are. In this example, coupling to the Windows API to get then logged in username so directly is undesirable. We want to use a <em>service</em> that would supply us with credentials. That way if we’re testing, we can create a fake service that provides a predictable answer, and is therefore easier to test.</p>
<p>Coding to an interface also allows us to radically change the behavior of the service without having to alter its dependencies. If we move to a ASP.NET environment for example, we won’t want to use the current Windows Identity, we may want to use user information from the http context.</p>
<pre class="brush: csharp;">// Interface defining an identity service
public interface IIdentService {
    string GetCurrentUserName();
}

// Implementation of an identity service that returns the current
// logged in windows username
public class WindowsIdentService : IIdentService {
    public string GetCurrentUserName() {
        var wu = WindowsIdentity.GetCurrent();
        return wu == null ? string.Empty : wu.Name;
    }
}

// Interface defining a data provider service
public interface IDataProvider {
    Employee GetCurrentEmployee();
    void AddEmployee(Employee e);
    IQueryable&lt;Employee&gt; Employees();
}

// Our development data provider now implements the interface
public class DevDataProvider : IDataProvider {
    private readonly IIdentService _identService;
    private static readonly List&lt;Employee&gt; EmployeeStore = new List&lt;Employee&gt;();

    public DevDataProvider(IIdentService identService) {
        if (identService == null) throw new ArgumentNullException("identService");
        _identService = identService;
    }

    public Employee GetCurrentEmployee() {
        var emp = EmployeeStore.FirstOrDefault(
                        e =&gt; e.WindowsUsername.Equals(
                            _identService.GetCurrentUserName(),
                            StringComparison.OrdinalIgnoreCase));
        return emp;
    }

    public void AddEmployee(Employee e) {
        EmployeeStore.Add(e);
    }

    public IQueryable&lt;Employee&gt; Employees() {
        return EmployeeStore.AsQueryable();
    }
}</pre>
<p>We’re part of the way to where we need to be. Altering DevDataProvider to depend on the IIdentService interface frees it from a hard dependency on a particular identity implementation. The downside is we’ve made creation of the DevDataProvider a bit more complex, as we need to supply the new instance with an IIdentityService instance.</p>
<pre class="brush: csharp;">// Create a new ident service, required for the DAL
IIdentService identSvc = new WindowsIdentService();

// Create a new DAL
IDataProvider dal = new DevDataProvider(identSvc);</pre>
<p>The DevDataProvider now takes a constructor parameter of type IIdentService. This is where the <em>injection</em> in dependency injection comes from. DevDataProvider has a dependency, but instead of hard coding it into the definition of DevDataProvider, we inject it. There are different ways of injecting dependencies, but constructor injection is very popular and works well in many, or even most cases.</p>
<p>The complexity of constructing instances increases when we add a simple logging service which logs information or errors messages.</p>
<pre class="brush: csharp;">// Interface defining a logging service
public interface ILogService {
    void LogInfo(string msg, params object[] args);
    void LogError(string msg, params object[] args);
}

// Implementation of a console logging service
public class ConsoleLogger : ILogService {
    public void LogInfo(string msg, params object[] args) {
        Console.WriteLine(
            "{0} INFO: {1}", DateTime.Now,
            string.Format(msg, args));
    }

    public void LogError(string msg, params object[] args) {
        Console.WriteLine(
            "{0} ERROR: {1}", DateTime.Now,
            string.Format(msg, args));
    }
}</pre>
<p>The ILogService is implemented by a simple console logger. Now both the WindowsIdentService and the DevDataProvider can leverage the logger. They’re both modified to have ILogService instance injected via their respective constructors.</p>
<pre class="brush: csharp;">// Implementation of an identity service that returns the current
// logged in windows username
public class WindowsIdentService : IIdentService {
    private readonly ILogService _logSvc;

    public WindowsIdentService(ILogService logSvc) {
        if (logSvc == null) throw new ArgumentNullException("logSvc");
        _logSvc = logSvc;
    }

    public string GetCurrentUserName() {
        var wu = WindowsIdentity.GetCurrent();
        var un = wu == null ? string.Empty : wu.Name;
        _logSvc.LogInfo("Identified current user as: {0}", un);
        return un;
    }
}

// Our development data provider now implements the interface
public class DevDataProvider : IDataProvider {
    private readonly IIdentService _identService;
    private readonly ILogService _logSvc;
    private static readonly List&lt;Employee&gt; EmployeeStore = new List&lt;Employee&gt;();

    public DevDataProvider(IIdentService identService, ILogService logSvc) {
        if (identService == null) throw new ArgumentNullException("identService");
        if (logSvc == null) throw new ArgumentNullException("logSvc");
        _identService = identService;
        _logSvc = logSvc;
    }

    public Employee GetCurrentEmployee() {
        var un = _identService.GetCurrentUserName();
        var emp = EmployeeStore.FirstOrDefault(
                        e =&gt; e.WindowsUsername.Equals(un,
                            StringComparison.OrdinalIgnoreCase));
        if (emp == null) _logSvc.LogInfo("Current employee {0} not found", un);
        return emp;
    }

    public void AddEmployee(Employee e) {
        EmployeeStore.Add(e);
        _logSvc.LogInfo("Added employee with id {0}", e.EmployeeId);
    }

    public IQueryable&lt;Employee&gt; Employees() {
        return EmployeeStore.AsQueryable();
    }
}</pre>
<p>Now the services are getting more robust and they&#8217;re not tightly coupled to each other, they refer only to interfaces of the services they depend on. Main() however, where construction is going on, is getting messy.</p>
<pre class="brush: csharp;">static void Main(string[] args) {

    // ** Without using an DI Container approach **

    // Create a new logging service, required for uh.. everything
    ILogService consoleLog = new ConsoleLogger();

    // Create a new ident service, required for the DAL
    IIdentService identSvc = new WindowsIdentService(consoleLog);

    // Create a new DAL
    IDataProvider dal = new DevDataProvider(identSvc, consoleLog);</pre>
<p>Finally we’re at the point where we can see the benefit of an dependency injection container. One thing about DI containers is that they’re already written. Several mature, robust, open-source DI containers exist. We’ll use AutoFac, because that’s what I’ve been using lately. We include <a href="http://www.nuget.org/List/Packages/Autofac">AutoFac</a> using <a href="http://www.nuget.org">NuGet</a>.</p>
<pre class="brush: csharp;">// Create a singleton dependency injection container
private static readonly IContainer Container = ConfigureContainer();

// Configure the container
static IContainer ConfigureContainer() {
    // This is how AutoFac works. Other DI containers have similar
    // mechanisms for configuring the container
    var bld = new ContainerBuilder();

    // Register the types that implement the interfaces that are required
    // for injection. Note that we have robust control over lifetime, in
    // this case ILogService and IIdentService will be singletons, and
    // IDataProvider will provide a new instance each time it's requested
    bld.RegisterType&lt;ConsoleLogger&gt;().As&lt;ILogService&gt;().SingleInstance();
    bld.RegisterType&lt;WindowsIdentService&gt;().As&lt;IIdentService&gt;().SingleInstance();
    bld.RegisterType&lt;DevDataProvider&gt;().As&lt;IDataProvider&gt;();
    return bld.Build();
}

static void Main(string[] args) {

    // ** Using an IoC Container approach **
    var dal = Container.Resolve&lt;IDataProvider&gt;();</pre>
<p>We’ve created a static instance of IContainer (an AutoFac DI container). When we start the app we configure the container, which fundamentally consists of mapping the interfaces to concrete types that will be injected. For example, line 14 specifies that when there’s a need for an instance of ILogService, we will create an instance of ConsoleLogger. It further says that the DI container should use a SingleInstance() of ConsoleLogger. This has the effect of making ConsoleLogger a singleton. In our example, both the WindowsIdentService and the DevDataProvider will be handed the same instance of ConsoleLogger.</p>
<p>The magic happens when we call Container.Resolve&lt;IDataProvider&gt;(). The container determines what concrete class to create, and it looks for any dependencies in the constructor. It will see that to build a DevDataProvider, it needs an ILogService and an IWindowsIdentService, it will recursively resolve those as well.</p>
<p>These are the basics of dependency injection. Even in this simple example, it should be obvious that the components and classes that we can create can be designed with very low coupling and cohesion using this technique. There are anti-patterns for using DI as well. You should endeavor to practice this technique, do research and learn the most effective uses; there’s plenty of material out there.</p>
<p>I hope this example was helpful. Next post I’ll extend this sample by looking at testing, and mock objects, and how DI injection can make testing far easier.</p>
<p>Source code samples can be found at this <a href="https://bitbucket.org/efvincent/blog-post-dependency-injection-101">bitbucket repository</a>.</p>
<p><em>Update:</em> Follow up post &#8211; <a href="http://blog.efvincent.com/di-mock">Dependency Injection, Testing, and Mocking</a></p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/KfOhsXosErI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/practical-di-101/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/practical-di-101/</feedburner:origLink></item>
		<item>
		<title>Xoom</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/w6ZdTKVNxhk/</link>
		<comments>http://blog.efvincent.com/xoom/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 19:20:45 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[xoom]]></category>
		<category><![CDATA[motorola]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/xoom/</guid>
		<description><![CDATA[Two things; unlike many others (geeks and normal humans as well) I’ve been blissfully iPad free. I had an iPhone and still haven’t recovered, so there was no way I was getting a enbiggened* version of Job’s handiwork. So I can’t compare Xoom to the iPad.
I also have no Kindle, Nook, or other dedicated reading [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fxoom%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fxoom%2F" height="61" width="51" /></a></div><p>Two things; unlike many others (geeks and normal humans as well) I’ve been blissfully iPad free. I had an iPhone and still haven’t recovered, so there was no way I was getting a enbiggened* version of Job’s handiwork. So I can’t compare Xoom to the iPad.</p>
<p>I also have no Kindle, Nook, or other dedicated reading device. I <em>had</em> a Nook for 6 days, returned it. Just could not get into the dedicated book reading device. I need a computer that’s a <em>computer</em>, not just a book reader or (in spite of what Apple will tell you) a means to consume media, which is what iPad is.</p>
<p>There are plenty of real reviewers reviewing the Xoom. Go check them out. Mine is the point of view of a non-reviewer and programmer primarily on the Microsoft stack but a fan of other languages and platforms as well (lately Ruby and Python).</p>
<p>Which brings me to the <strong>Xoom</strong>. Yes, it is also an excellent means of consuming media. It’s got a 16:10 screen that’s apparently a bit bigger than the iPad’s, BUT, it is <a href="http://blog.efvincent.com/wp-content/uploads/2011/04/XoomCapture.png"><img title="XoomCapture" alt="XoomCapture" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top: 0px; border-right: 0px; padding-top: 0px" src="http://blog.efvincent.com/wp-content/uploads/2011/04/XoomCapture_thumb.png" border="0" width="219" height="240" align="left" /></a>also a computer. You can program the Xoom without sending Jobs and his crew a licensing fee**. You can give your friends any software you write (yes, software. An “app” is software peeps). The software you write can access the full capabilities of this device. We all know that Android is wide open vs. iOS which is… well which isn’t. And neither is Windows Phone 7 by the way, WP7 is less open than iOS. I still like WP7 <em>better</em> than iOS on the technical front, but lets not get into that.</p>
<h2>Operating System</h2>
<p>Android is a cool OS, no doubt about that. It’s a Linux platform, not too much hiding that fact. The shell (aka UI) is smooth, gets the job done. It’ll allow you to clutter it up and make it look like crap if you see fit. System menus and utilities are easily accessible. Wireless connectivity is cake to set up, Bluetooth too. And you have programmability – readily available SDKs, full access to the system, excellent documentation to be found, and a great OSS community out there. Thumbs up.</p>
<h2>Browser</h2>
<p>It’s got a real browser, with tabbed pages, a normal address bar, and even a plugin model. Not all plugins work that work for Chrome, but there are some and the fact that they can be written at all is a very good thing, more will show up. Another ++point for Android acting like a computer instead of a [whatever they’re calling the iPad experience].</p>
<p>The Xoom’s browser scored 218 points on <a href="http://html5test.com/index.html">HTML5test.com</a>, that’s better than IE9’s 130, but not as good as any of the other major browsers. Chrome scores 288 for example. Didn’t see an automated way of running the W3C’s tests, but suffice it to say that the Xoom’s browser has pretty decent compliance at this point.</p>
<p>All the web sites I visited looked good. Some sites can be confused by the user-agent and send the mobile version of the site. To fix this, you can nav to about:debug, then under the menu go to Settings… Debug… and set UAString to desktop. Now the user-agent will indicate desktop and you’ll get the full web pages. Oh and ++point for being able to put the browser in debug mode.</p>
<p>Loading pages (per CNet test) is faster on the Xoom than on the iPad2, so that’s good. Everything in my opinion was smooth and fast. Zooming, scrolling, etc. Thumbs up.</p>
<h2>Flash</h2>
<p>Go to the marketplace, find Flash, install. Done. Now you can use Flash if you really want to. Life is better without it though, Flash can dogeven on a dual core Atom, so there you go*** . Thumbs meh.</p>
<h2>Other Stuff</h2>
<p>The built in PDF reader is good enough for me. It remembers where you are in each file, which is the killer feature for me in a PDF reader. It’s smooth and fast, snap-zooms appropriately in either portrait or landscape to the correct page width, and has a clear, easy to use index. Thumbs up.</p>
<p>It’s got a book reader too, if you want to plunk money down on an e-book that traps you into a platform. This is Kindle territory, so I’m not buying books like that. I’ll by books from the publisher in PDF format, thanks. So no word on how the books thing is. Thumbs meh.</p>
<p>Rockplayer Light. This is a downloadable video player. Plays all kinds of formats, including MKV. If you know what I’m talking about then this is a ++point for Xoom. If you don’t then never mind. Nothing to see here. Move along.</p>
<p>PlayNow. Stream all kinds of internet vid to your device. Run a proggy on your PC, give it your netflix and hulu accounts. Then stream almost any internet vid to your device. This is a killer app, I use it all the time. I’m on a trial, but I’ll have to pay for this when it expires. Thumbs way up.</p>
<h2>Conclusion</h2>
<p>This was a crappy review. That’s why I don’t get free stuff. Xoom is cool enough for me to take 15 minutes and jot down some thoughts. Real person, real experiences, real thoughts. The Xoom is a real computer with no keyboard (you can fix that). It’s hackable, quick, and useful right out of the box. It’ll get better with more apps. Now I’ve got billable work to do. Later –e.</p>
<p>______________________</p>
<p>* <em>one of the characters in <a href="http://www.rottentomatoes.com/m/four_lions/">4 Lions</a> said this when told the toy AK-47 he was holding while shooting his jihadist video was too small – “lemme move closer to the camera, that’ll embiggen it”. Friggin hilarious. Recommend <a href="http://www.rottentomatoes.com/m/four_lions/">4 Lions</a>.</em></p>
<p>** <em>or Balmer and his crew. See? Fair <img src='http://blog.efvincent.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </em></p>
<p><em>*** used dog as a verb. No rules on these interwebs baby!</em></p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/w6ZdTKVNxhk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/xoom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/xoom/</feedburner:origLink></item>
		<item>
		<title>NuGet – Galleries of Geeky Goodness.</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/CIyTSMkyaEM/</link>
		<comments>http://blog.efvincent.com/nuget-geeky-goodness/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 23:27:26 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[EntityFramework]]></category>
		<category><![CDATA[NuGet]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[ef]]></category>
		<category><![CDATA[entity framework]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/?p=198</guid>
		<description><![CDATA[In a previous post I described how Scott Hanselman’s presentation during PDC 2010 introduced me to several wonderful and interesting technologies that are either just now out or about to be. The first one I’ll discuss is NuGet. There are many excellent introductions to NuGet by Hanselman and others. I’ll see if I can make [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fnuget-geeky-goodness%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fnuget-geeky-goodness%2F" height="61" width="51" /></a></div><p>In a <a href="http://blog.efvincent.com/open-source-nuget-and-more/" target="_blank">previous post</a> I described how Scott Hanselman’s <a href="http://www.hanselman.com/blog/PDC10BuildingABlogWithMicrosoftUnnamedPackageOfWebLove.aspx" target="_blank">presentation</a> during PDC 2010 introduced me to several wonderful and interesting technologies that are either just now out or about to be. The first one I’ll discuss is <a href="http://nuget.codeplex.com" target="_blank">NuGet</a>. There are many excellent introductions to NuGet by <a href="http://www.hanselman.com/blog/IntroducingNuPackPackageManagementForNETAnotherPieceOfTheWebStack.aspx" target="_blank">Hanselman</a> and others. I’ll see if I can make a meaningful contribution.</p>
<p>NuGet is an add-on to VS2010. It allows us to access <em>packages</em> that have been published either locally or publicly on the web. The main NuGet gallery just went live today at <a href="http://nuget.org">http://nuget.org</a>. These packages make it easy to install, update, and remove libraries and tools in Visual Studio 2010.</p>
<h2>Installation</h2>
<p>NuGet, like dozens of other useful add-ins, is available through the Visual Studio Extension Manager, reached under <em>Tools… Extension Manager…. </em></p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/01/image.png"><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" src="http://blog.efvincent.com/wp-content/uploads/2011/01/image_thumb.png" border="0" alt="image" width="554" height="328" /></a></p>
<p>From the Extension Manager choose (1) the online gallery, and then (2) search for NuGet. A restart of Visual Studio is required.</p>
<h2>The NuGet Package Manager Console</h2>
<p>NuGet has two user interfaces. There’s a PowerShell powered console window. This is the UI I was first introduced to and have gravitated towards. The console gives you a more complete sense of control over the process. Open the console by selecting <em>View… Other Windows… Package Manager Console</em>.</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/01/image1.png"><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" src="http://blog.efvincent.com/wp-content/uploads/2011/01/image_thumb1.png" border="0" alt="image" width="554" height="235" /></a></p>
<p>The <em>Package Source</em> drop down defaults to the official NuGet package gallery. There are already hundreds of packages out in the gallery, and you can expect it to grow rapidly. You can list the packages in the official library with:</p>
<p><span style="font-family: 'Courier New';">List-Packages –remote </span></p>
<p>There are a lot of them, and since the console is PowerShell we can take advantage of a PowerShell grid display:</p>
<p><span style="font-family: 'Courier New';">List-Packages –remote | out-GridView</span></p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/01/image2.png"><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" src="http://blog.efvincent.com/wp-content/uploads/2011/01/image_thumb2.png" border="0" alt="image" width="554" height="277" /></a></p>
<h2>Installing and Removing Packages</h2>
<p>Now comes the really cool part. The whole idea of NuGet is to be able to install / add external libraries to your project without having to find them on the web, download a zip or msi file, install it somewhere on your local machine, figure out what the instructions are for incorporating them into your project, and patching it in manually.</p>
<p>Let’s take for example, the latest CTP5 version of Entity Framework, that allows for <em>Code First</em> use of the Entity Framework. EF4 will justify at least one future dedicated post, but we’ll use it here for a quick example. Normally, if you want to try out something like a new version of Entity Framework, there’s downloading, installing, shaving the chicken, bla bla bla that you’ve got to do. Not with NuGet. NuGet can effectively <em>“</em>xcopy deploy” it into our project.</p>
<p>Create a new console application in Visual Studio. From the Package Manager Console, type <em>Install-Package EF</em> and press the tab key. An intellisense window will pop up showing packages that begin with EF.</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/01/image3.png"><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" src="http://blog.efvincent.com/wp-content/uploads/2011/01/image_thumb3.png" border="0" alt="image" width="398" height="178" /></a></p>
<p>At the time of this writing, there are five packages. The EFCTP4 ones are obsolete, from the last CTP. For this example, choose <em>EFCodeFirst</em> and press enter. The console displays license info, downloads the EFCodeFirst package, and incorporates it into your project. In the solution explorer, we can see a reference  (1) and a file (2) added to the project.</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/01/image4.png"><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" src="http://blog.efvincent.com/wp-content/uploads/2011/01/image_thumb4.png" border="0" alt="image" width="262" height="289" /></a></p>
<p>Checking the properties of the reference to EntityFramework, you can see that it’s not installed to the GAC, it’s local to the solution. NuGet creates a solution level folder called <em>packages</em>. In that folder, in addition to components used as references, is all the data that NuGet needs to install and uninstall packages. All local, without changing any configuration on the machine. More details of the inner workings of NuGet and its packages will come at a later date, plus there’s information out there you can dig up if you’re so inclined.</p>
<h2>Using Entity Framework</h2>
<p>Now that we’ve got it installed, let’s quickly use EFCodeFirst just to prove it’s working. For this example, you’ll need SQL Server installed (there are other options, but one thing at a time). I’ve got Express which works fine. In another post I’ll show the new SQL Server Compact Edition 4, which coincidentally you can add to a project using NuGet <img class="wlEmoticon wlEmoticon-smile" style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" src="http://blog.efvincent.com/wp-content/uploads/2011/01/wlEmoticon-smile.png" alt="Smile" />.</p>
<p>EFCodeFirst allows you to build POCO objects and use them to build a DB. Let’s build a quick model. There are a few examples floating around modeling blog posts and comments, so lets <em>not</em> do that. Let’s do (super simple) prescriptions and medications. It’s on my mind because I’m recovering from knee surgery. This code is entered directly into the Program.cs source file for this example.</p>
<pre class="brush: csharp;">public class Prescription {
    public int Id { get; set; }
    public string MedName { get; set; }
    public string Directions { get; set; }
    public int Quantity { get; set; }
    public int Refills { get; set; }
    public int RefillsRemaining { get; set; }
    public ICollection&lt;FilledScript&gt; FilledScripts { get; set; }
}

public class FilledScript {
    public int Id { get; set; }
    public Prescription Script { get; set; }
    public DateTime Filled { get; set; }
    public int Doses { get; set; }
}

public class MedContext : DbContext {
    public DbSet&lt;Prescription&gt; Prescriptions { get; set; }
    public DbSet&lt;FilledScript&gt; FilledScripts { get; set; }
}</pre>
<p>We have a class for a Prescription, and one for a FilledScript (filled prescription). We give each one an integer Id, which EF will interpret (by convention) as the primary key and identity for each entity. A relation is created between them, again by convention. On the “one” side,  the Prescription has a collection of filled scripts, and on the “many” side, the filled script has a reference to its prescription.</p>
<p>The <em>MedContext</em> class inherits from EF’s DbContext, and pulls the model together. Each DbSet&lt;…&gt; identifies an entity to EF. In this minimal case, that’s all that’s required to define a model using EFCodeFirst. Lets take a look at using it:</p>
<pre class="brush: csharp;">static void Main(string[] args) {

    DbDatabase.SetInitializer(new DropCreateDatabaseAlways&lt;MedContext&gt;());

    var ctx = new MedContext();
    var p = new Prescription() {
        MedName = "Vicoden",
        Directions = "One every 4 hours as needed for pain",
        Quantity = 60,
        Refills = 2,
        RefillsRemaining = 1,
        FilledScripts = new[] {
            new FilledScript() {
                Filled = DateTime.Parse("12/28/2010"),
                Doses = 0
            },
            new FilledScript() {
                Filled = DateTime.Parse("1/12/2011"),
                Doses = 48
            }
        }
    };

    ctx.Prescriptions.Add(p);
    ctx.SaveChanges();

    foreach (var script in ctx.Prescriptions) {
        Console.WriteLine("Script for {0}, filled {1} time(s), we have {2} doses on hand",
            script.MedName,
            script.FilledScripts.Count(),
            script.FilledScripts.Sum(fs =&gt; fs.Doses));
    }

    Console.Write("\nPress any key...");
    Console.ReadKey(true);
}</pre>
<p>For this simple example I’m just adding some data and retrieving it right from the main() function of the console app. Line 3 probably the only line that needs explanation. It’s a call to the static method <span style="font-family: 'Courier New';">SetInitializer()</span> on the static <span style="font-family: 'Courier New';">DbDatabase</span> class of EF. The parameter is a new instance of <span style="font-family: 'Courier New';">DropCreateDatabaseAlways&lt;MedContext&gt;</span>. Creating this initializer with it’s type parameter set to our <span style="font-family: 'Courier New';">MedContext</span> tells the EF engine that it should always drop the database and recreate it, every time the app is run. This is part of EF’s new fluent interface. Under almost no circumstance would you actually use this initializer, but it’s handy for testing and playing around. This way you can iterate over your model getting a new database for each run.</p>
<p>The rest of the function adds a Prescription object with two FilledScript objects, and saves it in the context. It then loops through the prescriptions and writes some info to the console about each. No rocket science there.</p>
<h2>Where did EF Create the Database?</h2>
<p>One cool thing about EFCodeFirst is that you can leave off pretty much all the configuration information. We didn’t tell EF anything about how or where to create the database. It came up with some reasonable defaults. If you open SSMS and connect to your instance of SQL Express, you can see the database it created. Here’s a DB diagram in SSMS from what EF created:</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/01/image5.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" src="http://blog.efvincent.com/wp-content/uploads/2011/01/image_thumb5.png" border="0" alt="image" width="554" height="423" /></a></p>
<p>The database it created (1) is named according to the context that it stores, MedContext for us. It created tables (2 &amp; 3) for the entities we created, and to implement the one to many relationship, it added a foreign key to the FilledScripts table linking it to the Prescriptions table. There’s always a DBA to disagree with an auto-generated schema, but in this simple case, you must admit there’s not a whole lot you could do differently. If we select the records out of the database after our run you get the expected results:</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2011/01/image6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" src="http://blog.efvincent.com/wp-content/uploads/2011/01/image_thumb6.png" border="0" alt="image" width="554" height="405" /></a></p>
<p>All of this was created by convention, and can also be overridden and specified explicitly using either attributes on the classes that make up the model, or by using EF’s fluent API. This barely scratches the surface, but hopefully gets you interested. I’ll cover EF in more depth in a future post, plus there’s plenty of info out there already.</p>
<h2>Back to NuGet – Removing a Package</h2>
<p>Ok, we’ve seen how easy it is to add a package like EFCodeFirst (and how cool EF itself is). NuGet also allows you to remove a package easily. In the Package Manager Console window, issue the command:</p>
<pre>PM&gt; Uninstall-Package EFCodeFirst
Successfully removed 'EFCodeFirst 0.8' from ConsoleApplication2
Successfully uninstalled 'EFCodeFirst 0.8'</pre>
<p>NuGet will remove all traces of the package from your project. For EFCodeFirst there’s not much to remove. But other packages are far more complicated, even pulling in prerequisite packages, and modifying the web or app.config. NuGet undoes all these changes when a package is removed.</p>
<h2>Try it yourself!</h2>
<p>One great thing about NuGet is how painless it makes trying out new things. You’re not fouling up your precious work machine with a bunch of installations. You can create a quick test project, add a bunch of things you’ve always wanted to try, and just blow it all away when you’re done. No evidence you’ve been <em>learning</em>.</p>
<p>Ever want to try Fluent NHibernate but didn’t feel like dealing with all the crap that goes with tracking it down and installing it? How about Ninject, Castle, ELMAH, iTextSharp, Moq, or Prism? They’re all out there. Just NuGet them and start playing / learning.</p>
<h2>What else?</h2>
<p>I didn’t even mention the wizardy, non-console approach. Right click on your project, and select <em>Add Library Package Reference… </em>and you get a pretty UI that lets you click instead of type. But you’re a coder. You can type.</p>
<p>Publishing your own package. You can create any package you want. Perhaps one that pulls in some of your favorite utility classes. Or perhaps you’ve got some corporate libraries and frameworks that you want to be able to incorporate using NuGet. You can create your own private or a corporate NuGet gallery. Or if you have something worth while, publish it up to the official NuGet gallery, it’s own to the public.</p>
<p>I hope you’ve found this ramble of mine useful. Search around for more NuGet goodness. There are plenty of bloggers way more interesting then me publishing good information.</p>
<p>Happy Coding!</p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/CIyTSMkyaEM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/nuget-geeky-goodness/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/nuget-geeky-goodness/</feedburner:origLink></item>
		<item>
		<title>Open Source, MVC, MVVM, Entity Framework, NuGet, and More…</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/hrOs8lL_mxw/</link>
		<comments>http://blog.efvincent.com/open-source-nuget-and-more/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 13:53:31 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[EntityFramework]]></category>
		<category><![CDATA[NuGet]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[entity framework]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/?p=182</guid>
		<description><![CDATA[Updated: see the NuGet post
The 2010 PDC was a few months ago. There was plenty of interesting and exciting tech news coming from the conference, but the one session that really sparked something with me was Scott Hanselman’s Presentation, ASP.NET + Packaging + Open Source = Crazy Delicious. Officially, I was mining PDC for Azure [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fopen-source-nuget-and-more%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fopen-source-nuget-and-more%2F" height="61" width="51" /></a></div><p><strong>Updated</strong>: see the <a href="http://blog.efvincent.com/nuget-geeky-goodness/" target="_blank">NuGet post</a></p>
<p>The 2010 PDC was a few months ago. There was plenty of interesting and exciting tech news coming from the conference, but the one session that really sparked something with me was Scott Hanselman’s Presentation, <em><a href="http://www.hanselman.com/blog/PDC10BuildingABlogWithMicrosoftUnnamedPackageOfWebLove.aspx" target="_blank">ASP.NET + Packaging + Open Source = Crazy Delicious</a></em>. Officially, I was mining PDC for Azure and Windows Identity Foundation material for the project I was (and still am) working on. But I <em>love</em> Hanselman’s presentation style; is stuff is always entertaining, stimulating, and <strong>packed</strong> with information. This one was no exception.</p>
<p>In a nutshell, the presentation described several different new and pre-release technologies mashed up together into a single very technical demo. All of these techs are interesting, and over the last couple of months I’ve dived into several, if not all of them and I’ve learned a whole lot. I plan on writing about these techs, mostly because when I do I end up learning a lot in the process. But also so I can promote some of these in some small way. In the coming [random time period] I’ll be posting on:</p>
<p><a href="http://nuget.codeplex.com/" target="_blank"><strong>NuGet</strong></a> – <em>a free, open source developer focused package management system for the .NET platform intent on simplifying the process of incorporating third party libraries into a .NET application during development</em>. (that’s quoted from the Codeplex page). It’s way cooler then it sounds… you’ll see.</p>
<p><strong><a href="http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx" target="_blank">Entity Framework Magic Unicorn Edition</a></strong> – That’s what Hanselman called it, it’s got one of those painful Microsoft names in real life, but Magic Unicorn sounds better. It’s Entity Framework 4, the Code Only Entity Framework. EF without boxes and lines. It’s super cool and you’ll love it. Trust me.</p>
<p><strong><a href="http://weblogs.asp.net/scottgu/archive/2010/12/10/announcing-asp-net-mvc-3-release-candidate-2.aspx" target="_blank">ASP.NET MVC</a> –</strong> Not bleeding edge, it’s been around for a couple of years now. Version 3 is in beta right now, and it’s getting tight. Time to take a look. Even if you’re not going to use it at work next week, it’s important to start thinking about web development done differently then ASP.NET (some would argue, correctly).</p>
<p><strong>MVVM Pattern in WPF and Silverlight</strong> – Ok this one is not from Hanselman’s talk, but at the same time I’ve been looking at all the other tech I’ve had to dive into some WPF UI work for a project, and I figured I’d see what all the chatter was about with MVVM. I’ve spent so many years server side, I decided to approach UI with a clean slate.</p>
<p>I’m sure there’ll be more. I’m rehabbing from knee replacement surgery, so I have some extra time behind the keyboard. Now – <a href="http://blog.efvincent.com/nuget-geeky-goodness/">on to NuGet</a>.</p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/hrOs8lL_mxw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/open-source-nuget-and-more/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/open-source-nuget-and-more/</feedburner:origLink></item>
		<item>
		<title>5Dm2 vs S95 Holiday Shootout Deathmatch!</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/7QWjuXpLklU/</link>
		<comments>http://blog.efvincent.com/5dm2-vs-s95/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 19:42:58 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[Canon]]></category>
		<category><![CDATA[photo]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/?p=165</guid>
		<description><![CDATA[Hey that’s an enthusiastic title…
My wife and son (and what the heck, me too) were surprised to see snow falling this morning as we lazed around the fire in the den. Not just some wispy dots, but some decent sized flakes. Not very common in Greensboro, NC the first week of December. There are many [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2F5dm2-vs-s95%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2F5dm2-vs-s95%2F" height="61" width="51" /></a></div><p>Hey that’s an enthusiastic title…</p>
<p>My wife and son (and what the heck, me too) were surprised to see snow falling this morning as we lazed around the fire in the den. Not just some wispy dots, but some decent sized flakes. Not very common in Greensboro, NC the first week of December. There are many years it doesn’t snow at all down here. As a native Long Islander, my default reaction to snow in NC is “yea that’s not REAL snow”. But this kinda is. Very holiday joy, ho ho ho, jingle belly stuff.</p>
<p>So I find myself at my desk reading my RSS feeds and decide to snap a pic with my relatively new Canon S95. My primary camera is a Canon 5Dm2, but the S95 is pretty decent and fits in my pocket. I’ve been strict about taking the S95 with me pretty much whenever I take my wallet with me. Hasn’t resulted in any miracle catches yet, but it’s only a few weeks old.</p>
<p>Looking at the pic I have the same reaction that I usually have with the S95. “Meh…” it just doesn’t feel like a real camera to me. I grab the 5Dm2 that sits within arms reach of my keyboard and go for a quick comparison. To pics, approximately the same field of view. 5Dm2 vs S95. Here are the shots.</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2010/12/2-dm-full-small-c01.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="2-dm-full-small-c01" border="0" alt="2-dm-full-small-c01" src="http://blog.efvincent.com/wp-content/uploads/2010/12/2-dm-full-small-c01_thumb.png" width="240" height="313" /></a><a href="http://blog.efvincent.com/wp-content/uploads/2010/12/2-dm-full-small-c02.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="title" border="0" alt="alt" src="http://blog.efvincent.com/wp-content/uploads/2010/12/2-dm-full-small-c02_thumb.png" width="240" height="350" /></a></p>
<p>This wasn’t going to be much of a “guess which is which” thing, so I’m not ruining anything by saying right off the bat, the S95 is on the left. The color is different, that’s for sure, and this is after trying to get them somewhat close using white balance in Adobe Bridge. The S95 also has a 4:3 aspect ratio – that’s something that can be set, but the other ratios are achieved by cropping this one apparently. A 35mm frame is 3:2, which is what the 5Dm2 is on the right.</p>
<p>I used the S95 and the 5Dm2 as I would normally use them. So for the S95, that mean turning it on, finding the right focal length, adding some exposure compensation (for the darkly lit low contrast white scene). Brace my arm against the window (‘cause I don’t know what shutter speed or ISO this thing will pick), and shoot it. Shot came off at 1/125 sec, ISO 200, f4.9, +0.67 exposure comp. Not bad choices.</p>
<p>With the 5Dm2, I popped on my EF-100mm Macro lens cause it’s super sharp and I love it. I set ISO to 1000, exposure comp to a bit over one, and aperture to 4.5 (coincidentally, same as the S95 chose). Because I wanted it sharp. Shot came off at 1/400 sec. That shutter speed is going to make a difference. Which is why I set it that way. Which is what you do with a DSLR.</p>
<p>So they’re not that different shrunk all the way down to 220px. To see even more of a difference, I’ll crop it down. These crops are of a section in the lower right. They’re not at 100%, I chose a section such that both cameras would have to be reduced for the final output for web. The 5Dm2 clearly reduced more than the S95. Both had the same levels and curves applied, and both had unsharp mask 60%, 1px radius, threshold 2. Both originals where shot RAW (nice that the point and shoot has a RAW mode).</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2010/12/2-dm-full-crop-c01.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="2-dm-full-crop-c01" border="0" alt="2-dm-full-crop-c01" src="http://blog.efvincent.com/wp-content/uploads/2010/12/2-dm-full-crop-c01_thumb.png" width="320" height="470" /></a><a href="http://blog.efvincent.com/wp-content/uploads/2010/12/2-dm-full-crop-c02.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="title" border="0" alt="alt" src="http://blog.efvincent.com/wp-content/uploads/2010/12/2-dm-full-crop-c02_thumb.png" width="320" height="470" /></a></p>
<p>Yep that’s the DSLR vs point and shoot difference in a nutshell, for me. Now point and shoot is necessary sometimes. You want to have a pocketable camera. Just realize what you’re trading for convenience. </p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/7QWjuXpLklU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/5dm2-vs-s95/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/5dm2-vs-s95/</feedburner:origLink></item>
		<item>
		<title>In Car Video–Carolina Motorsports Park</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/CReHbARm2HA/</link>
		<comments>http://blog.efvincent.com/in-car-videocarolina-motorsports-park/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 23:57:06 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Porshe]]></category>
		<category><![CDATA[CMP]]></category>
		<category><![CDATA[DE]]></category>
		<category><![CDATA[Driving]]></category>
		<category><![CDATA[PCA]]></category>
		<category><![CDATA[Porsche]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/in-car-videocarolina-motorsports-park/</guid>
		<description><![CDATA[I’ve just finished uploading a new in-car video from the Hurricane region PCA Driver’s Ed event at Carolina Motorsports Park in South Carolina. View it embedded here, or click the link below the picture to go to Vimeo.
 
PCA DE CMP August 2010 from Eric Vincent on Vimeo.
Some notes about the video and data acquisition, [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fin-car-videocarolina-motorsports-park%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fin-car-videocarolina-motorsports-park%2F" height="61" width="51" /></a></div><p>I’ve just finished uploading a new in-car video from the Hurricane region PCA Driver’s Ed event at Carolina Motorsports Park in South Carolina. View it embedded here, or click the link below the picture to go to Vimeo.</p>
<p> <iframe src="http://player.vimeo.com/video/14339850" width="601" height="338" frameborder="0"></iframe>
<p><a href="http://vimeo.com/14339850">PCA DE CMP August 2010</a> from <a href="http://vimeo.com/efvincent">Eric Vincent</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Some notes about the video and data acquisition, for those interested in the gory, geeky details:</p>
<p>· Video was shot on a <a href="http://www.amazon.com/gp/product/B002VA57XC">GoPro HD</a>. Last year I tried a ContourHD, but the picture wasn’t quite as good and the sound was terrible – the GoPro does a better job dealing with the wind noise.</p>
<p>· The data (speedo, Tach, Throttle position) was collected using a <a href="http://www.amazon.com/PLX-Devices-2340-KIWI-Wifi/dp/B002ICSOTC">PLX 2340 KIWI Wifi</a>. This device interfaces with the OBD-II port of your car (also used by mechanics to read diagnostics), and transmits several different types of metrics in real time over Wi-Fi at with an observed sample rate of about 3 samples / second.</p>
<p>· The data was then recorded using <a href="http://www.devtoaster.com/products/rev/">Rev by DevToaster</a> for iPhone. This app is compatible with the PLX, so it can collect data being transmitted by your car. It also adds accelerometer data. There’s GPS too, but the iPhone’s GPS is completely unusable for track purposes. The sampling frequency is too low, and it does some very funky interpolation which in the end renders the GPS data useless. For example, here’s a simple plot (graphed using F#) of one of the runs:</p>
<p><a href="http://blog.efvincent.com/wp-content/uploads/2010/08/GPSPlot10081401.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" class="wlDisabledImage" title="GPS Plot 100814-01" border="0" alt="GPS Plot 100814-01" src="http://blog.efvincent.com/wp-content/uploads/2010/08/GPSPlot10081401_thumb.png" width="403" height="436" /></a></p>
<p>· The data is then exported from the phone as an emailed comma separated value file (.csv).</p>
<p>· A small program I wrote in F# reads the file and transforms the timestamps in to frame numbers (29.97 frames/second), and the data into either rotational data (for the analog gauges) or simple integers (for the digital gauges).</p>
<p>· That data can then be imported into Adobe After Effects. Images of the gauges (drawn from scratch in Adobe Illustrator) are animated into a video overlay using the PLX/Rev data.</p>
<p>· Adobe Premiere then takes the gauges and overlays them on top of the video from the track. There’s some fiddling involved to get the gauge data and video in sync. Premiere then renders the video (20 minutes took 6 hours&#8230; not sure if I’m doing that right).</p>
<p>· That rendered video is HD and is pretty big, so I used Microsoft Expression Encoder (Adobe has an encoder too) to re-encode the video into a smaller MP4 format that Vimeo wanted for uploading.</p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/CReHbARm2HA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/in-car-videocarolina-motorsports-park/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/in-car-videocarolina-motorsports-park/</feedburner:origLink></item>
		<item>
		<title>Combinators in C#</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/3cyYfA3kHZo/</link>
		<comments>http://blog.efvincent.com/combinators-in-c/#comments</comments>
		<pubDate>Mon, 03 May 2010 05:15:43 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Combinator]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/?p=136</guid>
		<description><![CDATA[As C# has evolved it has acquired more and more of what some people refer to has functional programming features and constructs. One such concept is the idea that a function is a &#34;first class” value. This is a fancy way of saying that functions are values that can be passed to and returned from [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fcombinators-in-c%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fcombinators-in-c%2F" height="61" width="51" /></a></div><p>As C# has evolved it has acquired more and more of what some people refer to has <em>functional programming </em>features and constructs. One such concept is the idea that a function is a &quot;first class” value. This is a fancy way of saying that functions are values that can be passed to and returned from other functions. A function that operates on other functions is called a high-order function.</p>
<p>Combinators are high order functions that compose, combine, or otherwise modify functions in useful and interesting ways. These types of operations are not typically seen in C#, but expanding your problem solving toolkit to include these concepts is not only fun and interesting, but can result in new, efficient, robust solutions.</p>
<h2>The Timer</h2>
<p>Imagine you’ve got a method that you want to put a stopwatch on. For example, in the following code, you need to see how long it’s taking each download to complete, for debugging information.</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">class Program {

    static string url;

    static void Main(string[] args) {

        url = &quot;http://microsoft.com&quot;;
        RetrieveFromWeb();

        url = &quot;http://amazon.com&quot;;
        RetrieveFromWeb();

        url = &quot;http://dpreview.com&quot;;
        RetrieveFromWeb();

        Console.Write(&quot;Press any key...&quot;);
        Console.ReadKey(true);
    }

    static void RetrieveFromWeb() {
        System.Net.WebClient wc = new System.Net.WebClient();
        var s = wc.DownloadString(url);
        Console.WriteLine(&quot;URL: {0} - string length is {1}&quot;, url, s.Length);
    }
}</pre>
<p>An ugly solution would be to do this.</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">static void Main(string[] args) {
    var sw = new System.Diagnostics.Stopwatch();

    sw.Start();
    url = &quot;http://microsoft.com&quot;;
    RetrieveFromWeb();
    Console.WriteLine(&quot;{0:#,##0}ms&quot;, sw.ElapsedMilliseconds);

    sw.Restart();
    url = &quot;http://amazon.com&quot;;
    RetrieveFromWeb();
    Console.WriteLine(&quot;{0:#,##0}ms&quot;, sw.ElapsedMilliseconds);

    sw.Restart();
    url = &quot;http://dpreview.com&quot;;
    RetrieveFromWeb();
    Console.WriteLine(&quot;{0:#,##0}ms&quot;, sw.ElapsedMilliseconds);

    Console.Write(&quot;Press any key...&quot;);
    Console.ReadKey(true);
}</pre>
<p>Try not to get physically ill. You and I both know there’s plenty of code running around in the wild that looks a lot like this. Red flags – you see repeated code – starting, stopping, printing the time. Should we go into the RetrieveFromWeb() method and modify it for timing? Let’s not. Lets try this instead. Step one, let’s define a variable for the method we’re calling. Leaving out the timing code for now, we make this change:</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">

static void Main(string[] args) {

    // Create a delegate, set it to the method of interest

    Action retrieveFromWeb = RetrieveFromWeb;

    url = &quot;http://microsoft.com&quot;;
    retrieveFromWeb();

    url = &quot;http://amazon.com&quot;;
    retrieveFromWeb();

    url = &quot;http://dpreview.com&quot;;
    retrieveFromWeb();

    Console.Write(&quot;Press any key...&quot;);
    Console.ReadKey(true);
}
</pre>
<p>I’ve added a local variable of type <em>Action, </em>and set it equal to the call to the method <em>RetrieveFromWeb(),</em> and now we’re calling that method indirectly. It has the same exact effect, the method gets called three times. Only we’ve added a layer of indirection. We do this all the time in OO programming; for example, you might have a Person object, but rather than coding directly to that object, you create an IPerson interface, and code to that, opening up the possibility of mocking the object, decorating it, etc. Similar thing here. Rather than “binding” the call sites directly to the method, we’re binding to a variable that points to the method.</p>
<p>We’re doing this because now we’ve got a value that can be altered or augmented to add additional functionality. This is where a combinator comes in. This is a simple timer combinator:</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">class Combinators {

    public static Action Time(Action a) {
        return () =&gt; {
            var sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            try {
                a();
            } finally {
                Console.WriteLine(&quot;{0:#,##0}ms&quot;, sw.ElapsedMilliseconds);
            }
        };
    }
}</pre>
<p>This combinator takes an <em>Action</em> and returns a new action (aka a new function), that has timing included. At line 8 the parameter action is being called, the timing is what’s added. The only change we have to make to the main method is where the delegate is being defined:</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">

Action retrieveFromWeb = Combinators.Time(RetrieveFromWeb);
</pre>
<p>The rest of the method stays the same, but now, there’s timing added. This is a super-simple, contrived example, but you should be starting to see what’s possible. Let’s take this a step further. Here’s an example simulating retrieval of information from the database:</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">static void Main(string[] args) {

    Func&lt;string, Guid&gt; lookupUser = LookupUser;

    var emails = new[] {
        &quot;eric@work.com&quot;, &quot;joel@office.com&quot;, &quot;cole@school.com&quot;,
        &quot;karin@job.com&quot;, &quot;haley@home.com&quot; };

    foreach (var em in emails) {
        var id = lookupUser(em);
        Console.WriteLine(&quot;user {0} id = {1}&quot;, em, id);
    }

    Console.Write(&quot;Press any key...&quot;);
    Console.ReadKey(true);
}

static Guid LookupUser(string email) {
    // Fake looking up a user in the database
    var rnd = new Random(System.DateTime.Now.Millisecond);
    Thread.Sleep(rnd.Next(250, 2500));
    return Guid.NewGuid();
}</pre>
<p>Memoization is the idea that a function can remember the results for given parameters. It’s like caching, but the term is specific to caching function results based on input. Here’s a simple Memoization combinator for C#:</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">public static Func&lt;A, B&gt; Memoize&lt;A, B&gt;(Func&lt;A, B&gt; fn) {
    var dict = new Dictionary&lt;A, B&gt;();
    return a =&gt; {
        B b = default(B);
        if (!dict.TryGetValue(a, out b)) {
            b = fn(a);
            dict.Add(a, b);
        }
        return b;
    };
}</pre>
<p>It’s simple, but it demonstrates some very useful and interesting functional programming concepts. First, it’s takes and returns Func&lt;A,B&gt;. This is a delegate with a parameter of type A that returns a type B. This will work for effectively any method with that signature. Next point of interest, a dictionary is created, then the lambda is created and returned. The lambda refers to the dictionary defined outside the lambda. It is said that the dictionary is <em>captured</em> in a <em>closure</em>. It’s not important that you remember the terms, but look over the code and see if the concept is clicking for you. This function will return (effectively) a function, the dictionary is <em>captured</em> by that function. Even when the call to Memoize() goes out of scope, the dict variable will still exist in the returned function. Enough talk. We modify the main program just slightly:</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">Func&lt;string, Guid&gt; lookupUser = Combinators.Memoize(LookupUser);</pre>
<p>The Memoize function will create a new function, one that caches results of the LookupUser function <em>automatically.</em> Nothing else has to change in the program to take advantage of this. Want to be sure that it’s actually working? Time it! The non-memoized LookupUser() has a built in Thread.Sleep(2500), and so takes 2.5sec * number of lookups to run. The memoized version will run almost instantly, so we can prove the memoizer is working by timing. The time combinator we created earlier was for timing Actions. I’ve created a overload of the time combinator that has the signature we need &#8211; Func&lt;A,B&gt;:</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">public static Func&lt;A, B&gt; Time&lt;A, B&gt;(Func&lt;A, B&gt; fn) {
    return a =&gt; {
        var sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        try {
            return fn(a);
        } finally {
            Console.WriteLine(&quot;{0:#,##0}ms&quot;, sw.ElapsedMilliseconds);
        }
    };
}</pre>
<p>and added it to the definition of lookUpUser in the main function</p>
<pre class="brush: csharp; toolbar: false; auto-links: false;">

Func&lt;string, Guid&gt; lookupUser =
    Combinators.Time(
        Combinators.Memoize&lt;string,Guid&gt;(LookupUser));
</pre>
<p>Again without modifying the main code, we’ve augmented the method. Throw in some duplicate email addresses to test it out. The real power of these techniques becomes more evident the more you use them. Functional programming developers have been using these are similar techniques for years, and now with the added ability and flexibility of C#, we can employ these patters as well.</p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/3cyYfA3kHZo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/combinators-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/combinators-in-c/</feedburner:origLink></item>
		<item>
		<title>Parsing Json using F#</title>
		<link>http://feedproxy.google.com/~r/CurriedFunctions/~3/N2TO4Kcy2Q8/</link>
		<comments>http://blog.efvincent.com/parsing-json-using-f/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 18:29:49 +0000</pubDate>
		<dc:creator>EFVincent</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[parser]]></category>

		<guid isPermaLink="false">http://blog.efvincent.com/?p=116</guid>
		<description><![CDATA[I was in one of those curious moods the other day, and decided to check out the API for the service behind my RSS reader, FeedDemon. I like this reader because it keeps my subscriptions and read / unread data synchronized across machines and my iPhone via an app called NetNewsWire.
Well it ends up that [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.efvincent.com%2Fparsing-json-using-f%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.efvincent.com%2Fparsing-json-using-f%2F" height="61" width="51" /></a></div><p>I was in one of those curious moods the other day, and decided to check out the API for the service behind my RSS reader, <a href="http://www.newsgator.com/Individuals/FeedDemon/Default.aspx" target="_blank">FeedDemon</a>. I like this reader because it keeps my subscriptions and read / unread data synchronized across machines and my iPhone via an app called <a href="http://netnewswireapp.com/iphone/" target="_blank">NetNewsWire</a>.</p>
<p>Well it ends up that Google has gobbled up the service and it’s now Google Reader. Fair enough. It means you can also read your RSS feeds on the web at the Google reader site. Whatevs. It also turns out that Google has not publicized the API for that service yet. Yuck. But there are a couple of people out there (like <a href="http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI" target="_blank">here</a> and <a href="http://blog.martindoms.com/2009/08/15/using-the-google-reader-api-part-1/" target="_blank">here</a>) who have picked it apart using fiddler or some such thing.</p>
<p>Well to put this meandering story to an end, I was playing with the API and it seems that some of the methods only return JSON. Others return XML, and others still are switchable. Messy. No wonder it’s not public yet. So that brings me to the point of this post… Parsing Json using F#.</p>
<h3>Why F#?</h3>
<p>I’ve avoided posting about F# to date, even though I love “playing” with it; I feel that posts about C# are more relevant. People are using F#, but I’ve never had the chance to use it on the job, and I don’t know anyone personally who has either. But since it’s <em>mainstream</em> now, what the heck. Plus parsing is one of those tasks that’s right up F#’s alley. If you haven’t worked with a functional language since college (or ever), give it a whirl. It’s refreshingly different from pure, straight, intensely object oriented thinking.</p>
<h3>Json, Briefly</h3>
<p>Most everyone knows what Json is by now. It’s just a tad more horrifying in real life as the supernatural killer from the Friday the 13th movies with whom it homophonetically shares its name. It’s a text format for representing data that can be evaluated in that wonderfully fast and lose language of the web, JavaScript, resulting in actual JavaScript objects. Read about it from the <a href="http://json.org" target="_blank">experts here</a>. Here’s an example of some awesome <a href="http://json.org" target="_blank">Json</a>:</p>
<pre class="brush: js; toolbar: false; auto-links: false;">{
    &quot;glossary&quot;: {
        &quot;title&quot;: &quot;example glossary&quot;,
        &quot;GlossDiv&quot;: {
            &quot;title&quot;: &quot;S&quot;,
            &quot;GlossList&quot;: {
                &quot;GlossEntry&quot;: {
                    &quot;ID&quot;: &quot;SGML&quot;,
                    &quot;SortAs&quot;: &quot;SGML&quot;,
                    &quot;GlossTerm&quot;: &quot;Standard Generalized Markup Language&quot;,
                    &quot;Acronym&quot;: &quot;SGML&quot;,
                    &quot;Abbrev&quot;: &quot;ISO 8879:1986&quot;,
                    &quot;GlossDef&quot;: {
                        &quot;para&quot;: &quot;A meta-markup language, used to create markup languages such as DocBook.&quot;,
                        &quot;GlossSeeAlso&quot;: [&quot;GML&quot;, &quot;XML&quot;]
                    },
                    &quot;GlossSee&quot;: &quot;markup&quot;
                }
            }
        }
    }
}</pre>
<h3>The Tokenizer</h3>
<p>I’m no parsing / compiler / language expert, but I do know that tokenizing is the first step. This is when we run through the string and create tokens; identifying the open and close braces, the name value pairs, etc.</p>
<p>First thing we want to look at is the Token type, which is a discriminated union. This type defines the <em>logical</em> things that are in the Json string. For example, an open quote, some characters, and a close quote is a string.</p>
<pre class="brush: fsharp; toolbar: false; auto-links: false;">type Token =
  | OpenBracket | CloseBracket
  | OpenArray | CloseArray
  | Colon | Comma
  | String of string
  | Number of string</pre>
<p>For this simple parser, the tokens are as above, open and close bracket and square bracket (aka array), colon, comma, numbers, and strings. This is a good reason to start with Json, it’s painfully simple. The tokenize function below turns the string into a list of tokens.</p>
<pre class="brush: fsharp; toolbar: false; auto-links: false;">let tokenize source = 

  let rec parseString acc = function
    | '\\' :: '&quot;' :: t -&gt; // escaped quote
                          parseString (acc + &quot;\&quot;&quot;) t
    | '&quot;' :: t -&gt; // closing quote terminates
                  acc, t
    | c :: t -&gt; // otherwise accumulate
                parseString (acc + (c.ToString())) t
    | _ -&gt; failwith &quot;Malformed string.&quot;

  let rec token acc = function
    | (')' :: _) as t -&gt; acc, t // closing paren terminates
    | (':' :: _) as t -&gt; acc, t // colon terminates
    | (',' :: _) as t -&gt; acc, t // comma terminates
    | w :: t when Char.IsWhiteSpace(w) -&gt; acc, t // whitespace terminates
    | [] -&gt; acc, [] // end of list terminates
    | c :: t -&gt; token (acc + (c.ToString())) t // otherwise accumulate chars 

  let rec tokenize' acc = function
    | w :: t when Char.IsWhiteSpace(w) -&gt; tokenize' acc t   // skip whitespace
    | '{' :: t -&gt; tokenize' (OpenBracket :: acc) t
    | '}' :: t -&gt; tokenize' (CloseBracket :: acc) t
    | '[' :: t -&gt; tokenize' (OpenArray :: acc) t
    | ']' :: t -&gt; tokenize' (CloseArray :: acc) t
    | ':' :: t -&gt; tokenize' (Colon :: acc) t
    | ',' :: t -&gt; tokenize' (Comma :: acc) t
    | '&quot;' :: t -&gt; // start of string
      let s, t' = parseString &quot;&quot; t
      tokenize' (Token.String(s) :: acc) t'
    | '-' :: d :: t when Char.IsDigit(d) -&gt; // start of negative number
        let n, t' = token (&quot;-&quot; + d.ToString()) t
        tokenize' (Token.Number(n) :: acc) t'
    | '+' :: d :: t | d :: t when Char.IsDigit(d) -&gt; // start of positive number
        let n, t' = token (d.ToString()) t
        tokenize' (Token.Number(n) :: acc) t'
    | [] -&gt; List.rev acc // end of list terminates
    | _ -&gt; failwith &quot;Tokinzation error&quot;

  tokenize' [] source</pre>
<p>We don’t have time for the full treatment of F#. If there’s any interest, I’ll post up some tutorials or at least links to some of the very many existing good tutorials out there already. For now, assume we have an understanding of the syntax, and the logic of tokenizing and parsing is what we’re after here.</p>
<p>One of the first things we see is that <em>tokenize</em> defines a few functions within the function. Great feature of F#, allowing definitions of inner functions; it allows the coder to partition logic without having function explosion.</p>
<p>The driving inner function is <em>tokenize’</em>, whose signature is (Token list –&gt; char list –&gt; Token list). This means it takes a Token list and a char list, and returns a Token list. The first token list (acc) is an accumulator. This list is built up as the procedure calls itself recursively. This is a common pattern in functional languages, to “thread” an accumulator through recursive calls. The second parameter is a char list, or a list of characters from the source string.</p>
<p>We don’t see the char list explicitly because of the use of the&#160; <em>function</em> keyword at like 20, this keyword means that the last parameter (the char list) is implicitly used in a match statement, the cases of which follow after the function keyword. The syntax would be equivalent to:</p>
<pre class="brush: fsharp; toolbar: false; auto-links: false;">  let rec tokenize' acc sourceChars =
    match sourceChars with
    | w :: t when Char.IsWhiteSpace(w) -&gt; tokenize' acc t   // skip whitespace
    | '{' :: t -&gt; tokenize' (OpenBracket :: acc) t
    | '}' :: t -&gt; tokenize' (CloseBracket :: acc) t
...</pre>
<p>The idea that the sourceChars variable being passed immediately to the match..with construct is so common in F# that the <em>function</em> keyword is used as a contraction, allowing the elimination of what is an unnecessary variable. In many cases, this construct allows for a one line function definition.</p>
<p>Most of the cases in the block matches a character which in turn maps to a token, which is appended to the accumulator (acc : Token list), and passed to a recursive call of tokenize’. This way the tokens are built up (in reverse order) as the string is traversed. This is seen in lines 22-27. Line 21 skips whitespace by calling tokenize’ without adding a token to the accumulator first.</p>
<p>Things are a bit more interesting at lines 28, 31, and 34. Line 28 detects the beginning of a string, and starts a new recursive thread with the <em>parseString</em> function, the signature of which is (string –&gt; char list –&gt; string * char list). So the accumulator is a string, the “work” is being done on a char list, to which is passed our source char list, and the return is a tuple of string * char list, which is our parsed string and the rest of the source char list. One (of I’m sure <strong>many</strong>) optimizations that could be made is to use a mutable StringBuilder as the accumulator in parseString.</p>
<p>The <em>token</em> function on line 12 does a similar job for non-string literals (numbers in this case, there are no bools in Json).</p>
<p>Lastly, line 37 handles the case where we run out of source characters. The Token list we’ve been accumulating is backwards; we’ve been “appending” to the front of the list all this time. This is a common pattern. At the end, we return the reverse of the list. When the Json at the top is put through the tokenizer, we get this:</p>
<pre class="brush: fsharp; toolbar: false; auto-links: false;">&gt; let tk = tokenize source;;

val tk : Token list =
  [OpenBracket; String &quot;glossary&quot;; Colon; OpenBracket; String &quot;title&quot;; Colon;
   String &quot;example glossary&quot;; Comma; String &quot;GlossDiv&quot;; Colon; OpenBracket;
   String &quot;title&quot;; Colon; String &quot;S&quot;; Comma; String &quot;GlossList&quot;; Colon;
   OpenBracket; String &quot;GlossEntry&quot;; Colon; OpenBracket; String &quot;ID&quot;; Colon;
   String &quot;SGML&quot;; Comma; String &quot;SortAs&quot;; Colon; String &quot;SGML&quot;; Comma;
   String &quot;GlossTerm&quot;; Colon; String &quot;Standard Generalized Markup Language&quot;;
   Comma; String &quot;Acronym&quot;; Colon; String &quot;SGML&quot;; Comma; String &quot;Abbrev&quot;;
   Colon; String &quot;ISO 8879:1986&quot;; Comma; String &quot;GlossDef&quot;; Colon; OpenBracket;
   String &quot;para&quot;; Colon;
   String
     &quot;A meta-markup language, used to create markup languages such as DocBook.&quot;;
   Comma; String &quot;GlossSeeAlso&quot;; Colon; OpenArray; String &quot;GML&quot;; Comma;
   String &quot;XML&quot;; CloseArray; CloseBracket; Comma; String &quot;GlossSee&quot;; Colon;
   String &quot;markup&quot;; CloseBracket; CloseBracket; CloseBracket; CloseBracket;
   CloseBracket]</pre>
<p>This list of tokens is an intermediate step. From here, we could go in several directions. For my purposes, I wanted to see it as XML. To get there, I created a parser that takes the token list and returns an XElement.</p>
<pre class="brush: fsharp; toolbar: false; auto-links: false;">let parseToXml source =
  let map = function
    | Token.Number(n) -&gt; n.ToString()
    | Token.String(x) -&gt; x
    | v -&gt; failwith &quot;Syntax Error, unrecognized token in map()&quot;

  let rec parseValue (acc:XElement) = function
    | OpenBracket :: t -&gt;
      let newElement, t' = parseElement acc t
      newElement, t'
    | OpenArray :: t -&gt;
      let name = acc.Name.LocalName
      if name.EndsWith(&quot;ies&quot;) &amp;&amp; name.Length &gt; 3 then
        let childName = name.Substring(0, name.Length - 3) + &quot;y&quot;
        let newListElement, t' = parseArray childName acc t
        newListElement, t'
      elif name.EndsWith(&quot;s&quot;) &amp;&amp; name.Length &gt; 1 then
        let childName = name.Substring(0, name.Length - 1)
        let newListElement, t' = parseArray childName acc t
        newListElement, t'
      else
        let childName = acc.Name.LocalName
        acc.Name &lt;- XName.Get(childName + &quot;s&quot;)
        let newListElement, t' = parseArray childName acc t
        newListElement, t'
    | h :: t -&gt;
      acc.Value &lt;- map(h)
      acc, t
    | _ -&gt; failwith &quot;bad value&quot;

  and parseArray name acc = function
    | Comma :: t -&gt; parseArray name acc t
    | CloseArray :: t -&gt;  acc, t
    | t -&gt;
      let newElement = XElement(XName.Get(name))
      let acc', t' = parseValue newElement t
      acc.Add(acc')
      parseArray name acc t'

  and parseElement (acc : XElement) = function
    | Comma :: t -&gt;
      parseElement acc t
    | Token.String(n) :: Colon :: t -&gt;
      let newElement = XElement(XName.Get(n))
      let v, t' = parseValue newElement t
      acc.Add(v)
      parseElement acc t'
    | CloseBracket :: t -&gt;
      acc, t
    | _ -&gt; failwith &quot;Malformed JSON object&quot;

  let root = XElement(XName.Get(&quot;root&quot;))
  let tokens = tokenize source

  match tokens with
    | OpenBracket :: t -&gt;
      let result, t' = parseElement root t
      result
    | _ -&gt; failwith &quot;Json did not begin with an object&quot;</pre>
<p>There is still syntax checking happening in the parse step. The tokenizer would have found illegal tokens, but the parse function will find illegal Json structure. It works in a similar way to the tokenizer, except instead of a raw stream of characters, we’ve got a stream of tokens, which is examined one by one and passed to a set of recursive functions which accumulates the XML, in this case as an XElement.</p>
<p>parseToXml defines inner functions <em>map</em>, <em>parseValue</em>, <em>parseArray</em>, and <em>parseElement.</em> It then gets to work by creating a root XElement, and getting the Token list. Json should start with an OpenBracket token. Anything else and we failwith an error (like C#’s throw). When we find that OpenBracket, the recursion begins with a call to parseElement. The root XElement serves as the accumulator, and the Token list is the “work” to be done.</p>
<p>You should be able to see what’s happening if you’re somewhat comfortable with F#. The parseValue function does some extra work to wrap arrays in nodes where the singularity / plurality of the node makes it read a bit better (in English, most of the time). The other point of interest is that the accumulator is a mutable object in parseToXml, unlike the Token list was in tokenize. At lines 37 and 46, the newly created elements are added to the accumulator using the Add(XElement) method, which mutates the accumulator and keeps using it. This is a departure from “purer” functional techniques, but that’s what’s cool about F#, you can make those departures where it makes sense. In this case, leveraging the XElement class of the .NET framework was worth it. Consuming this class from C# is that much easier and more intuitive.</p>
<p>After all is said and done, this is the XML that is emitted.</p>
<pre class="brush: xml; toolbar: false; auto-links: false;">&lt;root&gt;
  &lt;glossary&gt;
    &lt;title&gt;example glossary&lt;/title&gt;
    &lt;GlossDiv&gt;
      &lt;title&gt;S&lt;/title&gt;
      &lt;GlossList&gt;
        &lt;GlossEntry&gt;
          &lt;ID&gt;SGML&lt;/ID&gt;
          &lt;SortAs&gt;SGML&lt;/SortAs&gt;
          &lt;GlossTerm&gt;Standard Generalized Markup Language&lt;/GlossTerm&gt;
          &lt;Acronym&gt;SGML&lt;/Acronym&gt;
          &lt;Abbrev&gt;ISO 8879:1986&lt;/Abbrev&gt;
          &lt;GlossDef&gt;
            &lt;para&gt;A meta-markup language, used to create markup languages such as DocBook.&lt;/para&gt;
            &lt;GlossSeeAlsos&gt;
              &lt;GlossSeeAlso&gt;GML&lt;/GlossSeeAlso&gt;
              &lt;GlossSeeAlso&gt;XML&lt;/GlossSeeAlso&gt;
            &lt;/GlossSeeAlsos&gt;
          &lt;/GlossDef&gt;
          &lt;GlossSee&gt;markup&lt;/GlossSee&gt;
        &lt;/GlossEntry&gt;
      &lt;/GlossList&gt;
    &lt;/GlossDiv&gt;
  &lt;/glossary&gt;
&lt;/root&gt;</pre>
<p>It isn’t the most sophisticated, performant, or I’m sure accurate Json parser out there. But it does the trick for my immediate need, is a good starting point for more sophistication later if needed, and was an excellent exercise in parsing and F#. It was fun to write, and hopefully it stimulates some curiosities. Let me know if you’ve got any questions or comments.</p>
<img src="http://feeds.feedburner.com/~r/CurriedFunctions/~4/N2TO4Kcy2Q8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.efvincent.com/parsing-json-using-f/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.efvincent.com/parsing-json-using-f/</feedburner:origLink></item>
	</channel>
</rss>
