NInject equivalent of Autofac AsClosedTypesOf

What is the NInject equivalent of the following code that uses Autofac:

var builder = new ContainerBuilder(); System.Reflection.Assembly assembly = ...; builder.RegisterAssemblyTypes(assembly).AsClosedTypesOf(typeof(OpenGeneric<>)) .As<IAnInterface>(); var resolved = container.Resolve<IEnumerable<IAnInterface>>(); 
+4
source share
1 answer

Using Ninject 3.0.0-rc3, you can use

 kernel.Bind( x => x.FromThisAssembly() .SelectAllClasses().InheritedFrom(typeof(BaseService<>)).WhichAreGeneric() .BindToAllInterfaces()); 

Depending on your requirements, you can remove the WhichAreGeneric operator. .SelectAllClasses().InheritedFrom(typeof(BaseService<>)).WhichAreGeneric() selects the classes for which the binding is being created.

Conventions ensure that the interface and implementation class must have the same open type arguments. For instance. When

 interface IBar<T1, T2> interface IBaz<T> interface IFoo class Bar<T1, T2> : IBar<T1, T2>, IBaz<T1>, IFoo class Foo : IBar<int, int>, IFoo 

IBar<T1, T2> is the only valid interface for Bar<T1, T2> . But for Foo, both IBar<int, int>, IFoo valid.

+2
source

Source: https://habr.com/ru/post/1391881/


All Articles