Browse Source

resolve #1147 Use serialization/deserialization for memorydb.

pull/1151/head
maliming 7 years ago
parent
commit
b9b7f98a35
  1. 4
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabase.cs
  2. 15
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabaseCollection.cs
  3. 2
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbRepository.cs
  4. 9
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbSerializer.cs
  5. 10
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabase.cs
  6. 61
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs
  7. 29
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbBinarySerializer.cs
  8. 5
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs
  9. 2
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDbCoreRepositoryExtensions.cs
  10. 3
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/PeopleAppService.cs
  11. 1
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs
  12. 1
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs
  13. 1
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Phone.cs

4
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabase.cs

@ -1,10 +1,10 @@
using System.Collections.Generic;
using Volo.Abp.Domain.Entities;
namespace Volo.Abp.Domain.Repositories.MemoryDb
{
public interface IMemoryDatabase
{
List<TEntity> Collection<TEntity>();
IMemoryDatabaseCollection<TEntity> Collection<TEntity>() where TEntity : class, IEntity;
TKey GenerateNextId<TEntity, TKey>();
}

15
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDatabaseCollection.cs

@ -0,0 +1,15 @@
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);
bool Contains(TEntity entity);
}
}

2
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbRepository.cs

@ -8,7 +8,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
{
IMemoryDatabase Database { get; }
List<TEntity> Collection { get; }
IMemoryDatabaseCollection<TEntity> Collection { get; }
}
public interface IMemoryDbRepository<TEntity, TKey> : IMemoryDbRepository<TEntity>, IRepository<TEntity, TKey>

9
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/IMemoryDbSerializer.cs

@ -0,0 +1,9 @@
namespace Volo.Abp.Domain.Repositories.MemoryDb
{
public interface IMemoryDbSerializer
{
object Serialize(object obj);
object Deserialize(object obj);
}
}

10
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabase.cs

@ -1,6 +1,7 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Volo.Abp.Domain.Entities;
namespace Volo.Abp.Domain.Repositories.MemoryDb
{
@ -16,9 +17,12 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
_entityIdGenerators = new ConcurrentDictionary<Type, InMemoryIdGenerator>();
}
public List<TEntity> Collection<TEntity>()
public IMemoryDatabaseCollection<TEntity> Collection<TEntity>()
where TEntity : class, IEntity
{
return _sets.GetOrAdd(typeof(TEntity), _ => new List<TEntity>()) as List<TEntity>;
return _sets.GetOrAdd(typeof(TEntity),
_ => new MemoryDatabaseCollection<TEntity>(new MemoryDbBinarySerializer())) as
MemoryDatabaseCollection<TEntity>;
}
public TKey GenerateNextId<TEntity, TKey>()

61
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDatabaseCollection.cs

@ -0,0 +1,61 @@
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, object> _dictionary = new Dictionary<string, object>();
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).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));
}
public bool Contains(TEntity entity)
{
return _dictionary.ContainsKey(GetEntityKey(entity));
}
private string GetEntityKey(TEntity entity)
{
return entity.GetKeys().JoinAsString(",");
}
}
}

29
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbBinarySerializer.cs

@ -0,0 +1,29 @@
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);
}
}
}

5
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs

@ -14,7 +14,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
{
//TODO: Add dbcontext just like mongodb implementation!
public virtual List<TEntity> Collection => Database.Collection<TEntity>();
public virtual IMemoryDatabaseCollection<TEntity> Collection => Database.Collection<TEntity>();
public virtual IMemoryDatabase Database => DatabaseProvider.GetDatabase();
@ -33,6 +33,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
public override TEntity Update(TEntity entity, bool autoSave = false)
{
Collection.Update(entity);
return entity;
}
@ -48,7 +49,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
public override long GetCount()
{
return Collection.Count;
return Collection.Count();
}
protected override IQueryable<TEntity> GetQueryable()

2
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDbCoreRepositoryExtensions.cs

@ -13,7 +13,7 @@ namespace Volo.Abp.Domain.Repositories
return repository.ToMemoryDbRepository().Database;
}
public static List<TEntity> GetCollection<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository)
public static IMemoryDatabaseCollection<TEntity> GetCollection<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository)
where TEntity : class, IEntity<TKey>
{
return repository.ToMemoryDbRepository().Collection;

3
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/PeopleAppService.cs

@ -35,7 +35,7 @@ namespace Volo.Abp.TestApp.Application
var phone = new Phone(person.Id, phoneDto.Number, phoneDto.Type);
person.Phones.Add(phone);
Repository.Update(person);
return ObjectMapper.Map<Phone, PhoneDto>(phone);
}
@ -43,6 +43,7 @@ namespace Volo.Abp.TestApp.Application
{
var person = await GetEntityByIdAsync(id);
person.Phones.RemoveAll(p => p.Number == number);
Repository.Update(person);
}
public Task<GetWithComplexTypeInput> GetWithComplexType(GetWithComplexTypeInput input)

1
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/City.cs

@ -3,6 +3,7 @@ using Volo.Abp.Domain.Entities;
namespace Volo.Abp.TestApp.Domain
{
[Serializable]
public class City : AggregateRoot<Guid>
{
public string Name { get; set; }

1
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs

@ -7,6 +7,7 @@ using Volo.Abp.MultiTenancy;
namespace Volo.Abp.TestApp.Domain
{
[AutoMapTo(typeof(PersonEto))]
[Serializable]
public class Person : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
public virtual Guid? TenantId { get; set; }

1
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Phone.cs

@ -7,6 +7,7 @@ using Volo.Abp.Domain.Entities;
namespace Volo.Abp.TestApp.Domain
{
[Table("AppPhones")]
[Serializable]
public class Phone : Entity
{
public virtual Guid PersonId { get; set; }

Loading…
Cancel
Save