mirror of https://github.com/abpframework/abp.git
committed by
GitHub
13 changed files with 151 additions and 24 deletions
@ -1,10 +1,10 @@ |
|||||
using System.Collections.Generic; |
using Volo.Abp.Domain.Entities; |
||||
|
|
||||
namespace Volo.Abp.Domain.Repositories.MemoryDb |
namespace Volo.Abp.Domain.Repositories.MemoryDb |
||||
{ |
{ |
||||
public interface IMemoryDatabase |
public interface IMemoryDatabase |
||||
{ |
{ |
||||
List<TEntity> Collection<TEntity>(); |
IMemoryDatabaseCollection<TEntity> Collection<TEntity>() where TEntity : class, IEntity; |
||||
|
|
||||
TKey GenerateNextId<TEntity, TKey>(); |
TKey GenerateNextId<TEntity, TKey>(); |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,13 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.Domain.Repositories.MemoryDb |
||||
|
{ |
||||
|
public interface IMemoryDatabaseCollection<TEntity> : IEnumerable<TEntity> |
||||
|
{ |
||||
|
void Add(TEntity entity); |
||||
|
|
||||
|
void Update(TEntity entity); |
||||
|
|
||||
|
void Remove(TEntity entity); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.Domain.Repositories.MemoryDb |
||||
|
{ |
||||
|
public interface IMemoryDbSerializer |
||||
|
{ |
||||
|
byte[] Serialize(object obj); |
||||
|
|
||||
|
object Deserialize(byte[] value, Type type); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,56 @@ |
|||||
|
using System; |
||||
|
using System.Collections; |
||||
|
using System.Collections.Generic; |
||||
|
using Volo.Abp.Domain.Entities; |
||||
|
|
||||
|
namespace Volo.Abp.Domain.Repositories.MemoryDb |
||||
|
{ |
||||
|
public class MemoryDatabaseCollection<TEntity> : IMemoryDatabaseCollection<TEntity> |
||||
|
where TEntity : class, IEntity |
||||
|
{ |
||||
|
private readonly Dictionary<string, byte[]> _dictionary = new Dictionary<string, byte[]>(); |
||||
|
|
||||
|
private readonly IMemoryDbSerializer _memoryDbSerializer; |
||||
|
|
||||
|
public MemoryDatabaseCollection(IMemoryDbSerializer memoryDbSerializer) |
||||
|
{ |
||||
|
_memoryDbSerializer = memoryDbSerializer; |
||||
|
} |
||||
|
|
||||
|
public IEnumerator<TEntity> GetEnumerator() |
||||
|
{ |
||||
|
foreach (var entity in _dictionary.Values) |
||||
|
{ |
||||
|
yield return _memoryDbSerializer.Deserialize(entity, typeof(TEntity)).As<TEntity>(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
IEnumerator IEnumerable.GetEnumerator() |
||||
|
{ |
||||
|
return GetEnumerator(); |
||||
|
} |
||||
|
|
||||
|
public void Add(TEntity entity) |
||||
|
{ |
||||
|
_dictionary.Add(GetEntityKey(entity), _memoryDbSerializer.Serialize(entity)); |
||||
|
} |
||||
|
|
||||
|
public void Update(TEntity entity) |
||||
|
{ |
||||
|
if (_dictionary.ContainsKey(GetEntityKey(entity))) |
||||
|
{ |
||||
|
_dictionary[GetEntityKey(entity)] = _memoryDbSerializer.Serialize(entity); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Remove(TEntity entity) |
||||
|
{ |
||||
|
_dictionary.Remove(GetEntityKey(entity)); |
||||
|
} |
||||
|
|
||||
|
private string GetEntityKey(TEntity entity) |
||||
|
{ |
||||
|
return entity.GetKeys().JoinAsString(","); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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 |
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>()); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
@ -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 Microsoft.Extensions.DependencyInjection.Extensions; |
||||
using Volo.Abp.Domain; |
using Volo.Abp.Domain; |
||||
|
using Volo.Abp.Domain.Repositories.MemoryDb; |
||||
|
using Volo.Abp.Json; |
||||
using Volo.Abp.Modularity; |
using Volo.Abp.Modularity; |
||||
using Volo.Abp.Uow.MemoryDb; |
using Volo.Abp.Uow.MemoryDb; |
||||
|
|
||||
namespace Volo.Abp.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))] |
[DependsOn(typeof(AbpDddDomainModule))] |
||||
public class AbpMemoryDbModule : AbpModule |
public class AbpMemoryDbModule : AbpModule |
||||
{ |
{ |
||||
public override void ConfigureServices(ServiceConfigurationContext context) |
public override void ConfigureServices(ServiceConfigurationContext context) |
||||
{ |
{ |
||||
context.Services.TryAddTransient(typeof(IMemoryDatabaseProvider<>), typeof(UnitOfWorkMemoryDatabaseProvider<>)); |
context.Services.TryAddTransient(typeof(IMemoryDatabaseProvider<>), typeof(UnitOfWorkMemoryDatabaseProvider<>)); |
||||
|
context.Services.TryAddTransient(typeof(IMemoryDatabaseCollection<>), typeof(MemoryDatabaseCollection<>)); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue