Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I personally think it's a mistake to program against IDs. Instead, I would amend your table to the following: </p> <pre><code> **State table** ID | Desc | IsWorking | IsVacation ----------------------------------------------------------- 1 Working True False 2 Vacation False True </code></pre> <p>I would then use these attributes to make business decisions on such as:</p> <pre><code> public void MakeDecisionOnState(State state) { if (state.IsVacation) DoSomething(); if (state.IsWorking) DoSomethingElse(); } </code></pre> <p>Or by being even more clever, use the factory pattern to create the correct instance based on these attributes:</p> <pre><code> public abstract class State { public Guid Id { get; set; } public string Description { get; set; } public abstract void DoSomething(); } public class WorkingState : State { public override void DoSomething() { //Do something specific for the working state } } public class VacationState : State { public override void DoSomething() { //Do something specific for the vacation state } } public class StateFactory { public static State CreateState(IDataRecord record) { if (record.GetBoolean(2)) return new WorkingState { Id = record.GetGuid(0), Description = record.GetString(1) }; if (record.GetBoolean(3)) return new VacationState { Id = record.GetGuid(0), Description = record.GetString(1) }; throw new Exception("Data is screwed"); } } </code></pre> <p>Now you've eliminated the if/switch statement, and your code could simply be:</p> <pre><code>state.DoSomething(); </code></pre> <p>The reason why I do this is that often these types of entities (or more likely value objects in DDD) can be configured by the customer, i.e. they may not want to have some of the states active in the system, or they may wish to term them something else. By programming against the attributes the customer can delete / edit the records as they please and even if that process generates new ID's it doesn't affect the system, they just need to set the attributes.</p>
 

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