update readme.md migrate V2 1%

This commit is contained in:
Aigio Liu
2021-02-23 19:04:49 +08:00
parent a85f977ccf
commit abf92e98d0
21 changed files with 309 additions and 37 deletions
@@ -0,0 +1,27 @@
using SQLite;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using NotNull = System.Diagnostics.CodeAnalysis.NotNullAttribute;
using SQLiteNotNull = SQLite.NotNullAttribute;
using SQLiteTable = SQLite.TableAttribute;
namespace System.Application.Entities
{
[SQLiteTable("C2F5F5F5")]
[DebuggerDisplay("{DebuggerDisplay(),nq}")]
public sealed class KeyValuePair : IEntity<string>
{
[Column("B1E54167")]
[PrimaryKey]
[SQLiteNotNull]
[NotNull, DisallowNull] // C# 8 not null
public string? Id { get; set; }
[Column("70E8B6F4")]
[SQLiteNotNull]
[NotNull, DisallowNull] // C# 8 not null
public string? Value { get; set; }
string DebuggerDisplay() => $"{Id}, {Value}";
}
}
@@ -0,0 +1,73 @@
using System.Application.Repositories;
using System.Threading.Tasks;
using KeyValuePair = System.Application.Entities.KeyValuePair;
namespace System.Security
{
internal sealed class DesktopClientStorage : Repository<KeyValuePair, string>, IStorage
{
public async Task<string?> GetAsync(string key)
{
var item = await FirstOrDefaultAsync(x => x.Id == key);
return item?.Value;
}
string? IStorage.Get(string key)
{
Func<Task<string?>> func = () => GetAsync(key);
return func.RunSync();
}
void IStorage.Set(string key, string? value)
{
if (string.IsNullOrEmpty(value))
{
DeleteByKey(key);
}
else
{
Func<Task> func = () => InsertOrUpdateAsync(key, value);
func.RunSync();
}
}
async Task InsertOrUpdateAsync(string key, string value)
{
await InsertOrUpdateAsync(new KeyValuePair
{
Id = key,
Value = value,
});
}
async Task IStorage.SetAsync(string key, string? value)
{
if (string.IsNullOrEmpty(value))
{
await DeleteAsync(key);
}
else
{
await InsertOrUpdateAsync(key, value);
}
}
int DeleteByKey(string key)
{
Func<Task<int>> func = () => DeleteAsync(key);
return func.RunSync();
}
bool IStorage.Remove(string key)
{
var result = DeleteByKey(key);
return result > 0;
}
async Task<bool> IStorage.RemoveAsync(string key)
{
var result = await DeleteAsync(key);
return result > 0;
}
}
}
@@ -1,7 +1,9 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Application.Services;
using System.Application.Services.Implementation;
using System.Security;
using MSEXOptions = Microsoft.Extensions.Options.Options;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
@@ -9,13 +11,33 @@ namespace Microsoft.Extensions.DependencyInjection
public static class ServiceCollectionExtensions
{
/// <summary>
/// 添加由 Xamarin.Essentials.SecureStorage 实现的 <see cref="IStorage"/>
/// 尝试添加配置项
/// </summary>
/// <typeparam name="TOptions"></typeparam>
/// <param name="services"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IServiceCollection TryAddOptions<TOptions>(this IServiceCollection services, TOptions options) where TOptions : class, new()
{
services.TryAddSingleton(MSEXOptions.Create(options));
return services;
}
/// <summary>
/// 添加由 Xamarin.Essentials.SecureStorage 或 Repository 实现的 <see cref="IStorage"/>
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection TryAddStorage(this IServiceCollection services)
{
services.TryAddSingleton<IStorage, ClientStorage>();
if (DI.DeviceIdiom == DeviceIdiom.Desktop)
{
services.TryAddSingleton<IStorage, DesktopClientStorage>();
}
else
{
services.TryAddSingleton<IStorage, ClientStorage>();
}
return services;
}