I'm using SQLite-net (https://github.com/praeclarum/sqlite-net) for implementing a database using Mono on Android and have the following:
public class DatabaseTestASync
{
private SQLiteAsyncConnection m_db;
public delegate void StocksFound(List<Stock> list);
public event StocksFound StocksFoundListener;
// Uninteresting parts remove, e.g. constructor
public void AddStock(Stock stock)
{
m_db.InsertAsync(stock).ContinueWith(t => { Debug.WriteLine(stock.Symbol + " added"); });
}
public void GetAllStocks()
{
AsyncTableQuery<Stock> query = m_db.Table<Stock>().Where(stock => true);
query.ToListAsync().ContinueWith(QueryReturns);
}
private void QueryReturns(Task<List<Stock>> t)
{
if (StocksFoundListener != null)
StocksFoundListener(t.Result);
}
This is great for giving me a list of stocks, but I envisage having a collection of tables in my project and don't want to create a separate AddX, GetAllX, QueryReturns(X), etc. for each table. I'm after a generic way of doing these database operations and tried the following:
public class DBQuery<T>
{
private SQLiteAsyncConnection m_db;
public delegate void OnRecordsFound(List<T> list);
public event OnRecordsFound RecordsFoundListener;
public DBQuery (SQLiteAsyncConnection db)
{
m_db = db;
}
public void GetAllRecords()
{
AsyncTableQuery<T> query = m_db.Table<T>().Where(r => true); // COMPILE ERROR HERE
query.ToListAsync().ContinueWith(QueryReturns);
}
private void QueryReturns(Task<List<T>> t)
{
if (RecordsFoundListener != null)
RecordsFoundListener(t.Result);
}
}
However, it doesn't compile saying: 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'DatabaseUtility.AsyncTableQuery'.
Any ideas about how I can get this kind of generic database access working?