Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid using a number or enum when implementing business logic
    primarykey
    data
    text
    <p>I'm working on implementing come business logic in my C# application. I'm having trouble fitting the logic into a sane piece of code. The logic to implement goes like this:</p> <p>There is a tree of elements, let's say: Project, Country, Region, City. A single Project contains Countries. Countries contains Regions, Region contains Cities and Cities contain data entries. We'll be populating the elements with data entries based on what information is available.</p> <ul> <li>if <em>Country</em>, <em>Region</em>, and <em>City</em> information is available, push the data to the project and use the info to know where to insert the data entries.</li> <li>if <em>Country</em> info is not available, create a new <em>Country</em> and use <em>Region</em> and <em>City</em> info to insert the data entries.</li> <li>if only city info is available, create a new <em>Region</em> inside a new <em>Country</em> and use <em>City</em> info to put data there.</li> <li>if no info is available, create a new <em>City</em> inside a new <em>Region</em> inside a new <em>Country</em> and put the data there.</li> <li>if any info is not available out of order (for example <em>Country</em> and <em>Region</em> are available but not <em>City</em>) we have to fall back to the more general case (creating new <em>Country</em>, <em>Region</em> and <em>City</em> in this case). </li> </ul> <p>Additionally:</p> <ul> <li>when you create a leaf class, you have to provide the parent in the constructor.</li> <li>querying about what info is available is expensive.</li> <li>I would like to avoid repeating the code that creates new classes.</li> <li>I cannot change the implementation of classes <code>Country</code>, <code>Region</code>, <code>City</code>.</li> </ul> <p>My solution below works, but it's ugly and using an integer to control app flow gives me shivers.</p> <p><strong>How can I improve the code snippet below?</strong></p> <pre><code>Country country = null; Region region = null; City city = null; int level; if (!IsCityInfoAvailable()) { // we have to make a new country, region and city level = 3; } else if (!IsRegionInfoAvailable()) { // we have to make a new country and region level = 2; } else if (!IsCountryRegionAvailable()) { // we have to make a new country level = 1; } else { // we have all the info we need level = 0; } IDataEntryTarget target; if (level &gt; 0) { country = new Country(Project, "Unnamed Country"); target = country; } if (level &gt; 1) { region = new Region(country, "Unnamed Region", Region.DefaultRegionSettings); target = region; } if (level &gt; 2) { city = new City(region, "Unnamed City", 0); target = city; } // ... proceed with data entry code using `target`... </code></pre>
    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