From 81e96fa654040f7d628274d175025e410655ddf9 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 25 May 2019 14:33:00 +0800 Subject: [PATCH] Use Json serialization/deserialization entity. --- .../MemoryDb/IMemoryDatabaseCollection.cs | 2 -- .../MemoryDb/IMemoryDbSerializer.cs | 8 +++-- .../Repositories/MemoryDb/MemoryDatabase.cs | 13 ++++++--- .../MemoryDb/MemoryDatabaseCollection.cs | 9 ++---- .../MemoryDb/MemoryDatabaseManager.cs | 20 ++++++++----- .../MemoryDb/MemoryDbBinarySerializer.cs | 29 ------------------- .../MemoryDb/Utf8JsonMemoryDbSerializer.cs | 27 +++++++++++++++++ .../Volo/Abp/MemoryDb/AbpMemoryDbModule.cs | 6 ++-- .../UnitOfWorkMemoryDatabaseProvider.cs | 7 +++-- .../Volo/Abp/TestApp/Domain/City.cs | 1 - .../Volo/Abp/TestApp/Domain/Person.cs | 1 - .../Volo/Abp/TestApp/Domain/Phone.cs | 1 - 12 files changed, 64 insertions(+), 60 deletions(-) delete mode 100644 framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbBinarySerializer.cs create mode 100644 framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/Utf8JsonMemoryDbSerializer.cs diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabaseCollection.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabaseCollection.cs index f730bbbe48..a2f4d5f270 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabaseCollection.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabaseCollection.cs @@ -9,7 +9,5 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb void Update(TEntity entity); void Remove(TEntity entity); - - bool Contains(TEntity entity); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbSerializer.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbSerializer.cs index 8dec50fb75..2854a7893f 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbSerializer.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbSerializer.cs @@ -1,9 +1,11 @@ -namespace Volo.Abp.Domain.Repositories.MemoryDb +using System; + +namespace Volo.Abp.Domain.Repositories.MemoryDb { public interface IMemoryDbSerializer { - object Serialize(object obj); + byte[] Serialize(object obj); - object Deserialize(object obj); + object Deserialize(byte[] value, Type type); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabase.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabase.cs index 1df0397869..6c2aa1417c 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabase.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabase.cs @@ -1,18 +1,23 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Entities; namespace Volo.Abp.Domain.Repositories.MemoryDb { - public class MemoryDatabase : IMemoryDatabase + public class MemoryDatabase : IMemoryDatabase, ITransientDependency { private readonly ConcurrentDictionary _sets; private readonly ConcurrentDictionary _entityIdGenerators; - public MemoryDatabase() + private readonly IServiceProvider _serviceProvider; + + public MemoryDatabase(IServiceProvider serviceProvider) { + _serviceProvider = serviceProvider; _sets = new ConcurrentDictionary(); _entityIdGenerators = new ConcurrentDictionary(); } @@ -21,8 +26,8 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb where TEntity : class, IEntity { return _sets.GetOrAdd(typeof(TEntity), - _ => new MemoryDatabaseCollection(new MemoryDbBinarySerializer())) as - MemoryDatabaseCollection; + _ => _serviceProvider.GetRequiredService>()) as + IMemoryDatabaseCollection; } public TKey GenerateNextId() diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs index d7d77ce6f4..11c7f992a9 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs @@ -8,7 +8,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb public class MemoryDatabaseCollection : IMemoryDatabaseCollection where TEntity : class, IEntity { - private readonly Dictionary _dictionary = new Dictionary(); + private readonly Dictionary _dictionary = new Dictionary(); private readonly IMemoryDbSerializer _memoryDbSerializer; @@ -21,7 +21,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb { foreach (var entity in _dictionary.Values) { - yield return _memoryDbSerializer.Deserialize(entity).As(); + yield return _memoryDbSerializer.Deserialize(entity, typeof(TEntity)).As(); } } @@ -48,11 +48,6 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb _dictionary.Remove(GetEntityKey(entity)); } - public bool Contains(TEntity entity) - { - return _dictionary.ContainsKey(GetEntityKey(entity)); - } - private string GetEntityKey(TEntity entity) { return entity.GetKeys().JoinAsString(","); diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseManager.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseManager.cs index 4926abebf3..7507f3d625 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseManager.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseManager.cs @@ -1,19 +1,25 @@ -using System.Collections.Concurrent; +using System; +using System.Collections.Concurrent; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.Domain.Repositories.MemoryDb { - public static class MemoryDatabaseManager + public class MemoryDatabaseManager : ISingletonDependency { - private static ConcurrentDictionary _databases; + private readonly ConcurrentDictionary _databases = + new ConcurrentDictionary(); - static MemoryDatabaseManager() + private readonly IServiceProvider _serviceProvider; + + public MemoryDatabaseManager(IServiceProvider serviceProvider) { - _databases = new ConcurrentDictionary(); + _serviceProvider = serviceProvider; } - public static IMemoryDatabase Get(string databaseName) + public IMemoryDatabase Get(string databaseName) { - return _databases.GetOrAdd(databaseName, _ => new MemoryDatabase()); + return _databases.GetOrAdd(databaseName, _ => _serviceProvider.GetRequiredService()); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbBinarySerializer.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbBinarySerializer.cs deleted file mode 100644 index 0dc0fb3e53..0000000000 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbBinarySerializer.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.IO; -using System.Runtime.Serialization.Formatters.Binary; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Domain.Repositories.MemoryDb -{ - public class MemoryDbBinarySerializer : IMemoryDbSerializer, ITransientDependency - { - public object Serialize(object obj) - { - var stream = new MemoryStream(); - var formatter = new BinaryFormatter(); - formatter.Serialize(stream, obj); - return stream; - } - - public object Deserialize(object obj) - { - if (!(obj is MemoryStream stream)) - { - return null; - } - - var formatter = new BinaryFormatter(); - stream.Seek(0, SeekOrigin.Begin); - return formatter.Deserialize(stream); - } - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/Utf8JsonMemoryDbSerializer.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/Utf8JsonMemoryDbSerializer.cs new file mode 100644 index 0000000000..9964e6219b --- /dev/null +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/Utf8JsonMemoryDbSerializer.cs @@ -0,0 +1,27 @@ +using System; +using System.Text; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Json; + +namespace Volo.Abp.Domain.Repositories.MemoryDb +{ + public class Utf8JsonMemoryDbSerializer : IMemoryDbSerializer, ITransientDependency + { + private readonly IJsonSerializer _jsonSerializer; + + public Utf8JsonMemoryDbSerializer(IJsonSerializer jsonSerializer) + { + _jsonSerializer = jsonSerializer; + } + + byte[] IMemoryDbSerializer.Serialize(object obj) + { + return Encoding.UTF8.GetBytes(_jsonSerializer.Serialize(obj)); + } + + public object Deserialize(byte[] value, Type type) + { + return _jsonSerializer.Deserialize(type, Encoding.UTF8.GetString(value)); + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/AbpMemoryDbModule.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/AbpMemoryDbModule.cs index d4f679a5b7..bd8b4b98a4 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/AbpMemoryDbModule.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/MemoryDb/AbpMemoryDbModule.cs @@ -1,19 +1,19 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.Domain; +using Volo.Abp.Domain.Repositories.MemoryDb; +using Volo.Abp.Json; using Volo.Abp.Modularity; using Volo.Abp.Uow.MemoryDb; namespace Volo.Abp.MemoryDb { - /* TODO: Consider to store objects as binary serialized in the memory, which makes unit tests more realistic. - */ - [DependsOn(typeof(AbpDddDomainModule))] public class AbpMemoryDbModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.TryAddTransient(typeof(IMemoryDatabaseProvider<>), typeof(UnitOfWorkMemoryDatabaseProvider<>)); + context.Services.TryAddTransient(typeof(IMemoryDatabaseCollection<>), typeof(MemoryDatabaseCollection<>)); } } } diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Uow/MemoryDb/UnitOfWorkMemoryDatabaseProvider.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Uow/MemoryDb/UnitOfWorkMemoryDatabaseProvider.cs index 386ec77b12..24c1cdaeb9 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Uow/MemoryDb/UnitOfWorkMemoryDatabaseProvider.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Uow/MemoryDb/UnitOfWorkMemoryDatabaseProvider.cs @@ -11,15 +11,18 @@ namespace Volo.Abp.Uow.MemoryDb private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IConnectionStringResolver _connectionStringResolver; + private readonly MemoryDatabaseManager _memoryDatabaseManager; public UnitOfWorkMemoryDatabaseProvider( IUnitOfWorkManager unitOfWorkManager, IConnectionStringResolver connectionStringResolver, - TMemoryDbContext dbContext) + TMemoryDbContext dbContext, + MemoryDatabaseManager memoryDatabaseManager) { _unitOfWorkManager = unitOfWorkManager; _connectionStringResolver = connectionStringResolver; DbContext = dbContext; + _memoryDatabaseManager = memoryDatabaseManager; } public IMemoryDatabase GetDatabase() @@ -36,7 +39,7 @@ namespace Volo.Abp.Uow.MemoryDb var databaseApi = unitOfWork.GetOrAddDatabaseApi( dbContextKey, () => new MemoryDbDatabaseApi( - MemoryDatabaseManager.Get(connectionString) + _memoryDatabaseManager.Get(connectionString) )); return ((MemoryDbDatabaseApi)databaseApi).Database; diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs index 92df3a166e..76361463d7 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs @@ -3,7 +3,6 @@ using Volo.Abp.Domain.Entities; namespace Volo.Abp.TestApp.Domain { - [Serializable] public class City : AggregateRoot { public string Name { get; set; } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs index 9b017ef51f..8235225def 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs @@ -7,7 +7,6 @@ using Volo.Abp.MultiTenancy; namespace Volo.Abp.TestApp.Domain { [AutoMapTo(typeof(PersonEto))] - [Serializable] public class Person : FullAuditedAggregateRoot, IMultiTenant { public virtual Guid? TenantId { get; set; } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Phone.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Phone.cs index 43087a7d87..e53f2bc2e3 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Phone.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Phone.cs @@ -7,7 +7,6 @@ using Volo.Abp.Domain.Entities; namespace Volo.Abp.TestApp.Domain { [Table("AppPhones")] - [Serializable] public class Phone : Entity { public virtual Guid PersonId { get; set; }