Nov 12 2008

Dependency Injection for the Compact Framework

Category: Software DevelopmentJeff @ 08:03

I've spent the last 2 years developing WinForms and ASP.Net MVC applications, so it's been a while since I've worked on a .NET Compact Framework project.  In case you haven't heard of it, the .NET Compact Framework is a compact sub-set of the .NET framework that can run on devices with limited resources (like phones, blackberries, barcode scanners, etc.).  As I began working on the project, I decided up front that I would use the same SOLID principles I use on any other application.  I almost hit a roadblock, however, when it came to choosing a Dependency Injection framework.

I began with just a simple roll-your-own dependency resolver.  But this solution doesn't work well once you need to inject multiple layers of dependencies and build up your object hierarchies in a more complex fashion.  So I did some research into which (if any) DI frameworks support the Compact Framework.  It turns out that only one DI framework supports the Compact Framework: Ninject.  Typically StructureMap has been my DI container of choice, and recently I've spent some time evaluating the Unity container from Microsoft, but neither of these support the Compact Framework.  So I guess I've got a limited number of choices:

  1. Don't use IoC and dependency injection
  2. Continue to "roll-my-own" dependency injection tool
  3. Pray that Jeremy Miller ports StructureMap to the Compact Framework
  4. Use Ninject

Option 1 is not even a real option.  There are too many benefits to be gained from loose coupling and depending on abstractions rather than details.  Option 2 seemed feasible at first, until I needed more complicated object hierarchies.  I can't hold my breath and wait on something that may never happen, so Option 3 isn't a real option either.  So, it looks like I'm going to be heading to Ninja school!  I'll let you know how it goes and what I discover along the way.

Tags: , , , ,

Comments

1.
David Kemp United Kingdom says:

Doen't Ninject require .Net Framework 3.5 (or equivalent Compact Version?)
Will using Ninject restrict your software to the very latest devices?

2.
jeffd United States says:

Hi David,

According to the download page for .NET CF 3.5 ( www.microsoft.com/.../details.aspx ), device support for CF 3.5 goes back as far as Windows Mobile 2003 SE.

Also, I'm pretty sure that Ninject only requires CF 2.0.  I checked out the source code for Ninject, and in the build script I found the following comment: "The v3.5 compiler is used to take advantage of C# 3.0 features, but the code is compatible with v2.0 of the framework."  In addition to this, the build output is placed in a folder called "netcf-2.0" and the binary download is also listed for CF 2.0.  Just in case, I've send an email to Nate Kohari to get a final confirmation, but it looks to me like you can use Ninject with CF 2.0.

--Jeff

3.
jeffd United States says:

Nate Kohari says:

Jeff:

Ninject should work on CF 2.0, but I haven't used it myself since before 1.0. However, you will need the C# compiler from Visual Studio 2008 in order to build it from source. If you run into trouble with using Ninject on CF 2.0, please let me know.

Thanks,
Nate

4.
David Kemp United Kingdom says:

Thanks. Really. I'm not in a position to use this information at the moment - I kind of wish I'd have done more investigations a month ago.
I'd be interested to know what performance is like on a mobile device too - as reflection and "dynamic" parts of the .Net framework are well documented to be slower than their static counterparts (and understandable so).
Also, I'd be interested as to how you do any UI/Frontend automated testing - I can see the Business logic _should_ be the same on any platform - but the other parts (integration testing and UI testing) are going to be trickier.

5.
jeffd United States says:

In the future, I'll post regarding the performance of Ninject on the Compact Framework.  At this point my primary goal is developer productivity, so I'll go ahead and avoid premature optimization at this point.  It's a calculated risk, to be sure, but one I'm willing to take since I am convinced that Mobile application development must begin to benefit from good SOLID principles, patterns and practices.  IoC and Dependency Injection is just one part of this approach, but an important one.

Regarding UI testing, I can think of a couple of options.  One would be to execute automated UI tests against the application while running on the full .NET framework.  Of course if there are any behavioral differences in the application based on which version of the framework currently being targeted, there would be some issues.  But at least a reasonable level of coverage could be attained.

Another approach would be to implement a MVP approach where views are backed by a base class or interface that is testable.  With this approach the behavioral interactions of views could be tested, but of course this isn't the same as automated UI testing.

6.
Germán Schuager Argentina says:

Hi Jeff,
I've faced the same problem almost an year ago and I ended developing my own IoC framework.
In case you are interested, it is hosted here: http://code.google.com/p/compactcontainer/
and you can find some more information here: gschuager.blogspot.com/.../...ontainer-on-net.html

I don't think it has all the features of Ninject (I haven't used it) but worked pretty well for my requirements.

Regards.

7.
jeffd United States says:

Germán,

Your project looks solid and is much leaner than Ninject. Also, Ninject's registration process is quite unique in comparison to the majority of DI Frameworks. This makes it difficult to use with a Common Service Locator strategy ( http://www.codeplex.com/CommonServiceLocator ). But the CompactContainer fits this paradigm well. Thanks for passing this on.

Comments are closed