On the description presented here I regard this as mildly smelly. However, it seems easy to fix.
I'd be inclined to pass the dmReportBaseData object into the constructor of any component that needs it. This makes the contract clear at compile time rather than enforcing it at runtime as you currently do.
As it currently stands, the contract you enforce is stronger than it needs to be. Although TRBTempDatabase only requires a dmReportBaseData instance, it will only function if it can get that instance from a TdmRBReport report object.
Making this change would also allow TRBTempDatabase and TdmRBReport to have a divorce and still function successfully. And as @Lieven points out in the comments, this would likely make testing easier.
If all you're doing in a base class is maintaining a reference to a parent object then no, it's not code-smell, it's a perfectly legitimate use. You can explicitly design a base class to carry information about "something that might come later."
If the base class is relying on some characteristic of the derived class that isn't present in itself (i.e. the generalized class relies on a specialization of one of its children) then yeah, that might be a bit funky.
发布评论
评论(2)
根据这里提供的描述,我认为这是轻微的气味。然而,它似乎很容易修复。
我倾向于将 dmReportBaseData 对象传递到任何需要它的组件的构造函数中。这使得契约在编译时变得清晰,而不是像您当前那样在运行时强制执行。
就目前情况而言,您执行的合同比其需要的更强大。虽然
TRBTempDatabase
仅需要dmReportBaseData
实例,但只有当它可以从TdmRBReport
报表对象获取该实例时,它才会起作用。进行此更改还可以让
TRBTempDatabase
和TdmRBReport
分离并仍然成功运行。正如@Lieven 在评论中指出的那样,这可能会使测试变得更容易。On the description presented here I regard this as mildly smelly. However, it seems easy to fix.
I'd be inclined to pass the
dmReportBaseData
object into the constructor of any component that needs it. This makes the contract clear at compile time rather than enforcing it at runtime as you currently do.As it currently stands, the contract you enforce is stronger than it needs to be. Although
TRBTempDatabase
only requires admReportBaseData
instance, it will only function if it can get that instance from aTdmRBReport
report object.Making this change would also allow
TRBTempDatabase
andTdmRBReport
to have a divorce and still function successfully. And as @Lieven points out in the comments, this would likely make testing easier.如果您在基类中所做的只是维护对父对象的引用,那么不,这不是代码味道,而是完全合法的使用。您可以显式设计一个基类来携带有关“稍后可能出现的内容”的信息。
如果基类依赖于派生类的某些特征,而派生类本身并不存在(即广义类依赖于其子类之一的专门化),那么是的,这可能有点奇怪。
If all you're doing in a base class is maintaining a reference to a parent object then no, it's not code-smell, it's a perfectly legitimate use. You can explicitly design a base class to carry information about "something that might come later."
If the base class is relying on some characteristic of the derived class that isn't present in itself (i.e. the generalized class relies on a specialization of one of its children) then yeah, that might be a bit funky.