1

Generally, I try and avoid using inheritance in WCF contracts, preferring composition.

But in the following situation...

  • I have a service operation that can result in one of two things: ResultA and ResultB.
  • There is a boolean/enum in the response message to report this outcome.
  • There are a number of other properties in the response message. Some of these are only relevant in the event of ResultA and some are only relevant in the event of ResultB.

I see my options as being:

  1. Have a single response message contract that contains everything and when properties are not relevant, they are left as null. The client then has to look at the bool/enum to see if its ResultA or ResultB and ignore properties accordingly.
  2. Have 2 response messages contracts, both inheriting from a shared base. One representing ResultA and its relevant properties and one representing ResultB and its relevant properties.

I much prefer option 2 for a number of reasons, but it breaks the composition over inheritance rule.

What do people think?

4

3 回答 3

2

我的直觉是“重新设计你的界面”。具有可疑返回类型的方法通常不是良好设计的标志。这会导致该方法的每个调用者出现大量不必要且容易出错的逻辑。

所以我建议“秘密选项 3”:将接口重构为两个单独的方法。

于 2009-10-12T11:11:09.063 回答
0

更喜欢组合而不是继承!=永远不要使用继承:-)

于 2009-10-12T21:37:11.557 回答
0

所有规则都是用来打破的。如果您正在重用对象并且系统允许您使用继承......为什么不使用它呢?正如Phil Haack所说的那样……为自己考虑。

用一套人为的规则来限制自己是让你工作变得更加困难的好方法。我们可以使用继承是有原因的,我说这就是其中之一。

比继承更喜欢组合(史蒂夫·罗) 这是另一个角度。但是,如果您阅读它,他说的是功能的重用,而不是数据的重用。

于 2009-10-12T11:01:31.197 回答