Skip to content

Projecting Components

ChrisFindlay edited this page Aug 16, 2013 · 6 revisions

The second step is to select the components for which you want to create a binding. This can be compared to the To<> syntaxes of the single component binding syntax. This step consists of two sub steps.

1. Selecting Component Types

First you have to pick a base set of Types which are to be bound To<>:

Method Description
SelectAllClasses() This is the method that is used in almost every case. It selects all non-abstract classes.
SelectAllIncludingAbstractClasses() This method selects all classes including abstract ones.
SelectAllAbstractClasses() This method selects just abstract classes.
SelectAllInterfaces() This method selects all interfaces.
SelectAllTypes() Selects all types (classes, interfaces, structs, enums, primitive types).
Select(Func<Type, bool> filter) If none of the previous methods are appropriate for your needs, this method can be used to specify exactly which types you are interested in. The filter function is called for every type. To include a Type, one returns true. To ignore return false.

Notes: The projectors SelectAllIncludingAbstractClasses, SelectAllAbstractClasses and SelectAllInterfaces only make sense in combination with a binding generator that generates the implementation on the fly (e.g. BindToFactory); as with normal bindings, one can't Bind a service Type to an abstract class or interface implementation.

2. Filtering the projected Component Types

The second step is to filter the component Types projected in the previous step by chaining to one of the following methods:

Method Description
InNamespaces(IEnumerable<string> namespaces)
InNamespaces(params string[] namespaces)
Selects the types in the specified namespaces.
InNamespaceOf<T>()
InNamespaceOf(params Type[] types)
Selects the types in the same namespace as the given types.
NotInNamespaces(IEnumerable<string> namespaces)
NotInNamespaces(params string[] namespaces)
Selects all types except those that are in one of the given namespaces.
NotInNamespaceOf<T>()
NotInNamespaceOf(params Type[] types)
Selects all types not in same namespaces as the given type.
InheritedFromAny(IEnumerable<Type> types)
InheritedFromAny(params Type[] types)
InheritedFrom<T>()
Selects the types that are inherited from any of the given types.
WithAttribute<T>()
WithAttribute(Type attributeType)
WithAttribute<T>(Func<T, bool> predicate)
Selects the types that have the specified attribute. In case a predicate is pecified the types are filtered additionally using the given predicate.
WithoutAttribute<T>()
WithoutAttribute(Type attributeType)
WithoutAttribute<T>(Func<T, bool> predicate)
Selects the types that do not have the specified attribute. In case a predicate is pecified the types are filtered additionally using the given predicate.
WhichAreGeneric() Selects all generic types.
WhichAreNotGeneric() Selects all none generic types.
StartingWith(string prefix) Selects types with the given prefix in their name.
EndingWith(string suffix) Selects types with the given postfix in their name.

3. Including and Excluding types explicitly

Once you have selected the components you want to create bindings for you have the option to include or exclude some types that don't match the general definition explicitly. This is applied by using one of the following methods:

Method Description
Including<T>()
Including(params Type[] types)
Including(IEnumerable<Type> types)
Includes the specified type.
Excluding<T>()
Excluding(params Type[] types)
Excluding(IEnumerable<Type> types)
Excludes the specified types.

Including non public types

If you want that bindings for non public types are created then you have to specify this for the convention before secelting the types using IncludingNonePublicTypes() like this:

x.FromThisAssembly().IncludingNonePublicTypes().SelectAllClasses()

Continue Reading: Projecting Services to Bind
Back: Projecting Assemblies