
Untitled
By: a guest on
Aug 1st, 2012 | syntax:
None | size: 1.53 KB | hits: 6 | expires: Never
How to avoid using a number or enum when implementing business logic
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`...
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();
}