配置在抽象类上定义的 Autofac 委托工厂
我正在开发一个 C# 项目。我正在尝试摆脱具有大型 switch
语句的 Factory 类。
我希望将 Autofac 配置为能够基于参数构建依赖关系,从而允许 Autofac 取代 Factory。
我查看了 Autofac wiki 的 DelegateFactories 页面,但我不能弄清楚如何将模式应用到抽象类。下面是一些显示这种情况的代码:
public enum WidgetType
{
Sprocket,
Whizbang
}
public class SprocketWidget : Widget
{
}
public class WhizbangWidget : Widget
{
}
public abstract class Widget
{
public delegate Widget Factory(WidgetType widgetType);
}
public class WidgetWrangler
{
public Widget Widget { get; private set; }
public WidgetWrangler(IComponentContext context, WidgetType widgetType)
{
var widgetFactory = context.Resolve<Widget.Factory>();
Widget = widgetFactory(widgetType);
}
}
如果我说 new WidgetWrangler(context, WidgetType.Sprocket)
,它的 Widget
属性将是 SpocketWidget
。
当我尝试此操作时,出现错误,指出 Widget.Factory
未注册。此委托工厂模式是否不适用于抽象类,如果是,是否有其他方法可以实现此目的?
I'm working on a C# project. I'm trying to get rid of a Factory class that has a large switch
statement.
I want to configure Autofac to be able to construct a dependency based on a parameter, thereby allowing Autofac to take the place of the Factory.
I've looked at the DelegateFactories page of the Autofac wiki, but I can't figure out how to apply the pattern to an abstract class. Here's some code showing the situation:
public enum WidgetType
{
Sprocket,
Whizbang
}
public class SprocketWidget : Widget
{
}
public class WhizbangWidget : Widget
{
}
public abstract class Widget
{
public delegate Widget Factory(WidgetType widgetType);
}
public class WidgetWrangler
{
public Widget Widget { get; private set; }
public WidgetWrangler(IComponentContext context, WidgetType widgetType)
{
var widgetFactory = context.Resolve<Widget.Factory>();
Widget = widgetFactory(widgetType);
}
}
I'd like it if I were to say new WidgetWrangler(context, WidgetType.Sprocket)
, its Widget
property would be a SpocketWidget
.
When I try this, I get errors stating that Widget.Factory
is not registered. Does this delegate factory pattern not work with abstract classes, and if so, is there another way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的是
IIndex<,>
关系类型。如果您使用
.Keyed<>(...)
注册您的子类,您可以将注册设置为值 (object
)。例如:
那么您只需要
IIndex
的依赖项来模仿工厂行为。如果您只想解析工厂,那就使用依赖注入方法:
What you're looking for is the
IIndex<,>
Relationship Type.If you register your sub-classes with
.Keyed<>(...)
you can key a registration to a value (object
).For example:
Then you only require a dependency of
IIndex<WidgetType,Widget>
to mimic factory behaviour.That's using Dependency Injection approach, if you just want to resolve the factory: