kanayabhattad / autofac

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

Allow constructor dependencies to be filtered by metadata #440

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It's extremely handy to be able to leverage the metadata support in Autofac to 
filter dependencies, like:

public Manager([WithMetadata("LoggerName", "Manager")] ILogger logger)

If many loggers are registered with the LoggerName metadata, the consumer can 
simply specify the filter as an attribute to the constructor parameter. The 
same thing can be done for enumerable:

            public SolutionExplorer(
                [WithMetadata("Target", "Solution")] IEnumerable<IAdapter> adapters, 
                [WithMetadata("LoggerName", "Solution")] ILogger logger)
            {
                this.Adapters = adapters.ToList();
                this.Logger = logger;
            }

This probably belongs to the Extras.Attributed assembly, since it leverages 
metadata attributes as well as attributed annotations. 

A pull request will follow soon.

Original issue reported on code.google.com by dan...@cazzulino.com on 5 Jun 2013 at 5:22

GoogleCodeExporter commented 8 years ago
Here's the commit for this: 
https://code.google.com/r/daniel-withmetadata/source/detail?r=4818699a15fe66adad
d8da492ddef52aeb10214e

Not sure how to send a pull request using Mercurial :(. Is there such a thing 
even?

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

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 6 Jun 2013 at 2:55

GoogleCodeExporter commented 8 years ago
I'm going to bring this in pretty much as-is because it's cool and totally 
useful.

Something I'm noticing, though, is that it won't necessarily support all of the 
relationship types like Lazy<T>, Func<T>, and so on. I don't think it's a 
showstopper by any means, but it might be a sort of edge-case-gotcha if people 
really grab on and like it.

We can always add that support later if it becomes of interest to people. :)

Original comment by travis.illig on 6 Jun 2013 at 3:28

GoogleCodeExporter commented 8 years ago
I'm adding a small update to the logic such that the filter will only actually 
activate components that satisfy the metadata requirements. That way if 
activation is expensive or performs some action on the system, you're only 
incurring the cost for stuff you're using.

Original comment by travis.illig on 6 Jun 2013 at 3:46

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

Original comment by travis.illig on 6 Jun 2013 at 3:50

GoogleCodeExporter commented 8 years ago
Hi Travis, 
yes, I think the GetElementType method should be smarter and reusable outside 
this class. I'm finding another use for it:

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

Where I need to do the exact same calculation on the parameter type to 
determine the element type if it's an enumerable :)

Original comment by dan...@cazzulino.com on 6 Jun 2013 at 4:30