Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 1.53 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to avoid using a number or enum when implementing business logic
  2. Country country = null;
  3. Region region = null;
  4. City city = null;
  5.  
  6. int level;
  7.  
  8. if (!IsCityInfoAvailable())
  9. {
  10.     // we have to make a new country, region and city
  11.     level = 3;
  12. }
  13. else if (!IsRegionInfoAvailable())
  14. {
  15.     // we have to make a new country and region
  16.     level = 2;
  17. }
  18. else if (!IsCountryRegionAvailable())
  19. {
  20.     // we have to make a new country
  21.     level = 1;
  22. }
  23. else
  24. {
  25.     // we have all the info we need
  26.     level = 0;
  27. }
  28.  
  29. IDataEntryTarget target;
  30.  
  31. if (level > 0)
  32. {
  33.     country = new Country(Project, "Unnamed Country");
  34.     target = country;
  35. }
  36. if (level > 1)
  37. {
  38.     region = new Region(country, "Unnamed Region", Region.DefaultRegionSettings);
  39.     target = region;
  40. }
  41. if (level > 2)
  42. {
  43.     city = new City(region, "Unnamed City", 0);
  44.     target = city;
  45. }
  46.  
  47. // ... proceed with data entry code using `target`...
  48.        
  49. Func<Country> GetCountry = () => country ?? (country = new Country(Project, "Unnamed Country"));
  50. Func<Region> GetRegion = () => region ?? (region = new Region(GetCountry(), "Unnamed Region", Region.DefaultRegionSettings));
  51. Func<City> GetCity = () => city ?? (city = new City(GetRegion(), "Unnamed City", 0));
  52.  
  53. IDataEntryTarget target = null;
  54.  
  55. if (!IsCityInfoAvailable())
  56. {
  57.     // we have to make a new country, region and city
  58.      target = GetCity();
  59. }
  60. else if (!IsRegionInfoAvailable())
  61. {
  62.     // we have to make a new country and region
  63.     target = GetRegion();
  64. }
  65. else if (!IsCountryRegionAvailable())
  66. {
  67.     // we have to make a new country
  68.     target = GetCountry();
  69. }