本文介绍了依赖注入用户输入的最佳策略是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了大量的依赖注入,但是我想在运行时获取用户处理信息的输入。

I've used a fair amount of dependency injection, but I'd like to get input on how to handle information from the user at runtime.

我有一个连接到com端口的类。我允许用户选择COM端口号。现在我有一个COM端口参数作为一个构造函数参数。推理是该类无法在没有该信息的情况下运行,并且具体实现(该类的模拟版本不需要com端口)。

I have a class that connects to a com port. I allow the user to select the com port number. Right now, I have that com port parameter as a constructor argument. The reasoning being that the class cannot function without that information, and it's implementation specific (a mock version of this class wouldn't need a com port).

替代方法是具有启动方法,该方法占用com端口,或具有设置com端口的属性。这使得它与IoC容器非常兼容,但从类的角度来看,这并不一定是有意义的。

The alternative is to have a "Start" method that takes in the com port, or have a property that sets the com port. This makes it very compatible with an IoC container, but it doesn't necessarily make sense from the perspective of the class.

似乎逻辑路由与依赖关系注入设计,但这是因为我的UI正在获取特定类型的类的信息。

It seems like the logical route conflicts with the dependency injection design, but it's because my UI is getting information for a specific type of class.

其他替代方案将包括使用IoC容器,让我传递额外的构造函数参数,或者只是在顶级构建我需要的类,而不使用依赖注入。

Other alternatives would include using an IoC container that lets me pass in additional constructor parameters, or just constructing the classes I need at the top level without using dependency injection.

这种类型的问题有普遍接受的标准模式吗?

Is there a generally accepted standard pattern for this type of problem?

推荐答案

根据您的需要,您可以使用两条路线。

There are two routes you can take, depending on your needs.

1。将UI直接连接到您的具体课程

这是最简单的选项,但很多次完全可以接受。虽然您可能拥有许多接口和使用DI的域模型,但UI构成了对象图形的组合根,您可以简单地连接您的具体类,包括您所需的端口号参数。

This is the simplest option, but many times perfectly acceptable. While you may have a Domain Model with lots of interfaces and use of DI, the UI constitutes the Composition Root of the object graphs, and you could simply wire up your concrete class here, including your requrired port number parameter.

这种方法很简单易懂,并且易于实现。

The upside is that this approach is simple and easy to understand and implement.

缺点是您的灵活性降低。您将无法用另一个方法任意替换一个实现(但是再次,您可能不需要这种灵活性)。

The downside is that you get less flexibility. You will not be able to arbitrarily replace one implementation with another (but then again, you may not need that flexibility).

即使UI被锁定到具体的实现中,这并不意味着域模型本身不会在其他应用程序中重复使用。

Even with the UI locked to a concrete implementation, this doesn't mean that the Domain Model itself wouldn't be reusable in other applications.

2。添加抽象工厂

另一个选项是添加另一个间接层。而不是让你的UI直接创建类,它可以使用抽象工厂来创建实例。

The other option is to add another layer of indirection. Instead of having your UI create the class directly, it could use an Abstract Factory to create the instance.

工厂的Create方法可以将端口号作为输入,这个抽象在UI子层中最好。

The factory's Create method could take the port number as an input, so this abstraction belongs best in a UI sub-layer.

public abstract class MyFactory
{
    public abstract IMyInterface Create(int portNumber);
}

然后,您可以将您的DI容器连接到使用该工厂的实施端口号,并将其作为构造函数参数传递给您的实际实现。其他工厂实现可能会忽略该参数。

You could then have your DI container wire up an implementation of this factory that uses the port number and passes it as a constructor argument to your real implementation. Other factory implementations may simply ignore the parameter.

这种方法的优点是您不会污染您的API(或您的具体实现),而您仍然拥有

The advantage of this approach is that you don't pollute your API (or your concrete implementations), and you still have the flexibility that programming to interfaces give you.

缺点是它增加了另一层间接。

The disadvantage is that it adds yet another layer of indirection.

这篇关于依赖注入用户输入的最佳策略是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:05