Parameterized factory thesis in Autofac

I want to be able to register a factory component in Autofac with the following properties:

  • factory must accept a parameter (in this case, a string) and return an instance of the component that is specifically associated with this string
  • Component Lifetime Must Be InstancePerLifetimeScope
  • Implication No. 2 is that the second factory call with a specific parameter value returns the same component instance within its service life.

I was able to achieve this by writing code that acts as a β€œname registry” within the scope, while maintaining a dictionary of the names and instances of the components that were created. This works, but I suspect there might be a way to do this using some autofac registry magic, but I have not tried to crack it yet. Any ideas?

Thank!

+3
source share
1 answer

Looks like you want Autofac Named and Keyed Services .

+2
source

All Articles