配置在抽象类上定义的 Autofac 委托工厂 [英] Configuring an Autofac delegate factory that's defined on an abstract class

查看:28
本文介绍了配置在抽象类上定义的 Autofac 委托工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 C# 项目.我正在尝试摆脱具有大 switch 语句的 Factory 类.

I'm working on a C# project. I'm trying to get rid of a Factory class that has a large switch statement.

我想将 Autofac 配置为能够基于参数构建依赖项,从而允许 Autofac 代替 Factory.

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.

我查看了 Autofac wiki 的 DelegateFactories 页面,但是我不知道如何将模式应用于抽象类.下面是一些显示这种情况的代码:

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);
    }
}

如果我说new WidgetWrangler(context, WidgetType.Sprocket),我会喜欢它,它的Widget 属性将是一个SpocketWidget.

I'd like it if I were to say new WidgetWrangler(context, WidgetType.Sprocket), its Widget property would be a SpocketWidget.

当我尝试此操作时,我收到错误消息,指出 Widget.Factory 未注册.这种委托工厂模式是否不适用于抽象类,如果是,还有其他方法可以实现吗?

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?

推荐答案

您正在寻找的是 IIndex<,> 关系类型.

What you're looking for is the IIndex<,> Relationship Type.

如果您使用 .Keyed<>(...) 注册您的子类,您可以将注册键值 (object).

If you register your sub-classes with .Keyed<>(...) you can key a registration to a value (object).

例如:

builder.RegisterType<SprocketWidget>()
   .Keyed<Widget>(WidgetType.Sproket)
   .InstancePerDependency();

builder.RegisterType<WhizbangWidget>()
   .Keyed<Widget>(WidgetType.Whizbang)
   .InstancePerDependency();

那么你只需要依赖 IIndex 来模仿工厂行为.

Then you only require a dependency of IIndex<WidgetType,Widget> to mimic factory behaviour.

public class SomethingThatUsesWidgets
{    
    private readonly IIndex<WidgetType,Widget> _widgetFactory;
    public SomethingThatUsesWidgets(IIndex<WidgetType,Widget> widgetFactory)
    {
        if (widgetFactory == null) throw ArgumentNullException("widgetFactory");
        _widgetFactory = widgetFactory;
    }

    public void DoSomething()
    {
        // Simple usage:
        Widget widget = widgetFactory[WidgetType.Whizbang];

        // Safe Usage:
        Widget widget2 = null;
        if(widgetFactory.TryGetValue(WidgetType.Sprocket, out widget2))
        {
            // do stuff
        }
    }
}

这是使用依赖注入方法,如果您只想解析工厂:

That's using Dependency Injection approach, if you just want to resolve the factory:

var factory = Container.Resolve<IIndex<WidgetType,Widget>>();

这篇关于配置在抽象类上定义的 Autofac 委托工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆