Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoexport all types by interface by specific key #205

Closed
mcdis opened this issue Mar 2, 2019 · 8 comments
Closed

Autoexport all types by interface by specific key #205

mcdis opened this issue Mar 2, 2019 · 8 comments
Milestone

Comments

@mcdis
Copy link

mcdis commented Mar 2, 2019

Hi!

Can you help me with this scenario...
I want to scan all types in specific assembly and export all types which impl specific interface...but I want to export by key which defined thru attribute. And I want to export with the specific lifestyle.

[Foo("a")]
class A: IExportInterface    {}

[Foo("b")]
class B: IExportInterface    {}

// ... after apply some grace magick ...
container.Locate<IExportInterface>(withKey: "a") // Yeah, It`a instance of A
container.Locate<IExportInterface>(withKey: "b") // Yeah, It`a instance of B

Some optimize and degree of freedom requirements:

  • export types only from specific assembly;
  • key provider delegate: string ResolveKey(Type _type)

ExportAssemblyContaining?

I can suggest some export method like:

_.ExportAssemblyContaining<IHofChangeProcessor>().ByKey(_type=>_type.GetTypeInfo().GetCustomAttribute<FooAttribute>().Name).Lifestyle.Singleton();

_.ExportAssemblyContaining<IHofChangeProcessor>(typeof(Anchor).Assembly).ByKey(_type=>_type.GetTypeInfo().GetCustomAttribute<FooAttribute>().Name).Lifestyle.Singleton();
@mcdis
Copy link
Author

mcdis commented Mar 2, 2019

Can(How) I use interface IExportKeyedTypeAttribute?

@mcdis
Copy link
Author

mcdis commented Mar 2, 2019

Tried this code and looks like it works...

container.Configure(_ =>
{
   _.ExportAssemblyContaining<IHofChangeProcessor>()
   .Where(_t => typeof(IHofChangeProcessor).IsAssignableFrom(_t))
   .ByInterface<IHofChangeProcessor>()
   .ExportAttributedTypes();
});
// ...
   [Singleton]
   [HofChangeProcessorType("create-competition")]
  public class CreateCompetitionHofProc : HofChangeProcessor<CreateCompetitionHofProcSettings> {}
// ..
   public class HofChangeProcessorTypeAttribute : Attribute, IExportKeyedTypeAttribute
   {
      public HofChangeProcessorTypeAttribute(string _type)
      {
         Type = _type;
      }
      public string Type { get; }
      public Tuple<Type, object> ProvideKey(Type _attributedType)
      {
         return new Tuple<Type, object>(typeof(IHofChangeProcessor), _attributedType.GetTypeInfo().GetCustomAttribute<HofChangeProcessorTypeAttribute>().Type.ToLower());
      }
   }

@ipjohnson
Copy link
Owner

Hi @mcdis

I’ve been away for the weekend but I’ll take a look this evening and put something together.

@mcdis
Copy link
Author

mcdis commented Mar 4, 2019

I feel that there are some excessive descriptions.
I think that add ability to provide key provider factory method during export is good choice.
And we can combine type that identify assembly with export type.

_scope
.ExportAssemblyContaining<IdentifyAssemblyTypeD,IdentifyAssemblyTypeE,IdentifyAssemblyTypeF>() // multiply assemblies
.ByInterface<IFoo>() // .Where(_t => typeof(IFoo).IsAssignableFrom(_t)).ByInterface<IFoo>()
.ByKey(_type=>SelectKey(_type)) // factory selection method
.Lifestyle.Singleton()

Or some extra method
OfInterface<IFoo>() ~ .Where(_t => typeof(IFoo).IsAssignableFrom(_t)).ByInterface<IFoo>()

@ipjohnson
Copy link
Owner

Hi @mcdis

I hesitate to add an ExportAssemblyContaining<T, T2, T3>() , I'd be open to something like ExportAssmbliesContaining(params Type[] types) that way there is only one new extension to cover all cases.

There is already a ByKeyedTypes method that lets you export a set by keyed types. I think an extension method like this would be what you want.

        public static IExportTypeSetConfiguration ByKeyed<T>(this IExportTypeSetConfiguration configuration, Func<Type, object> keyFunc)
        {
            configuration.ByKeyedTypes(type =>
            {
                if (typeof(T).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
                {
                    var key = keyFunc(type);

                    if(key != null)
                    {
                        return new[] { new Tuple<Type, object>(type, key) }; 
                    }

                    return null;
                }

                return null;
            });

            return configuration;
        }

@mcdis
Copy link
Author

mcdis commented Mar 5, 2019

Hi @ipjohnson,
That extension looks good!

@ipjohnson
Copy link
Owner

I'll look at putting this into a preview release in the next week. Did you want the extension ExportAssmbliesContaining(typeof(ClassA), typeof(ClassB))?

ipjohnson added a commit that referenced this issue Mar 20, 2019
@ipjohnson ipjohnson added this to the 7.0 milestone Apr 23, 2019
@ipjohnson
Copy link
Owner

I'm going to close this out as it will be released in 7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants