c# - Ninject: GetAll instances that inherit from the same abstract class -
is possible ninject instances inherit specific abstract class? example, have following abstract class.
public abstract class myabstractclass { }
then have following 2 derived classes both inherit same abstract class.
public class myderivedclass1 : myabstractclass { } public class myderivedclass2 : myabstractclass { }
now going bind both derived classes kernel because want them in singleton scope.
_kernel = new standardkernel(); _kernel.bind<myderivedclass1>().toself().insingletonscope(); _kernel.bind<myderivedclass2>().toself().insingletonscope();
i know kernel can return me instance following.
_kernel.get<myderivedclass1>();
is there way of classes inherit myabstractclass? tried following without success.
ienumerable<myabstractclass> items = kernel.getall<myabstractclass>();
the hope above getall() method return list of 2 instances, 1 myderivedclass1 , second myderivedclass2.
instead of creating 2 bindings, second 1 "redirect" first, can is:
_kernel.bind<myabstractclass, myderivedclass1>() .to<myderivedclass1>().insingletonscope(); _kernel.bind<myabstractclass, myderivedclass2>() .to<myderivedclass1>().insingletonscope();
this expresses intention more clearly.
Comments
Post a Comment