I've been reading up on delegate and passing methods as a parameter in C#, simply because my "OCD" is gnawing me when I look at this code:
public static T GetSingleItem<T>(string query, params object[] args) where T : new()
{
using (var db = new SQLiteConnection(DbPath))
{
db.Trace = true;
return db.Query<T>(query, args).FirstOrDefault();
}
}
public static List<T> GetItems<T>(string query, params object[] args) where T : new()
{
using (var db = new SQLiteConnection(DbPath))
{
db.Trace = true;
return db.Query<T>(query, args);
}
}
public static void Insert(object obj)
{
using (var db = new SQLiteConnection(DbPath))
{
db.Trace = true;
db.Insert(obj);
}
}
public static void Update(object obj)
{
using (var db = new SQLiteConnection(DbPath))
{
db.Trace = true;
db.Update(obj);
}
}
You shouldn't take this question too serious. I know it's not on the top shelf of urgent problems, but I would anyway want to ask the question as it would be nice to know a solution for this.
Question: Is there a way to encapsulate the using statement and the db.Trace in one method and then simply call the rest of the methodcontent e.g. db.Update(obj) from their specific methods?
The obvious problem with passing a partial method as a parameter e.g.
public static T Runner<T>(Func<T> funcToRun)
Is that I'm calling db.Update() from the object instantiated by the using statement.
Any smart solutions to this pattern?
Aucun commentaire:
Enregistrer un commentaire