public async Task InsertAsync(T entity) { var changeListeners = new List <Action <EntityChange <T> > >(); T internalEntity; using (await Lock.WriterLockAsync()) { if (string.IsNullOrEmpty(entity.Id)) { entity.Id = ObjectId.GenerateNewId().ToString(); } if (Entities.TryGetValue(entity.Id, out T otherEntity)) { throw new KeyAlreadyExistsException("An entity with the same identifier already exists.") { Entity = otherEntity }; } OnBeforeEntityChanged(EntityChangeType.Insert, entity); internalEntity = entity.Clone(); Entities.Add(entity.Id, internalEntity); OnEntityChanged(EntityChangeType.Insert, null, internalEntity, changeListeners); if (PersistenceRepository != null) { await PersistenceRepository.InsertAsync(entity); } } SendToSubscribers(changeListeners, EntityChangeType.Insert, internalEntity); }
public async Task UpdateAsync(T entity, bool checkConflict = false) { var changeListeners = new List <Action <EntityChange <T> > >(); T internalEntity; using (await Lock.WriterLockAsync()) { OnBeforeEntityChanged(EntityChangeType.Update, entity); if (checkConflict) { CheckForConcurrencyConflict(entity); } entity.Revision++; internalEntity = entity.Clone(); T oldEntity = Entities[entity.Id]; Entities[entity.Id] = internalEntity; if (_changeListeners.TryGetValue(entity.Id, out ISet <Action <EntityChange <T> > > listeners)) { changeListeners.AddRange(listeners); } OnEntityChanged(EntityChangeType.Update, oldEntity, internalEntity, changeListeners); if (PersistenceRepository != null) { await PersistenceRepository.UpdateAsync(entity); } } SendToSubscribers(changeListeners, EntityChangeType.Update, internalEntity); }
public async Task InsertAsync(T entity, CancellationToken ct = default(CancellationToken)) { var allSubscriptions = new List <Subscription <T> >(); T internalEntity; using (await Lock.WriterLockAsync(ct)) { if (string.IsNullOrEmpty(entity.Id)) { entity.Id = ObjectId.GenerateNewId().ToString(); } if (Entities.ContainsKey(entity.Id)) { throw new KeyAlreadyExistsException("An entity with the same identifier already exists."); } OnBeforeEntityChanged(EntityChangeType.Insert, entity); internalEntity = entity.Clone(); Entities.Add(entity.Id, internalEntity); OnEntityChanged(EntityChangeType.Insert, null, internalEntity, allSubscriptions); if (PersistenceRepository != null) { await PersistenceRepository.InsertAsync(entity, ct); } } SendToSubscribers(allSubscriptions, EntityChangeType.Insert, internalEntity); }
public async Task UpdateAsync(T entity, bool checkConflict = false, CancellationToken ct = default(CancellationToken)) { var allSubscriptions = new List <Subscription <T> >(); T internalEntity; using (await Lock.WriterLockAsync(ct)) { OnBeforeEntityChanged(EntityChangeType.Update, entity); if (checkConflict) { CheckForConcurrencyConflict(entity); } entity.Revision++; internalEntity = entity.Clone(); T oldEntity = Entities[entity.Id]; Entities[entity.Id] = internalEntity; GetIdSubscriptions(entity.Id, allSubscriptions); OnEntityChanged(EntityChangeType.Update, oldEntity, internalEntity, allSubscriptions); if (PersistenceRepository != null) { await PersistenceRepository.UpdateAsync(entity, false, ct); } } SendToSubscribers(allSubscriptions, EntityChangeType.Update, internalEntity); }
public override void Init() { base.Init(); if (PersistenceRepository != null) { _engineIdIndex.PopulateIndex(PersistenceRepository.GetAllAsync().WaitAndUnwrapException()); } }
public override async Task InitAsync() { await base.InitAsync(); if (PersistenceRepository != null) { _engineIdIndex.PopulateIndex(await PersistenceRepository.GetAllAsync()); } }
public virtual void Init() { if (PersistenceRepository != null) { PersistenceRepository.Init(); foreach (T entity in PersistenceRepository.GetAllAsync().WaitAndUnwrapException()) { Entities[entity.Id] = entity; } } }
public virtual async Task InitAsync() { if (PersistenceRepository != null) { await PersistenceRepository.InitAsync(); foreach (T entity in await PersistenceRepository.GetAllAsync()) { Entities[entity.Id] = entity; } } }
public async Task DeleteAsync(string id) { var changeListeners = new List <Action <EntityChange <T> > >(); T internalEntity; using (await Lock.WriterLockAsync()) { internalEntity = DeleteEntity(id, changeListeners); if (PersistenceRepository != null) { await PersistenceRepository.DeleteAsync(id); } } SendToSubscribers(changeListeners, EntityChangeType.Delete, internalEntity); }
public async Task DeleteAsync(string id, CancellationToken ct = default(CancellationToken)) { var allSubscriptions = new List <Subscription <T> >(); T internalEntity; using (await Lock.WriterLockAsync(ct)) { internalEntity = DeleteEntity(id, allSubscriptions); if (PersistenceRepository != null) { await PersistenceRepository.DeleteAsync(id, ct); } } if (internalEntity != null) { SendToSubscribers(allSubscriptions, EntityChangeType.Delete, internalEntity); } }
public async Task DeleteAsync(T entity, bool checkConflict = false) { var changeListeners = new List <Action <EntityChange <T> > >(); T internalEntity; using (await Lock.WriterLockAsync()) { if (checkConflict) { CheckForConcurrencyConflict(entity); } internalEntity = DeleteEntity(entity.Id, changeListeners); if (PersistenceRepository != null) { await PersistenceRepository.DeleteAsync(entity); } } SendToSubscribers(changeListeners, EntityChangeType.Delete, internalEntity); }
public async Task DeleteAsync(T entity, bool checkConflict = false, CancellationToken ct = default(CancellationToken)) { var allSubscriptions = new List <Subscription <T> >(); T internalEntity; using (await Lock.WriterLockAsync(ct)) { if (checkConflict) { CheckForConcurrencyConflict(entity); } internalEntity = DeleteEntity(entity.Id, allSubscriptions); if (PersistenceRepository != null) { await PersistenceRepository.DeleteAsync(entity, false, ct); } } SendToSubscribers(allSubscriptions, EntityChangeType.Delete, internalEntity); }
public async Task DeleteAllByEngineIdAsync(string engineId, CancellationToken ct = default(CancellationToken)) { var deletedBuilds = new List <(Build, List <Subscription <Build> >)>(); using (await Lock.WriterLockAsync(ct)) { foreach (string buildId in Entities.Values.Where(e => e.EngineRef == engineId).Select(e => e.Id) .ToArray()) { var allSubscriptions = new List <Subscription <Build> >(); Build internalBuild = DeleteEntity(buildId, allSubscriptions); if (PersistenceRepository != null) { await PersistenceRepository.DeleteAsync(buildId, ct); } deletedBuilds.Add((internalBuild, allSubscriptions)); } } foreach ((Build internalBuild, List <Subscription <Build> > allSubscriptions) in deletedBuilds) { SendToSubscribers(allSubscriptions, EntityChangeType.Delete, internalBuild); } }