Ninject.Extensions.Conventions Preview

by Ian Davis on February 26, 2010

in Programming

I have been playing with the Ninject conventions binding extension syntax and usage model. It was originally based on the StructureMap assembly scanner, but I want to use the API in a different fashion.

I have been working on making the API more like LINQ in the way you think and use it. Here is a sampling of the new API that I am testing. Remember, you would never bind like this, ideally you would set up a few different scans to bind everything the way you want it.

IKernel kernel = new StandardKernel();
kernel.Scan(
	x =>
	{
		// set up the source collection of types to process
		x.FromCallingAssembly();
		x.From( from assembly in AppDomain.CurrentDomain.GetAssemblies()
				from type in assembly.GetExportedTypes()
				select type );
		x.FromAssemblyContaining<IGenericView>();
		x.FromAssembliesInPath( "." );
		x.FomAssembliesMatching( "*.Model.dll" );
		x.FromAssemblyContaining(new[] {typeof (IGenericView), typeof (StringView)});

		// Filter out what you want
		x.Where( type => !type.IsAbstract );
		x.WhereTypeIsInNamespace( "Base.Model" );
		x.WhereTypeIsInNamespaceOf<StringView>();
		x.WhereTypeIsNotInNamespace( "Base.Test" );
		x.WhereTypeIsNotInNamespaceOf<StandardKernel>();
		x.WhereTypeInheritsFrom<IGenericView>();

		// Exclude items even if they match your Where criteria
		x.Excluding<INinjectSettings>();
		x.Excluding( new[] {typeof (IServiceProvider), typeof (IInitializable)} );

		// Include items whether or not they match your Where criteria
		x.Including<DefaultView>();

		// Tell the scanner how to determine bindings
		x.BindWithDefaultConventions();

		// Declare the binding scope for scanned members
		x.InTransientScope();
	} );

Some API calls will not be available on all platforms due to security restrictions of course.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • email
  • LinkedIn
  • RSS
  • StumbleUpon
  • Technorati
  • Twitter
blog comments powered by Disqus

Previous post:

Next post: