Skip to content

Instantly share code, notes, and snippets.

@Eibwen
Created November 12, 2014 23:49
Show Gist options
  • Save Eibwen/b5a59d6b73c4ac8ea7b3 to your computer and use it in GitHub Desktop.
Save Eibwen/b5a59d6b73c4ac8ea7b3 to your computer and use it in GitHub Desktop.
A proposal for tracking if code is EVER hit or not
void Main()
{
//Usage is simply, anywhere in the solution:
CodeCalledRepository.Instance.CodeHit(CodeCalled.OldAssEmail1);
//Result is:
Database.Dump("Result 1");
CodeCalledRepository.Instance.DEBUGDUMP();
CodeCalledRepository.Instance.CodeHit(CodeCalled.additionalinfo);
//Will only update the database once per server, per reset.
CodeCalledRepository.Instance.CodeHit(CodeCalled.OldAssEmail1);
CodeCalledRepository.Instance.CodeHit(CodeCalled.OldAssEmail1);
CodeCalledRepository.Instance.CodeHit(CodeCalled.OldAssEmail1);
//Result is:
Database.Dump("Result after many calls");
CodeCalledRepository.Instance.DEBUGDUMP();
}
//This is in-memory on each server
public class CodeCalledRepository
{
static Lazy<CodeCalledRepository> lazyInstance = new Lazy<CodeCalledRepository>(() => new CodeCalledRepository());
public static CodeCalledRepository Instance
{
get
{
return lazyInstance.Value;
}
}
private CodeCalledRepository()
{
OnLoad();
}
Dictionary<CodeCalled, bool> _repository = new Dictionary<CodeCalled, bool>();
public void OnLoad()
{
"Loading".Dump();
//Load from the Database
// Note: ToList here is to not modify the enumeration
foreach (var row in Database.ToList())
{
CodeCalled value;
if (Enum.TryParse(row.Name, out value))
{
//Exists in the enum in code, add to cache
_repository.Add(value, row.HitCount > 0);
}
else
{
//Not in the enum in code, remove from database
Database.RemoveAll(x => x.Name == row.Name);
}
}
//Make sure all values exist in the Database
foreach (CodeCalled code in Enum.GetValues(typeof(CodeCalled)))
{
if (!_repository.ContainsKey(code))
{
Database.Add(new CodeCalled_Table { Name = code.ToString(), HitCount = 0, DateCreated = DateTime.Now });
_repository.Add(code, false);
}
}
}
public void CodeHit(CodeCalled value)
{
if (_repository.ContainsKey(value))
{
if (!_repository[value])
{
//Has not been hit before, on this server
//Update cache and increment database
_repository[value] = true;
Database.Find(x => x.Name == value.ToString()).HitCount++;
}
}
else
{
throw new Exception("Unknown CodeCalled value, this should be impossible");
}
}
public void DEBUGDUMP()
{
_repository.Dump();
}
}
//Enum so we can keep track... able to remove values from this and will be removed from database
public enum CodeCalled
{
None,
//## Section for Emails
OldAssEmail1,
//## Section for Email fields
additionalinfo,
AdditionalMatchDetails,
//...
//## Section for Business code
OldAssCode3
//## Section for idk... sections could help reduce merge conflicts, similar to the TicketToggle file
}
//This is the database table
public static List<CodeCalled_Table> Database = new List<CodeCalled_Table>
{
new CodeCalled_Table { Name = "SomeOldValue", DateCreated = new DateTime(2014, 11, 12) },
new CodeCalled_Table { Name = "AnotherOldValue", HitCount = 10, DateCreated = new DateTime(2014, 11, 11) },
};
public class CodeCalled_Table
{
public string Name { get; set; }
public int HitCount { get; set; }
public DateTime DateCreated { get; set; }
//Could have a DateUpdated too, but I'm invisioning this to be short-term checks
// So nothing should last in the enum for more than 3 months or something...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment