Grace
Grace copied to clipboard
Exporting classes that inherit from an abstract base class fails
I'm trying to export a series of classes, all inheriting from a mutual abstract class:
Container.Configure(_ =>
{
_.Export(Assembly.GetAssembly(typeof(AbstractBaseClass)).GetTypes()).BasedOn<AbstractBaseClass>().Lifestyle.Singleton();
});
When locating these classes:
var childClassses = Startup.Container.LocateAll<AbstractBaseClass>();
I get an empty array in return. What figures? Obviously, the abstract class itself should be filtered out (similar to an interface), but that's probably already done automatically.
Bonus question: Does the Lifestyle work individually on each class? If I later choose to only locate e.g. ChildClass
, will it return the same singleton?
BasedOn<T>
acts as a type filter but doesn't dictate how the type will be exported. I think you should be able to add a ByTypes()
call and it will specify which types you want to export by. It's a little clumsy but I think it should work.
_.Export(Assembly.GetAssembly(typeof(AbstractBaseClass)).GetTypes()).
BasedOn<AbstractBaseClass>().
ByTypes(t => new []{typeof(AbstractBaseClass)}).Lifestyle.Singleton();
Ok, I see thanks for the suggestion. Perhaps some convenience methods would be good to have here? Also it'd be nice to finally get those wrapper methods from Grace.Net
implemented. This code could peharps then be rewritten as:
_.Export(Types.AssemblyFromDefinedType<AbstractBaseClass>().GetTypes())
.IsSubClassOf<AbstractBaseClass>().Lifestyle.Singleton
With the way .net 5 is going I agree with you that bring back the code from Grace.Net makes sense. Though it might make most sense to just stick it in the main library.
Yea that's kind of what I meant, import them all into the main Grace library :)