mirror of https://github.com/abpframework/abp.git
12 changed files with 64 additions and 60 deletions
@ -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); |
|||
} |
|||
} |
|||
@ -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<string, IMemoryDatabase> _databases; |
|||
private readonly ConcurrentDictionary<string, IMemoryDatabase> _databases = |
|||
new ConcurrentDictionary<string, IMemoryDatabase>(); |
|||
|
|||
static MemoryDatabaseManager() |
|||
private readonly IServiceProvider _serviceProvider; |
|||
|
|||
public MemoryDatabaseManager(IServiceProvider serviceProvider) |
|||
{ |
|||
_databases = new ConcurrentDictionary<string, IMemoryDatabase>(); |
|||
_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<IMemoryDatabase>()); |
|||
} |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
} |
|||
@ -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)); |
|||
} |
|||
} |
|||
} |
|||
@ -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<>)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue