【问题标题】:Automatic factory generators for constructor DI构造函数 DI 的自动工厂生成器
【发布时间】:2015-09-03 08:23:04
【问题描述】:

我想在不传递任何“内核”容器的情况下使用基于工厂的依赖注入,因此如果没有从“顶部”显式传递其依赖项,就不可能实例化一个类。

执行此操作的手动方式需要在引导程序中使用如下代码:

    static void Main(string[] args)
    {
        // simplified example, can require classes in reality
        ABFactory abFactory = (data1) => new AB(data1);
        ACAFactory acaFactory = (data1) => new ACA(data1);
        ACFactory acFactory = (x) => new AC(x, acaFactory);
        IA a = new A(1, new AA(1, new AAA(), new AAB()), abFactory, acFactory);
        a.Action(123);
    }

当工厂定义为

delegate IAB ABFactory(string data1);
delegate IAC ACFactory(int x);
delegate IACA ACAFactory(int data1);

有什么可以让工厂建设更容易甚至自动化吗?支持不同的工厂类型(池、ThreadLocal 缓存等)?

更新

一些真实的代码示例:

public interface IItemSetSpawnController
{
    void TransitSpawned();
}

public class ItemSetSpawnController : IItemSetSpawnController
{
    readonly GameMap.ItemSet _set;
    readonly LootableFactoryDelegate _lootableFactory;
    readonly IFiber _fiber;

    readonly int _defaultRespawnTime;

    public ItemSetSpawnController([NotNull] GameMap.ItemSet set, int defaultRespawnTime, [NotNull] LootableFactoryDelegate lootableFactory, IFiber fiber)
    {
        if (set == null) throw new ArgumentNullException(nameof(set));
        if (lootableFactory == null) throw new ArgumentNullException(nameof(lootableFactory));
        if (set.Items.Count == 0) throw new ArgumentException("Empty set", nameof(set));
        _set = set;
        _lootableFactory = lootableFactory;
        _fiber = fiber;
        _defaultRespawnTime = defaultRespawnTime;
    }

    public void TransitSpawned()
    {
        // Fiber.Schedule for respawning
    }
}

public delegate IItemSetSpawnController ItemSetSpawnControllerFactory(
    [NotNull] GameMap.ItemSet set, int defaultRespawnTime, [NotNull] LootableFactoryDelegate lootableFactory, IFiber fiber);


protected virtual void AddMapLootables()
{
    foreach (var set in ItemSets)
    {
        if (set.Items.Count == 0) continue;
        var c = ItemSetSpawnControllerFactory(
            set,
            Settings.LootRespawnTime,
            LootableFactory,
            ExecutionFiber);
        c.TransitSpawned();
    }

}

【问题讨论】:

标签: c# dependency-injection factory factory-pattern reflection.emit


【解决方案1】:

在我看来,您的组件是使用运行时数据初始化的。这是bad practice,因为这会使你的DI配置复杂很多,我认为你的情况就是这样。

不要使用工厂来初始化组件,其中组件的构造函数同时依赖于服务依赖项和运行时数据,而是将运行时数据移出构造函数并在运行时将其提供给组件。您可以通过组件的方法传递运行时值来做到这一点,也可以将服务注入到组件中,允许组件在运行时(初始化之后)请求该值。

选择哪个选项,取决于运行时值的使用是否是实现细节。如果它是抽象的重要部分,则该值应该是抽象方法签名的一部分。如果它与抽象无关,并且只有组件本身应该知道它,那么注入允许访问此运行时值的服务是最明显的解决方案。

更新

如果您将运行时数据移出构造函数,您将无需使用工厂作为额外的抽象层,并且您可以将代码更改为以下内容:

public interface IItemSetSpawnController {
    void TransitSpawned(GameMap.ItemSet set);
}

public class ItemSetSpawnController : IItemSetSpawnController {
    readonly LootableFactoryDelegate _lootableFactory;
    readonly IFiber _fiber;
    readonly ISessings settings;
    public ItemSetSpawnController(ISessings settings,
        [NotNull] LootableFactoryDelegate lootableFactory, IFiber fiber) {
        _lootableFactory = lootableFactory;
        _fiber = fiber;
        _settings = settings;
    }
    public void TransitSpawned([NotNull] GameMap.ItemSet set) {
        if (set == null) throw new ArgumentNullException(nameof(set));
        // If LootRespawnTime is a config value that doesn't change after startup, you
        // can more it back into the constructor of the controller.
        int defaultRespawnTime = _settings.LootRespawnTime;
        // Fiber.Schedule for respawning
    }
}

class MapLooter {
    IItemSetSpawnController controller;
    public MapLooter(IItemSetSpawnController controller){
        this.controller = controller;
    }
    public void AddMapLootables() {
        foreach (var set in ItemSets) {
            if (set.Items.Count == 0) continue;
            this.controller.TransitSpawned(set);
        }
    }
}

【讨论】:

  • 是的,这是一种常用的方法——将上下文数据传递给构造函数。实际上,如果一个类在没有上下文的情况下被实例化,那么它应该是单例的。我了解您建议将此类上下文初始化代码移动到包含在接口中的单独方法中,但对我来说这看起来有点奇怪...
  • @Vlad:如果您更新您的问题并使您的代码更具体(使用您尝试创建的抽象和组件的实际名称),我可以举一个具体的例子来说明如何改进您的设计。
  • 很抱歉,如果我的代码不清楚,但是.. ItemSetSpawnController 的目的是包含一个集合的重生逻辑(带有计时器,订阅拾取事件)。因此它应该为每个 ItemSet 构造。
  • @Vlad:它应该被重新创建,或者你喜欢它被重新创建?根据我的经验,最好让应用程序组件保持无状态。这导致代码更简单,对象图更容易验证。
  • 它不是一次性对象。正如我所说,它在游戏生命周期内维护单个 ItemSet 的重生逻辑(游戏也是为服务器上的每个游戏房间实例化的运行时对象)。在我的实践中,处理附加到上下文类比使用一个实例来管理一组对象要好。例如。它允许对某些 ItemSets 进行非标准的重生实现。我的应用程序中的几乎所有东西都是上下文附加的运行时对象(甚至每个游戏实例的设置都可能不同),除了一些单例。
猜你喜欢
  • 1970-01-01
  • 2011-08-21
  • 2012-03-08
  • 1970-01-01
  • 2016-12-07
  • 2012-01-31
  • 2012-01-07
  • 2019-09-25
  • 1970-01-01
相关资源
最近更新 更多