Caliburn.Micro and Ninject example

Apr 2, 2011 at 10:46 AM
Edited Apr 2, 2011 at 10:48 AM

Hi.

I've just started to learn Caliburn.Micro and as I'm using Ninject instead of MEF I wonder if there are anyone out there who's got an example on how to set up a custom Caliburn bootstrapper with Ninject?

TIA

-- norgie

Apr 2, 2011 at 11:26 AM
Edited Apr 2, 2011 at 11:33 AM

Here's a stripped-down (and untested) version from a project I did, shows the very basics:

http://caliburnmicro.codeplex.com/discussions/252240

namespace NinjectSample
{
	using System;
	using System.Collections.Generic;
	using System.Reflection;
	using System.Windows.Controls;
	using System.Windows.Data;
	using Caliburn.Micro;
	using Ninject;

	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
		"CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",
		Justification = "Entry class is never Disposed, cleanup handled already by OnExit")]
	public sealed class NinjectBootstrapper : Bootstrapper<IShell>
	{
		#region Fields
		private IKernel _kernel;
		#endregion
		#region Base functionality overrides
		protected override void Configure()
		{
			_kernel = new StandardKernel();

			_kernel.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();
			_kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();

			_kernel.Bind<IShell>().To<ShellViewModel>().InSingletonScope();
		}

		protected override void OnExit(object sender, EventArgs e)
		{
			_kernel.Dispose();
			base.OnExit(sender, e);
		}
		#endregion
		#region IoC method overrides
		protected override object GetInstance(Type serviceType, string key)
		{
			if (serviceType != null)
			{
				return _kernel.Get(serviceType);
			}

			throw new ArgumentNullException("serviceType");
		}

		protected override IEnumerable<object> GetAllInstances(Type serviceType)
		{
			return _kernel.GetAll(serviceType);
		}

		protected override void BuildUp(object instance)
		{
			_kernel.Inject(instance);
		}
		#endregion
	}
}

You'll probably want to check out this post as well.

Apr 2, 2011 at 3:15 PM

Thanks for the quick reply. I'll have a closer look at this later (as well as the other post as well).

--norgie