I'm reading data from CSV files and store the result in a simple DataContext
instance that gets injected throughout my application.
public class DataContext
{
private static readonly IDictionary<Type, IEnumerable<BaseEntity>> EntityMap =
Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(BaseEntity)))
.ToDictionary(t => t, t => Enumerable.Empty<BaseEntity>());
public IEnumerable<T> Get<T>() where T : BaseEntity
{
return EntityMap[typeof(T)].Cast<T>();
}
public void Set<T>(IEnumerable<BaseEntity> value) where T : BaseEntity
{
EntityMap[typeof(T)] = value.ToList();
}
}
I listen for changes in a folder. As soon as a CSV file gets updated, I'll update the corresponding list. Something like this:
void OnFileChange(string fileName)
{
// handle in a separate thread to decrease the load of FileSystemWatcher
Task.Run(() => {
var parsedFileData = parser.Parse<DataType>(fileName);
dataContext.Set<DataType>(parsedFileData);
});
}
I never do a delta update of entities. It's always a complete replacement of the collection, which is by intention.
Some thoughts:
- What do you think of this in memory approach with variables vs something like Redis?
- Any other comments?