简体   繁体   中英

Xamarin, Android, PCL SQLLite, SQLite.NET snafu

It's pretty hard to understand the nuget packages to install in a Xamrin solution when searching around the web, there are dozens of packages, dozens of different solutions.

Right now, our solution has 2 projects, an Android one and a PCL one. Our model and data access is defined in the PCL. Our platform implementation is defined in the Android one.

We need SQLite, SQLite.Net (for the data annotations and table relations), and SQLiteExtentions for the *withchildren methods.

We are stuck in old versions because anytime we try to update anything, our frail magical way in which our installed packages are working together just falls apart. We do need to upgrade or find a way to add SQLCipher to this weird snafu of nuget packages.

Our current packages installed, which works:

Android project

PCL Project (model definition and data access methods)

Currently, if we update the SQLiteExtensions to 2.0, a bunch of other SQLite nuget packages are installed, breaking the frail stability of our data access code (fails on the *WithChildren methods with:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1929  'SQLiteConnection' does not contain a definition for         
'GetWithChildren' and the best extension method overload 
'ReadOperations.GetWithChildren<TEntity>(SQLiteConnection, object, bool)' 
requires a receiver of type 'SQLiteConnection'  

We'd also need to incorporate SQLiteCipher and no combinations of packages can be made to work with our solution.

Our Android platform specific implementation:

#region Usings

using OURPCLLib.DataAccess;
using Serilog;
using SQLite.Net;
using SQLite.Net.Interop;
using SQLite.Net.Platform.XamarinAndroid;
using System;
using System.IO;

#endregion Usings

public class AndroidSQLiteDatabase : SQLiteDatabaseAccess
{
    protected ISQLitePlatform SQLitePlatform
    {
        get { return new SQLitePlatformAndroidN(); }
    }

    protected override SQLiteConnection GetConnection()
    {
        var conn = new SQLiteConnection(
            SQLitePlatform,
            "dbpathforus.sqlite",
            SQLiteOpenFlags.ReadWrite |
            SQLiteOpenFlags.FullMutex |
            SQLiteOpenFlags.ProtectionCompleteUnlessOpen |
            SQLiteOpenFlags.Create |
            SQLiteOpenFlags.SharedCache);

        return conn;
    }

}

The (simplified) base data access class in the PCL:

#region Usings

using OURPCLLib.DataAccess.Entities;
using SQLite.Net;
using SQLite.Net.Attributes;
using SQLite.Net.Interop;
using SQLiteNetExtensions.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;

#endregion Usings

public abstract class SQLiteDatabaseAccess
{
    protected abstract SQLiteConnection GetConnection();

    // Example of one of the many methods accessing the DB using SQLite.Net
    public bool Any<TEntity>(Expression<Func<TEntity, bool>> expression)
       where TEntity : class, IBaseEntity, new()
    {        
        using (var currentConnection = this.GetConnection())
        {
            return currentConnection.Table<TEntity>().Where(expression).FirstOrDefault() != null;
        }
    }

    // Example of one of the methods accessing the DB using SQLiteExtentions
    public TEntity GetWithChildren<TEntity>(int id, bool recursive = false)
        where TEntity : class, IBaseEntity, new()
    {
        using (var currentConnection = this.GetConnection())
        {
            return currentConnection.GetWithChildren<TEntity>(id, recursive);
        }
    }
}

Anyone can help us as to how to use SQLite with SQLIte.net, SQLiteExtentions and SQLIte cipher on a project like ours? (data access in a pcl and connection implementation in an android project?

对于任何想知道的人,我只安装了SQLiteNetExtensions并让它自己获得了SQLite依赖关系,从而以最小的代码影响和少量功能损失(但不是很多)解决了它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM