trquth / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

Allow constructor dependencies to be resolved with keyed services #441

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Similar to [WithMetadata] but for keys used when registering services. Allows 
this:

public SolutionExplorer([Keyed("Solution")] Enumerable<IAdapter>
adapters)

This is a bit more straightforward and simplified than with metadata, since the 
registration with keyed data can be done in a scanning fashion too, like so:

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
    .Where(t => t.GetCustomAttribute<ComponentAttribute>() != null)
    .As(t => new KeyedService(t.GetCustomAttribute<ComponentAttribute>().Key, t.GetCustomAttribute<ComponentAttribute>().Type));

My commits to follow soon :)

Original issue reported on code.google.com by dan...@cazzulino.com on 6 Jun 2013 at 4:53

GoogleCodeExporter commented 8 years ago
Here's the commit: 
https://code.google.com/r/daniel-keyedattribute/source/detail?r=a5aff121211b54bb
d0b92b3d3c2d0fe626d84cc1

Original comment by dan...@cazzulino.com on 6 Jun 2013 at 5:29

GoogleCodeExporter commented 8 years ago
I'll see about getting this in shortly.

Something I'm thinking about - if there was an abstract base attribute for 
these parameter markings, you could use a single extension method to apply all 
the constructor parameter filtering (keyed and metadata) all in one call.

builder.RegisterType<Foo>().WithAttributeFilter();

The attributes could be passed in the lambda with the parameter info and 
context and they would individually be responsible for doing the resolution 
work. That would also make it so you could add more of these sorts of 
attributes easier without adding new extensions - just new attributes.

Original comment by travis.illig on 7 Jun 2013 at 3:09

GoogleCodeExporter commented 8 years ago
YES, totally cool way to improve it!

Original comment by dan...@cazzulino.com on 7 Jun 2013 at 4:44

GoogleCodeExporter commented 8 years ago
I have it working with the mentioned refactor and it's pretty straightforward 
to add new filters as needed. Will commit shortly.

Original comment by travis.illig on 7 Jun 2013 at 5:48

GoogleCodeExporter commented 8 years ago
This issue was closed by revision da1c220fbeb8.

Original comment by travis.illig on 7 Jun 2013 at 6:07

GoogleCodeExporter commented 8 years ago
I added the functionality but did not update the version on the 
assembly/package since we haven't released since last version update. The same 
semantic version change would apply to this new, backwards-compatible 
functionality.

Original comment by travis.illig on 7 Jun 2013 at 6:08

GoogleCodeExporter commented 8 years ago
Looks great :)

Original comment by dan...@cazzulino.com on 7 Jun 2013 at 6:35