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

时间:2015-05-24 21:19:34

标签: c# autofac

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?

1 个答案:

答案 0 :(得分:1)

Autofac配置支持不包括完全通过配置完成此操作所需的功能深度。配置模型从未打算与基于代码的注册100%功能等效。

一个可能的解决方法可能是创建一个模块来注册您需要的东西,然后使用配置注册模块。配置不支持的复杂部分可能是代码,但配置中的简单切换仍然存在。模块可以接受参数,因此如果您需要将类型或类似的类型传递给模块,您可以按类型名称进行操作,并在模块中执行Type.GetType以将其转换回来。

相关问题