Skip to content

Projecting Services to Bind

Chuck Durfee edited this page Aug 13, 2013 · 10 revisions

Once you have identified the components that you want to bind To, you have to specify the strategy that is to be used to create the Bindings.

All the built-in strategies share a common trait - where a binding defines a scoping rule, that scoping function is used for every bound service Type of the component. In other words, regardless of which of the potentially many service Types that have been bound To a component is used to resolve an instance, the scoping parent is shared across each of the Bindings. e.g., if you bind all interfaces and specify that they are in singleton scope you will always get the same instance regardless of which interface you request.

Method Description
BindAllBaseClasses() Binds all base types of the selected components to the current component.
BindAllInterfaces() Binds all interfaces of the selected component to the selected component. e.g.,
kernel.Bind<IInterface1, IInterface2>().To<Component>()
BindBase() Binds the immediate base type of the selected components to the current component.
BindDefaultInterface() Binds the default interface of the given types to the type. e.g. Foo : IFoo
BindDefaultInterfaces() Binds the default interfaces of the given types to the type. e.g., MyFoo matches IFoo; SuperCrazyFoo matches ICrazyFoo and IFoo
BindSingleInterface() Requires that the given type has exactly one interface. In this case this interface is bound to the type. If the type has no or several interfaces then no binding is added.
BindToSelf() Binds the type to itself.
BindSelection(ServiceSelector selector) Binds the selected interfaces to the type. (ServiceSelector is defined as delegate IEnumerable<Type> ServiceSelector(Type type, IEnumerable<Type> baseTypes)
BindUsingRegex(string pattern)
BindUsingRegex(string pattern, RegexOptions options)
Bind the interfaces of the current type matching the given regular expression to the type.
BindToFactory() This method is valid if the selected types are interfaces. In this case they are bound using ToFactory() extension method of the Ninject.Extensions.Factory extension.
BindToFactory(
     Func<IInstanceProvider>
          instanceProvider)
This method is valid if the selected types are interfaces. In this case they are bound using ToFactory(instanceProvider) extension method of the Ninject.Extensions.Factory extension.

Custom binding generators

In case none of the predefined binding generators fits your needs, you can define a custom one. This is a matter of implementing the IBindingGenerator interface:

/// 
/// Creates the bindings for a type using some convention.
/// 
public interface IBindingGenerator
{
    /// <summary>
    /// Creates the bindings for a type.
    /// </summary>
    /// <param name="type">The type for which the bindings are created.</param>
    /// <param name="bindingRoot">The binding root that is used to create the bindings.</param>
    /// <returns>The syntaxes for the created bindings to configure more options.</returns>
    IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot);
}

Now you can apply your binding generator using BindWith<T>() or BindWith(IBindingGenerator generator)

The predefined binding generators all take a IBindingCreator. In case the selection of one of the existing binding generators fits your need but you want to create the bindings differently you can implement this interface:

/// <summary>
/// Creates bindings.
/// </summary>
public interface IBindingCreator
{
    /// <summary>
    /// Creates the bindings for the specified services.
    /// </summary>
    /// <param name="bindingRoot">The binding root.</param>
    /// <param name="serviceTypes">The service types.</param>
    /// <param name="implementationType">The implementation type.</param>
    /// <returns>The syntax of the created bindings.</returns>
    IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(
        IBindingRoot bindingRoot,
        IEnumerable<Type> serviceTypes,
        Type implementationType);
}

Continue Reading: Configuring Bindings
Back: Projecting Components