This project is read-only.

Dependency Context Mapper

A context based dependency mapper for any* dependency injection framework.

Dependency injection is a great way to automate the creation of objects (behavior) in a system. It also allows to change the application behavior at run-time and that is exactly what the dependency context mapper does.

DCM creates a configurable layer between your interface(s) and implementations to return the required implementation based on the current situation. For example a user that requires a data repository based on an assigned role.


//// Registering services directly on the mapper. 
//// Wrappers for the services will be registered in the dependency injection container
//// Register a default implementation when no context or value has been mapped.

mapper.RegisterDefault<IArticleRepository, EmptyArticleRepository>();
mapper.RegisterDefault<IUserRepository, EmptyUserRepository>();

//// Register implementations per user role.
mapper.Register<IArticleRepository, AdministratorArticleRepository>(Role.Administrator);
mapper.Register<IArticleRepository, RestrictedArticleRepository>(Role.Editor);
mapper.Register<IArticleRepository, RestrictedArticleRepository>(Role.Reviewer);

//// Or the same, but fluently:
mapper.RegisterDefault<IArticleRepository, EmptyArticleRepository>();
mapper.RegisterDefault<IUserRepository, EmptyUserRepository>();

.Register<IArticleRepository, AdministratoArticleRepository>()
.Register<IUserRepository, AdministratorUserRepository>();

mapper.RegisterContext(Role.Editor, Role.Reviewer)
.Register<IArticleRepository, RestrictedArticleCollection>();

//// Retrieving an instance from the dependency injection container should work now:
var articles = container.GetInstance<IArticleRepository>();

When requesting an instance from your dependency container, the context mapper's intercepting registration will return the dependency based on the current context of the ContextProvider.


Currently, I consider this project as 'stable' without warranty. So far it has been successfully implemented in several production environments with SimpleInjector and with great result.

Pleas let me know if you have any questions.

*) Implement the IContainerAdaptor interface to support your framework of choice.


PM> Install-Package DependencyContextMapper


NuGet - Twitter - Blog

DCM is optimized for SimpleInjector, but not limited to. See the documentation for adapter examples.

Last edited Aug 9, 2014 at 9:39 PM by AppiePau, version 31