Comments (1)
public static class KernelExtensions
{
/// <summary>
/// Binds an open generic type to its implementation and adds all its defined decorators
/// </summary>
/// <param name="kernel">Ninject Container</param>
/// <param name="openGenericType">Open generic Type</param>
/// <param name="assembly">Assembly to scan for the open generic type implementation</param>
/// <param name="decoratorTypes">Types of the decorators. Order matters. Order is from the most outer decorator to the inner decorator</param>
public static void BindManyOpenGenericsWithDecorators(this IKernel kernel, Type openGenericType, Assembly assembly, params Type[] decoratorTypes)
{
var allImplementations = GetAllTypesImplementingOpenGenericType(openGenericType, assembly);
foreach (var type in allImplementations.Where(type => !decoratorTypes.Contains(type)))
{
var genericInterface = type.GetInterfaces().FirstOrDefault(x => openGenericType.IsAssignableFrom(x.GetGenericTypeDefinition()));
// real implementation
var parentType = decoratorTypes.Last();
kernel.Bind(genericInterface).To(type)
.WhenInjectedInto(parentType);
}
for (var i = 0; i <= decoratorTypes.Count() - 1; i++)
{
var decoratorType = decoratorTypes[i];
if (i == 0)
{
// most outer decorator
kernel.Bind(openGenericType).To(decoratorType);
}
else
{
// inner decorators
var parentType = decoratorTypes[i - 1];
kernel.Bind(openGenericType).To(decoratorType)
.WhenInjectedInto(parentType);
}
}
}
private static IEnumerable<Type> GetAllTypesImplementingOpenGenericType(Type openGenericType, Assembly assembly)
{
return (from type in assembly.GetTypes()
from interfaceType in type.GetInterfaces()
let baseType = type.BaseType
where
(baseType != null && baseType.IsGenericType &&
openGenericType.IsAssignableFrom(baseType.GetGenericTypeDefinition())) ||
(interfaceType.IsGenericType &&
openGenericType.IsAssignableFrom(interfaceType.GetGenericTypeDefinition()))
select type);
}
}
from ninject.extensions.conventions.
Related Issues (20)
- "public interface" with "internal class" implementation not supported HOT 3
- BindBase does not bind abstract classes as expected HOT 1
- Where to find features like Scan HOT 4
- Value cannot be null.\r\nParameter name: first HOT 9
- Please consider making a portable version of this library HOT 1
- Get IAssemblyNameRetriever from Kernel instead of instanciating it
- Ninject.Extensions.Conventions preventing update Service Reference HOT 1
- Incorrect spelling of class IIncludingNonePublicTypesSyntax HOT 2
- GetNameWithoutGenericPart Throws SubString exception HOT 5
- Bindings not loaded into Module HOT 1
- Binding types that have multiple generic interfaces of the same type
- Bind to multiple interfaces, one of which is Inherited
- Bind Interfaces that are in Other Project / Assembly
- A none backwards compatible change was made to minor version of the Ninject.Extensions.Conventions
- Named bindings by convention
- Scan() code
- WhereTypeInheritsFrom shouldn't have side effect HOT 2
- 2.4 documentation? HOT 2
- Binding to interfaces and services in external projects is not working. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ninject.extensions.conventions.