Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:56:43+00:00 2026-06-02T09:56:43+00:00

I’m working on implementing come business logic in my C# application. I’m having trouble

  • 0

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:

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.

  • if Country, Region, and City information is available, push the data to the project and use the info to know where to insert the data entries.
  • if Country info is not available, create a new Country and use Region and City info to insert the data entries.
  • if only city info is available, create a new Region inside a new Country and use City info to put data there.
  • if no info is available, create a new City inside a new Region inside a new Country and put the data there.
  • if any info is not available out of order (for example Country and Region are available but not City) we have to fall back to the more general case (creating new Country, Region and City in this case).

Additionally:

  • when you create a leaf class, you have to provide the parent in the constructor.
  • querying about what info is available is expensive.
  • I would like to avoid repeating the code that creates new classes.
  • I cannot change the implementation of classes Country, Region, City.

My solution below works, but it’s ugly and using an integer to control app flow gives me shivers.

How can I improve the code snippet below?

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 > 0)
{
    country = new Country(Project, "Unnamed Country");
    target = country;
}
if (level > 1)
{
    region = new Region(country, "Unnamed Region", Region.DefaultRegionSettings);
    target = region;
}
if (level > 2)
{
    city = new City(region, "Unnamed City", 0);
    target = city;
}

// ... proceed with data entry code using `target`...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:56:46+00:00Added an answer on June 2, 2026 at 9:56 am

    Edited: try it like this: The only question I have is where are city, region & country initialized? In the Is() methods?

    Func<Country> GetCountry = () => country ?? (country = new Country(Project, "Unnamed Country"));
    Func<Region> GetRegion = () => region ?? (region = new Region(GetCountry(), "Unnamed Region", Region.DefaultRegionSettings));
    Func<City> GetCity = () => city ?? (city = new City(GetRegion(), "Unnamed City", 0));
    
    IDataEntryTarget target = null;
    
    if (!IsCityInfoAvailable())
    {
        // we have to make a new country, region and city
         target = GetCity();
    }
    else if (!IsRegionInfoAvailable())
    {
        // we have to make a new country and region
        target = GetRegion();
    }
    else if (!IsCountryRegionAvailable())
    {
        // we have to make a new country
        target = GetCountry();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.