Skip to content

Autoexport all types by interface by specific key #205

Closed

Description

@mcdis

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

Activity

  1. mcdis commented on Mar 2, 2019

    @mcdis
    Author

    Can(How) I use interface IExportKeyedTypeAttribute?

  2. mcdis commented on Mar 2, 2019

    @mcdis
    Author

    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());
          }
       }
    
    
  3. ipjohnson commented on Mar 3, 2019

    @ipjohnson
    Owner

    Hi @mcdis

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

  4. mcdis commented on Mar 4, 2019

    @mcdis
    Author

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

  5. ipjohnson commented on Mar 4, 2019

    @ipjohnson
    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;
            }
  6. mcdis commented on Mar 5, 2019

    @mcdis
    Author

    Hi @ipjohnson,
    That extension looks good!

  7. ipjohnson commented on Mar 7, 2019

    @ipjohnson
    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))?

  8. added a commit that references this issue on Mar 20, 2019
  9. added this to the 7.0 milestone on Apr 23, 2019
  10. ipjohnson commented on Apr 23, 2019

    @ipjohnson
    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions