Created
December 22, 2019 16:44
-
-
Save acraven/9028d5e6cee781d51b0cf380286b0fe0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Threading.Tasks; | |
using Microservice.Components; | |
using Microservice.Model; | |
namespace Microservice.Dependencies.ObjectStore.InMemory | |
{ | |
public class InMemoryAggregateStore : IAggregateStore | |
{ | |
private readonly IList<IAggregate> _aggregates = new List<IAggregate>(); | |
private readonly ITenantAccessor _tenantAccessor; | |
public InMemoryAggregateStore(ITenantAccessor tenantAccessor) | |
{ | |
_tenantAccessor = tenantAccessor; | |
} | |
public Task AddAsync<TAggregate>(TAggregate aggregate) where TAggregate : IAggregate | |
{ | |
aggregate.ID = Guid.NewGuid(); | |
aggregate.TenantID = _tenantAccessor.TenantID; | |
_aggregates.Add(aggregate); | |
return Task.CompletedTask; | |
} | |
public Task<TAggregate> GetAsync<TAggregate>(Guid id) where TAggregate : IAggregate | |
{ | |
return Task.FromResult(ItemsOf<TAggregate>().SingleOrDefault(c => c.ID == id)); | |
} | |
public Task<IList<TAggregate>> GetAllAsync<TAggregate>() where TAggregate : IAggregate | |
{ | |
return Task.FromResult<IList<TAggregate>>(ItemsOf<TAggregate>().ToList()); | |
} | |
private IEnumerable<TAggregate> ItemsOf<TAggregate>() where TAggregate : IAggregate | |
{ | |
return _aggregates.OfType<TAggregate>().Where(c => c.TenantID == _tenantAccessor.TenantID); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment