C#:将 BusinessObject 传递给“BusinessLayer”构造函数或其方法?
我负责支持 C# Winforms 应用程序,该应用程序使用 BusinessObjects(不包含逻辑,仅包含属性)和带有操作这些实体的类(“帮助程序”)的 BusinessLayer。
问题: 如果您将 BusinessObject 传递给 Helpers 构造函数,然后在构造函数内,实例化 Helper 的可公开访问的 Entity 变量 或者 您是否应该将实体传递给对其进行操作的方法?
场景 1:对于构造函数
Car myCar = new Car();
CarHelper ch = new CarHelper(myCar);
ch.Wash(suds);
ch.Upgrade(upgradeKit);
ch.Save();
场景 2:对于作用于实体的方法
Car myCar = new Car();
CarHelper ch = new CarHelper();
ch.Wash(myCar, suds);
ch.Upgrade(myCar, upgradeKit);
ch.Save(myCar);
场景 1 存在两个主要问题: A) 下一个开发人员必须深入研究 CarHelper 类,以认识到它有一个公共 Car 访问器属性,它在需要它的方法中引用该属性。这进一步混淆了 Helper 类,因为每个方法在执行其职责之前都需要检查“null”Car 属性...... B) 如果在操作之间存在一堆其他代码,则可能会不清楚 ch.Wash() 实际上在做什么......它甚至对 Car 对象起作用吗......?
大家觉得怎么样???
I'm charged with the support of a C# Winforms app which uses BusinessObjects (containing no logic, just properties) and a BusinessLayer with classes ('Helpers') that manipulate those entities.
The question:
Should you pass in the BusinessObject to the Helpers constructor and then inside the constructor, instantiate the Helper's publicly accessible Entity variable
OR
Should you just pass the Entity to the methods that act on it?
Scenario 1: To the constructor
Car myCar = new Car();
CarHelper ch = new CarHelper(myCar);
ch.Wash(suds);
ch.Upgrade(upgradeKit);
ch.Save();
Scenario 2: To the methods that act on the Entity
Car myCar = new Car();
CarHelper ch = new CarHelper();
ch.Wash(myCar, suds);
ch.Upgrade(myCar, upgradeKit);
ch.Save(myCar);
Two major problems i have with Scenario 1:
A) The next developer has to dig into the CarHelper class to realise that it has a public Car accessor property which it references within methods that need it. This further obfuscates the Helper class in that each method needs to check against a 'null' Car property before performing its duties...
B) If there exists a bunch of other code in between operations, it can become unclear what ch.Wash() is actually doing...does it even act on a Car object at all...?
What does everyone think???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是否有任何原因导致您无法将逻辑移至 BusinessObject 中
完全取消帮助程序类。阅读起来更具语义意义,并且无需检查空值。
需要维护的类数量也减少了一半
Is there any reason why you can't move the logic into the BusinessObject
Do away with the helper class entirely. Makes more semantic sense to read, and there's no need to check for nulls.
Half as many classes to maintain as well
这是非常正确的...在我看来,将 BusinessObject 中的逻辑包装起来以使其具有自我意识也是最好的...但我不认为这是一个选项,因为:
在“应用程序”中,BusinessObjects 位于命名空间中(....ApplicationServices) 由 DAO 引用,因此它实际上无法调用 DAO 方法(因为这会导致循环依赖) - 因此它无法实现该功能 这
似乎是整个应用程序背后的前提是 BusinessObjects 没有实现根本没有任何逻辑......它们只是信息的容器并且没有任何行为。
然后你有作用于实体的 BusinessLayer 类...
然后你有将实体中的更改保存到数据库的 DataLayer 类。
所以显然,让实体自我意识并实现自己的行为是一个很大的“不不”(在这个应用程序中)......我确信这是这里真正的问题。
但是,假设我无法改变这一点,你会怎么做?
或者
That's very true...wrapping up the logic in the BusinessObject to make it self-aware is best in my opinion too...i didn't consider that an option though because:
In 'the application' the BusinessObjects are in a namespace (....ApplicationServices) which is referenced by the DAO and so it can't actually call DAO methods (as it would cause a circular dependancy) - so it can't implement the functionality for
It seems the premise behind the entire application is that the BusinessObjects do not implement any logic at all...they are just containers of information and do not have any behaviour.
Then you have BusinessLayer classes which act on the entities...
Then you have the DataLayer classes which persist the changes in the entites to the DB.
So apparently, making the Entities self aware and implement their own behaviour is a big 'no no' (in this application)... i'm sure that is the real problem here.
However, assuming i can't change that, what would you do?
OR
让您的 CarHelper 类扩展 Car
两全其美怎么样?
What about making your CarHelper class extend Car
Best of both worlds