Creating general queries in SQLite-net C # using SQLiteAsyncConnection

I am using SQLite-net ( https://github.com/praeclarum/sqlite-net ) to implement a database using Mono on Android and has 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 assume there is a collection of tables in my project and I don't want to create separate AddX, GetAllX, QueryReturns (X), etc. for each table. I use the general way to perform these database operations and have 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, he does not compile the statement: "T" must be a non-abstract type with an open constructor without parameters in order to use it as a parameter "T" in the generic type or method "DatabaseUtility.AsyncTableQuery".

, ?

+5
1

SQLite, generics...

AsyncTableQuery

public class AsyncTableQuery<T>
        where T : new ()
    {

... T:

public class DBQuery<T> where T : new ()

constrained generic parameter - ( , , T).

+5

All Articles