1

Web を検索すると、Xamrin ソリューションにインストールする nuget パッケージを理解するのはかなり困難です。数十のパッケージ、数十の異なるソリューションがあります。

現在、ソリューションには Android プロジェクトと PCL プロジェクトの 2 つのプロジェクトがあります。モデルとデータ アクセスは PCL で定義されています。私たちのプラットフォームの実装は、Android で定義されています。

SQLite、SQLite.Net (データ注釈とテーブル関係用)、および *withchildren メソッド用の SQLiteExtentions が必要です。

何かを更新しようとするたびに、インストールされたパッケージが連携する脆弱な魔法の方法が崩れてしまうため、古いバージョンにとどまっています。アップグレードするか、SQLCipher をこの奇妙な nuget パッケージに追加する方法を見つける必要があります。

現在インストールされているパッケージは次のように機能します。

Android プロジェクト

PCL プロジェクト(モデル定義とデータ アクセス方法)

現在、SQLiteExtensions を 2.0 に更新すると、他の SQLite nuget パッケージが多数インストールされ、データ アクセス コードの脆弱な安定性が損なわれます (以下の *WithChildren メソッドで失敗します)。

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'  

また、SQLiteCipher を組み込む必要があり、ソリューションで動作するようにパッケージを組み合わせることはできません。

Android プラットフォーム固有の実装:

#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;
    }

}

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);
        }
    }
}

私たちのようなプロジェクトで、SQLite.net、SQLiteExtentions、および SQLIte cipher で SQLite を使用する方法について、誰でも助けてもらえますか? (Androidプロジェクトでのpclと接続の実装でのデータアクセス?

4

1 に答える 1