Chris Payne
Chris Payne

Reputation: 1710

How can I use the decorator pattern with Autofac if I only have XML configuration available to me?

I have a specific implementation of a service that I can only register via an Autofac XML configuration file (http://autofac.readthedocs.org/en/latest/configuration/xml.html).

I need to decorate this implementation in order to add some new functionality, but I can't seem to find a way to achieve this.

Normally, I'd do this in an Autofac module like so:

    builder.RegisterType<AzureBlobShellSettingsManager>().Named<IShellSettingsManager>("implementation");

    builder.RegisterType<DecoratorShellSettingsManager>().As<IShellSettingsManager>().WithParameter(
                (p, c) => p.ParameterType == typeof(IShellSettingsManager),
                (p, c) => c.ResolveNamed<IShellSettingsManager>("implementation"));

Is it possible to get the same result with an XML configuration? And if so, how?

Upvotes: 0

Views: 160

Answers (1)

Travis Illig
Travis Illig

Reputation: 23894

Autofac configuration support doesn't include the depth of functionality you need to do this entirely via configuration. The configuration model was never intended to be 100% feature-equivalent with code-based registrations.

One potential workaround might be to create a module that registers the things you need and then register the module using configuration. The complex part that configuration doesn't support could be code, but the easy switch in config would still be there. And modules can take parameters, so if you need to pass a type or something like that to the module, you could do it by type name and do Type.GetType in the module to convert it back.

Upvotes: 1

Related Questions