using System.Collections.Generic; using System.Linq; using Abp.Domain.Entities; using Abp.Domain.Repositories; namespace Abp.MemoryDb.Repositories { //TODO: Implement thread-safety..? public class MemoryRepository : AbpRepositoryBase where TEntity : class, IEntity { private readonly IMemoryDatabaseProvider _databaseProvider; protected MemoryDatabase Database { get { return _databaseProvider.Database; } } protected List Table { get { return Database.Set(); } } private readonly MemoryPrimaryKeyGenerator _primaryKeyGenerator; public MemoryRepository(IMemoryDatabaseProvider databaseProvider) { _databaseProvider = databaseProvider; _primaryKeyGenerator = new MemoryPrimaryKeyGenerator(); } public override IQueryable GetAll() { return Table.AsQueryable(); } public override TEntity Insert(TEntity entity) { if (entity.IsTransient()) { entity.Id = _primaryKeyGenerator.GetNext(); } Table.Add(entity); return entity; } public override TEntity Update(TEntity entity) { var index = Table.FindIndex(e => EqualityComparer.Default.Equals(e.Id, entity.Id)); if (index >= 0) { Table[index] = entity; } return entity; } public override void Delete(TEntity entity) { Delete(entity.Id); } public override void Delete(TPrimaryKey id) { var index = Table.FindIndex(e => EqualityComparer.Default.Equals(e.Id, id)); if (index >= 0) { Table.RemoveAt(index); } } } }