I'm working with an existing code base here and while I don't have the time/budget to re-do the entire thing (ultimate solution), I do get a bit of leeway to enhance the code base, as well as maintaining it.
We're using EntityFramework and the SynchronizedBindingList
is essentially just that; it keeps all the data in memory, but also ensures that it's as in-sync as possible with the database. The idea being that memory is cheap and we'd rather use the end-users' (internal staff) memory, than have a few hundred users frequently querying the database. The concept works well and we haven't really had any issues with the setup, I'm just trying to make the code more maintainable.
At present, we have a lot of Manager
classes, essentially one for each table, that has the following code:
public class LessonsLeartManager
{
private static readonly Lazy<LessonsLearntManager> Lazy =
new Lazy<LessonsLearntManager>(() => new LessonsLearntManager());
public SynchronizedBindingList<LessonsLearnt, DbContext> LessonsLearntList { get; private set; }
public static LessonsLearntManager Instance
{
get { return Lazy.Value; }
}
private LessonsLearntManager()
{
LessonsLearntList = new SynchronizedBindingList<LessonsLearnt, DbContext>();
LessonsLearntList.RefreshSync();
}
// Manager specific code goes here.
}
Obviously this is highly repetitive and difficult to maintain if you want to change something as it would have to be changed across multiple files.
I have been fiddling with this idea today and would like to get other opinions on the quality/idea of this code:
public class DatabaseTable<TManager, TTable> where TManager : class, new()
where TTable : class, IDbPk<TTable>, IAutoIncludeEntity<TTable>, new()
{
private static readonly Lazy<TManager> Lazy = new Lazy<TManager>(() => new TManager());
public SynchronizedBindingList<TTable, DbContext> List { get; private set; }
internal DatabaseTable()
{
List = new SynchronizedBindingList<TTable, DbContext>();
List.RefreshSync();
}
public static TManager Instance
{
get { return Lazy.Value; }
}
}
Obviously the previous LessonsLearntManager
code then becomes:
public class LessonsLearntManager : DatabaseTable<LessonsLearntManager, LessonsLearnt>{//Manager specific code}
and can be used as such:
MyGridView.DataSource = LessonsLearntManager.Instance.List;
which is a far bit better than previously (the class declaration anyway, the usage doesn't change much).