在C#或OOP中,两个类应该相互引用吗? [英] In C# or OOP, should 2 classes reference each other that are related?

查看:55
本文介绍了在C#或OOP中,两个类应该相互引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发类库.我设计了3个主要的类来帮助我们对数据进行建模.它们的设计使得A类包含B类实例的列表,而B类包含对C类实例的引用,即:

I am working on a class library using C#. I have designed 3 main classes to help model our data. They are designed such that class A contains a list of class B instances, and class B contains a reference to a class C instance, ie:

public class Policy
{
    public List < PolicyTerm > myTerms;
    person Customer;
    string PolicyNumber;
}

public class PolicyTerm
{
     public Billing myBill;
     Datetime effectivedate;
     List < Activities > termActivities;
     public doAction()
     {
          use value from Policy, like PolicyNumber;
     }

}

public class Billing
{
    float remainingBalance;
    Datetime nextDueDate;
    public void doSomething()
    {
         reference value from PolicyTerm, such as effective date;
         use value from Policy, such as PolicyNumber;
    }
}

我遇到的问题是,当我尝试在PolicyTerm或Billing中使用需要来自包含类的数据的方法时.在上面的示例中,这就是尝试使用PolicyTerm中的值的"doSomething"方法,例如该术语在请求数据或将数据保存到我们的数据库中的生效日期.

The problem I have is when I try to use a method within PolicyTerm or Billing that needs data from the containing class. In the example above, this would be the method "doSomething" trying to use a value from PolicyTerm, like the effective date for the term in requesting or saving data to our database.

由于这种情况,我想知道我的班级设计是否正确.我是否应该仅在子类中添加对父"类的引用,以使父类的数据可用?还是我需要重新考虑代码的整体结构和设计?

I am wondering if I have the correct design for my classes because of this scenario. Should I just add a reference to the "parent" class within the child classes, in order to make the parent's data available? Or do I need to rethink the overall structure and design of the code?

我觉得类设计可以很好地用于对数据和业务规则进行建模,但是确实会产生一些类似上述情况的限制.我喜欢PolicyTerm和Billing的分离,因为它能够独立修改和测试该代码.另外,我觉得它使每个部分都更小,更简单.

I feel like the class design works well for modeling the data and our business rules, but it does create a few limitations like the above situation. I liked the separation of the PolicyTerm and Billing for the ability to allow that code to be modified and tested independently. Also, I feel like it keeps each section smaller and simpler.

任何可以提供的建议将不胜感激.

Any advice that can be provided would be much appreciated.

更新:代码块已更新,以提供有关该代码的更多详细信息.

Update: Code block was updated to provide more details on the code in question.

推荐答案

如果 doSomething()始终需要对 C 对象的父级的引用,则应确实将其放在引用到C中,您可以确保它引用正确的 B 实例.OTOH如果该引用并不总是父级,但仍然总是引用同一个 B 实例,它仍然建议将其转换为 C 的成员.OTOH,如果可以使用不同的引用来调用 doSomething(),则该引用应作为方法参数保留.

If doSomething() always needs the reference to the C object's parent, you should indeed put this reference into C where you can ensure that it refers to the correct B instance. OTOH if that reference is not always the parent, but still it is always going to refer to the same B instance, it still suggests turning it into a member of C. OTOH if doSomething() can be called with varying references, that reference should be kept as a method parameter.

从孩子到父母的引用或在两个类之间具有相互依赖关系本身并不坏-它取决于上下文.这样做的结果是两个类不能分开使用,因此实际上它们构成了 component .这可能对您来说可能可接受或不可接受.

It is not bad per se to put a reference from child to parent, or to have a mutual dependency between two classes - it depends on the context. The consequence of this is that the two classes can not be used separately, so in fact they form a component. This may or may not be acceptable for you.

组件通常可以由多个类组成-具有其项和迭代器的集合实际上是一个典型示例.但是,建议在物理级别上也表达这些类之间的逻辑依赖性.通过将一个类作为另一个类的内部类,或将两个类都作为第三类的内部类.

Components in general can consist of multiple classes - a collection with its items and iterator(s) is in fact a typical example. However, it is advisable to express the logical dependency between these classes on the physical level as well, e.g. by making one class an inner class of the other, or making both classes inner classes in a third class.

这篇关于在C#或OOP中,两个类应该相互引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆