Note that there are some explanatory texts on larger screens.

plurals
  1. POIn C# or OOP, should 2 classes reference each other that are related?
    primarykey
    data
    text
    <p>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: </p> <pre><code>public class Policy { public List &lt; PolicyTerm &gt; myTerms; person Customer; string PolicyNumber; } public class PolicyTerm { public Billing myBill; Datetime effectivedate; List &lt; Activities &gt; 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; } } </code></pre> <p>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.</p> <p>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? </p> <p>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.</p> <p>Any advice that can be provided would be much appreciated. </p> <p>Update: Code block was updated to provide more details on the code in question. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload