Code Pyre

All Code Dies and Burns in Time

Fork me on GitHub

Ninject.Extensions.Conventions Preview

| Comments

EDIT The conventions extension has been completely rewritten with a new API for Ninject 3.0. This article references the 2.X releases.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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.

Comments