Grace icon indicating copy to clipboard operation
Grace copied to clipboard

Exporting classes that inherit from an abstract base class fails

Open silkfire opened this issue 4 years ago • 4 comments

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?

silkfire avatar May 10 '20 00:05 silkfire

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();

ipjohnson avatar May 11 '20 11:05 ipjohnson

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

silkfire avatar May 11 '20 13:05 silkfire

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.

ipjohnson avatar May 11 '20 22:05 ipjohnson

Yea that's kind of what I meant, import them all into the main Grace library :)

silkfire avatar May 12 '20 00:05 silkfire