确定不要在构造函数中使用所有参数吗? - 重新审视
该类在构造函数中注入所有依赖项,但一次仅使用其中一个依赖项。这被认为是糟糕的设计吗?
public class OrderPayment
{
ICreditCardPayment _ccPayment;
ICashPayment _cashPayment;
public OrderPayment(ICreditCardPayment ccPayment, ICashPayment cashPayment)
{
_ccPayment = ccPayment;
_cashPayment = cashPayment;
}
private void PrepareOrder(Order order)
{
// Do stuff with the order
}
public PaymentResult PayByCreditCard(Order order)
{
PrepareOrder(order);
return _ccPayment.Pay(order);
}
public PaymentResult PayByCreditCard(Order order)
{
PrepareOrder(order);
return _cashPayment.Pay(order);
}
}
另一种方法是:
public class OrderPayment
{
private void PrepareOrder(Order order)
{
// Do stuff with the order
}
public PaymentResult PayByCreditCard(Order order, ICreditCardPayment ccPayment)
{
PrepareOrder(order);
return ccPayment.Pay(order);
}
public PaymentResult PayByCreditCard(Order order, ICashPayment cashPayment)
{
PrepareOrder(order);
return cashPayment.Pay(order);
}
}
这会使函数调用稍微复杂化。即使不是每个构造函数参数都被使用,您会使用第一个看起来更干净的吗?考虑到 DI 框架必须实例化潜在的重类,即使它们可能不会全部被使用,我不确定这有多好。
那么您会使用哪一个呢?或者也许有不同的实现?
This class injects all dependencies in the constructor, but only one of the dependencies are used at a time. Is this considered bad design?
public class OrderPayment
{
ICreditCardPayment _ccPayment;
ICashPayment _cashPayment;
public OrderPayment(ICreditCardPayment ccPayment, ICashPayment cashPayment)
{
_ccPayment = ccPayment;
_cashPayment = cashPayment;
}
private void PrepareOrder(Order order)
{
// Do stuff with the order
}
public PaymentResult PayByCreditCard(Order order)
{
PrepareOrder(order);
return _ccPayment.Pay(order);
}
public PaymentResult PayByCreditCard(Order order)
{
PrepareOrder(order);
return _cashPayment.Pay(order);
}
}
An alternative is this:
public class OrderPayment
{
private void PrepareOrder(Order order)
{
// Do stuff with the order
}
public PaymentResult PayByCreditCard(Order order, ICreditCardPayment ccPayment)
{
PrepareOrder(order);
return ccPayment.Pay(order);
}
public PaymentResult PayByCreditCard(Order order, ICashPayment cashPayment)
{
PrepareOrder(order);
return cashPayment.Pay(order);
}
}
This one complicates the function call somewhat. Would you use the first, cleaner looking one, even though not every constructor parameter is used? Considering a DI framework has to instantiate potentially heavy classes even though they may not all be used, I'm not sure how good this is.
So which one would you use? Or maybe a different implementation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会从
ICashPayment
和ICreditCardPayment
重构或提取通用接口。您的代码示例表明您的方法都在调用xPayment.Pay
,这看起来是您的通用接口方法的良好候选者。您的更专业的接口可以继承并构建在它的基础上。
一般来说,我会避免让构造函数(或任何方法)接受未使用的参数,或者如果使用一个参数,则不使用另一个参数。这通常表明您没有在适当的抽象级别上操作,或者您的类/方法有太多职责。
I would refactor or extract a common interface from
ICashPayment
andICreditCardPayment
. Your code sample indicates your methods are both invokingxPayment.Pay
, which looks like a good candidate for your common interface method.Your more specialized interfaces can inherit from and build upon it.
In general, I would avoid having constructors (or any method) accept arguments that go unused, or if one argument is used, the other is not. That's normally an indication that you are either not operating at the proper level of abstraction, or that your class/method has too many responsibilities.
您需要一个需要满足以下所有条件的操作:
您正在尝试定义一些操作顺序与操作中正在执行的内容无关。依赖注入也可以在方法级别使用。
你需要一个像这样的方法:
You need an operation that requires all of the following:
You are trying to define some order of operation independent of what is being performed in the operation. Dependency injection can be used at the method level too.
you need a method like this one: