For my first attempt at blogging I went with the industry heavyweight: WordPress. In general I’d sum it up as annoying. Whilst it’s amazingly powerful and flexible the editor has a nasty habit of mangling content and theming it involves PHP, it also involves faffing around with various plugins if you want decent performance.
Next I gave Ghost a try, I really like the admin UI, writing posts in Markdown is great and the default theme is lovely, however I went with the self hosted option and keeping it up to date on Azure was more hassle than it was worth. I might give Ghost another try once it hits version 1.
Which brought me on to Github pages. They’re powered by Jekyll, support Markdown and HTML and best of all there’s nothing to keep up to date.
I’m now trying to come up with an exciting design (not my strong point), for now I’ve come up with something based on Poole by @mdo.
Not sure yet whether I’ve had a really amazing idea or a really stupid one - so that’s all I’m going to say for now. If it turns out to be as good as I hope expect to see a follow up post in the not too distant future. On the other hand if it’s as bad an idea as I suspect it may be I’ll probably just go quiet and forget to blog for a while longer.
If you’ve been using Ninject for years, this probably isn’t the blog post for you. If however you’re just staring out or have just been using it on the web it may be some use.
At work we’ve been using Ninject in our .NET MVC web projects for a while now, our Windows services on the other hand are all a few years old and haven’t really changed for a good while. They tend to just instantiate classes here and there making them nigh on untestable.
A recent project has lead to us rewriting one of our core Windows services and this seemed the ideal oportunity to bring in full unit testing and dependency injection.
If you follow the Ninject documentation it’s nice an easy to get started, however it’s not too clear on how you actually create instances of a classes with constructor dependencies (this happens as if by magic in MVC projects).
So here’s my example of how to get this working with a Windows service.
We’ll start of with the basic classes our service depends upon:
A basic reposity style setup that would normally talk to your database.
publicinterfaceIWidgetService{intCountWidgets();}publicclassWidgetService:IWidgetService{publicintCountWidgets(){// Lets skip the database for now :) return5;}}
A processing class that makes use of your repository. We’ll make use of constructor injection here.
Here’s a basic (and completely useless) Windows service that just instantiates classes when required.
publicclassWindowsService:ServiceBase{publicWindowsService(){// This is the bit we want to tidy up varservice=newWidgetService();varprocessor=newWidgetProcessor(service);processor.DoSomething();}/* lots of missing code */}
Adding Ninject
Use NuGet to add the Ninject reference to your project
Then add a class that inherits from NinjectModule - such as the following - that sets up mapping of your interfaces to concrete classes.
Add a Ninject kernel in to your Windows service ready for dependency injection.
publicclassWindowsService:ServiceBase{publicWindowsService(){varkernel=newStandardKernel();kernel.Load(Assembly.GetExecutingAssembly());// This is the bit we want to tidy up varservice=newWidgetService();varprocessor=newWidgetProcessor(service);processor.DoSomething();}/* lots of missing code */}
Inject your dependencies
So this is your final change, get rid of that WidgetService instance.
publicclassWindowsService:ServiceBase{publicWindowsService(){varkernel=newStandardKernel();kernel.Load(Assembly.GetExecutingAssembly());varprocessor=kernel.Get<WidgetProcessor>();processor.DoSomething();}/* lots of missing code */}
As you can see, we completely remove any reference of the WidgetService repository and instead ask Ninject for an instance of a WidgetProcessor.
The mapping you set up in the NinjectBindings lets Ninject know which concrete classes you want in place of interfaces, so it will automatically create a WidgetService instance and pass in to the WidgetProcessor constructor leaving you with clean, testable code.
So I thought I’d try again at this blogging lark. I expect the novelty to wear off within a few weeks and this content will then languish here ignored, but for now I’ve got to at least start with good intentions.
In an effort to keep my interest going a little longer than last time I thought I’d try out something a bit different to the LAMP stack I’ve been ignoring for the last few years.
I’m hosting on Windows Azure (I knew the free MSDN credits would come in useful for something) and running Ghost. It was a doddle to set up, the Ghost team have added a single click installer to the Azure gallery.
And now, that’s enought of the standard first post waffle, I better get on with trying some real content.