/// <summary> /// Adds a number of entities to the system. /// </summary> /// <param name="entities">The entities to add.</param> /// <param name="db">The unit of work to operate against.</param> public virtual void Add(IEnumerable <TEntity> entities, IUnitOfWorkLinq <TEntity, TKey> db) { Guard.ArgumentNotNull(entities, nameof(entities)); Guard.ArgumentNotNull(db, nameof(db)); //avoid multiple enumerations var entitiesAsArray = entities.ToArray(); entitiesAsArray.Each(OnBeforeAdd); DemandValid(entitiesAsArray); // try assign domain id entitiesAsArray.Each(entity => { if (entity.Id.Equals(default(TKey))) { _idProvider.Provide(entity); //assign next key } }); Db.EntitySet.Writer.Insert(entitiesAsArray); // send domain events entitiesAsArray.Each(entity => { MessagePipe.SendAsync(new ItemAdded <TEntity>(entity)); // notify item add OnAfterItemAdded(entity); } ); }
/// <summary> /// Deletes an entity by its key. /// </summary> /// <param name="key">The id of the entity to delete.</param> /// <param name="db">The unit of work instance to work against.</param> public virtual void RemoveById(TKey key, IUnitOfWorkLinq <TEntity, TKey> db) { Guard.ArgumentNotNull(db, nameof(db)); // assume db session is open OnBeforeDelete(key); db.EntitySet.Writer.DeleteById(key); OnBeforeDeleteCommit(db, key); MessagePipe.SendAsync(new ItemDeleted <TEntity, TKey>(key)); }
/// <summary> /// Updates a given entity against a given unit of work. /// </summary> /// <remarks> /// Caller must control commits to the given unit of work. /// </remarks> /// <param name="entity">The entity to update.</param> /// <param name="db">The unit of work instance to work against.</param> public virtual void Update(TEntity entity, IUnitOfWorkLinq <TEntity, TKey> db) { Guard.ArgumentNotNull(entity, nameof(entity)); Guard.ArgumentNotNull(db, nameof(db)); //validate business logic DemandValid(entity); OnBeforeUpdate(entity); db.EntitySet.Writer.Update(entity); MessagePipe .SendAsync(new ItemUpdated <TEntity>(entity)); }
protected virtual void OnAfterItemUpdated(TEntity entity) { // todo MessagePipe.SendAsync(new ItemUpdated <TEntity>(entity)); }