Skip to content
Portable component composition annotations, dependency injection framework agnostic.
C# Batchfile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CommonComposition.Autofac Remove the CommonComposition dependency from the DI container packages Aug 14, 2014
CommonComposition.Mef Remove the CommonComposition dependency from the DI container packages Aug 14, 2014
CommonComposition.Microsoft.Tests Made content files match the target project root namespace. Dec 2, 2013
CommonComposition.Microsoft
CommonComposition.Ninject Remove the CommonComposition dependency from the DI container packages Aug 14, 2014
CommonComposition.Tests Made content files match the target project root namespace. Dec 2, 2013
CommonComposition.Unity Remove the CommonComposition dependency from the DI container packages Aug 14, 2014
CommonComposition.Windsor Remove the CommonComposition dependency from the DI container packages Aug 14, 2014
CommonComposition Added more resolutions for the project icon Jan 11, 2014
build Added push capabilities to the build script. Dec 3, 2013
icon Added more resolutions for the project icon Jan 11, 2014
.gitattributes Initial version of basic conventional registration of all components … Sep 26, 2013
.gitignore Added .nuget to the ignored folders list, since it's automatically do… Dec 2, 2013
ClariusLabs.snk Updated release notes, moved global composition to a root location. Sep 26, 2013
CommonComposition.props Created nuget specs for all supported containers. Sep 26, 2013
CommonComposition.sln Added push capabilities to the build script. Dec 3, 2013
CommonComposition.targets Added full documentation to all APIs. Dec 2, 2013
CommonComposition.tasks Removed nuget binary, and added automatic download and restore of pac… Dec 2, 2013
GlobalAssemblyInfo.cs Added push capabilities to the build script. Dec 3, 2013
LICENSE Initial commit Sep 25, 2013
MyGet.bat Updated build script Nov 28, 2013
README.md Fixing typo in readme Sep 9, 2015

README.md

Icon Common Composition

Portable component composition annotations, dependency injection framework agnostic.

Why

Typically, application logic doesn't require deep knowledge of specific dependency injection framework features, APIs and extensibility points. It can even be considered a best practice to keep your logic code devoid of specific DI frameworks types and quirks.

Most frameworks today allow for convention-based registration of components (like "register all concrete types with their implemented interfaces"). A typical annotation that's usually needed is whether the registered component should be instantiated only once in a container and reused (like a container-scoped singleton) or if a new instance should be created every time the component is requested.

It would be clearly desirable to be able to express that type of annotation (as well as whether a type should be registered for composition at all) in a framework agnostic way. That's precisely what Common Composition is for.

The Common Composition provides basic annotation attributes you need to specify how components should be composed in a dependency injection container. It allows your application logic to remain DI framework agnostic while still leveraging the benefits of convention-based configuration.

In addition, Common Composition will provide a set of default configurations for various containers so that the behave consistently with regards to some common relationship types, such as dependencies on Func factories, IEnumerable, etc.

What about Common Service Locator?

The Common Service Locator project also provides an abstraction over IoC containers, but its intended consumers are other frameworks, not application code. So much that a Service Locator is considered by most to be an anti-pattern.

Your application code should clearly NOT depend on that abstraction.

What

The goal is to keep the annotations you make on your application logic to a minimum. Currently, you can annotate your types that should be registered for composition with the [ComponentAttribute]:

[Component(IsSingleton = false)]
public class Foo : IFoo
{
    public Foo(IBar bar)
    {
        this.Bar = bar;
    }

    public IBar Bar { get; private set; }
}

[Component(IsSingleton = true)]
public class Bar : IBar { } 

public interface IComponent { }
public interface IFoo : IComponent { }
public interface IBar : IComponent { }

Under Common Composition conventions, all major dependency injection frameworks (Autofac, Ninject, Windsor, Unity and MEF) will make both Foo and Bar available (note Bar is an internal class), and resolving all IComponent instances will shield both Foo and Bar. Also, resolving twice the Bar/IBar component will return the same instance, since it's configured to be a singleton.

A common set of unit tests that run and are required to pass on all supported containers, ensures this common set of behaviors.

Here's how to leverage Common Composition on each of the supported frameworks:

  1. Autofac:

         var builder = new ContainerBuilder();
         builder.RegisterComponents(typeof(IFoo).Assembly);
    
         var container = builder.Build();
    
  2. Ninject:

         var kernel = new Ninject.StandardKernel();
    
         kernel.RegisterComponents(typeof(IFoo).Assembly);
    
  3. Windsor:

         var container = new WindsorContainer();
    
         container.RegisterComponents(typeof(IFoo).Assembly);
    
  4. Unity:

         var container = new UnityContainer();
    
         container.RegisterComponents(typeof(IFoo).Assembly);
    
  5. MEF:

         var catalog = new ComponentCatalog(typeof(IFoo).Assembly);
         var container = new CompositionContainer(catalog);
    
  6. Microsoft.Composition (MEF for Windows Store / NET45:

         var configuration = new ContainerConfiguration();
         configuration.RegisterComponents(typeof(IFoo).Assembly);
    
         var container = configuration.CreateContainer();
    

Install

On your application logic projects:

        install-package CommonComposition

On your application bootstrapping code (i.e. your Main(), or web application startup procedure, etc.), depending on the chosen container implementation:

        install-package CommonComposition.Autofac
        install-package CommonComposition.Ninject
        install-package CommonComposition.Windsor
        install-package CommonComposition.Unity
        install-package CommonComposition.Mef
You can’t perform that action at this time.