/// <summary> /// Gets all instances of an entity against a given unit of work. /// </summary> public virtual IEnumerable <TEntity> GetAll(IUnitOfWorkLinq <TEntity, TKey> db) { Guard.ArgumentNotNull(db, nameof(db)); //convert to hashset to make unique return(OnMaterialized(Db.EntitySet.Reader.GetAll())); }
/// <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> /// Gets a set of entities by a set of keys against an open unit of work. /// </summary> public IEnumerable <TEntity> GetByIds(IEnumerable <TKey> keys, IUnitOfWorkLinq <TEntity, TKey> db) { Guard.ArgumentNotNull(keys, nameof(keys)); Guard.ArgumentNotNull(db, nameof(db)); //convert to hashset to make unique return(OnMaterialized(db.EntitySet.Reader.GetByIds(keys.ToHashSetSafe()))); }
/// <summary> /// Creates a new instance. /// </summary> public CapabilityProvider( IUnitOfWorkLinq <Capability, Guid> db, IEntityIdProvider <Capability, Guid> keyBinder, IMessagePipe messagePipe = null, IProvideSpecifications <Capability> specProvider = null, EntityHandler <Capability, Guid> entityHandler = null) : base(db, keyBinder, messagePipe, specProvider, entityHandler) { }
public ProtocolTypeProvider( IUnitOfWorkLinq <ProtocolType, string> db, IEntityIdProvider <ProtocolType, string> idProvider, IMessagePipe messagePipe = null, IProvideSpecifications <ProtocolType> specProvider = null, EntityHandler <ProtocolType, string> entityHandler = null) : base(db, idProvider, messagePipe, specProvider, entityHandler) { }
public StakeholderLoginProvider( IUnitOfWorkLinq <StakeholderLogin, Guid> db, IEntityIdProvider <StakeholderLogin, Guid> keyBinder, IMessagePipe messagePipe = null, IProvideSpecifications <StakeholderLogin> specProvider = null, EntityHandler <StakeholderLogin, Guid> entityHandler = null) : base(db, keyBinder, messagePipe, specProvider, entityHandler) { }
public BusinessUnitProvider( IUnitOfWorkLinq <BusinessUnit, Guid> db, IEntityIdProvider <BusinessUnit, Guid> keyBinder, IMessagePipe messagePipe = null, IProvideSpecifications <BusinessUnit> specProvider = null, EntityHandler <BusinessUnit, Guid> entityHandler = null) : base(db, keyBinder, messagePipe, specProvider, entityHandler) { }
public LifeCycleProvider( IUnitOfWorkLinq <LifeCycle, string> db, IEntityIdProvider <LifeCycle, string> keyBinder, IMessagePipe messagePipe = null, IProvideSpecifications <LifeCycle> specProvider = null, EntityHandler <LifeCycle, string> onBeforeInsert = null) : base(db, keyBinder, messagePipe, specProvider, onBeforeInsert) { }
/// <summary> /// Creates a new instance. /// </summary> public ScenarioProvider( IUnitOfWorkLinq <Scenario, Guid> db, IEntityIdProvider <Scenario, Guid> keyBinder, IMessagePipe messagePipe = null, IProvideSpecifications <Scenario> specProvider = null, EntityHandler <Scenario, Guid> entityHandler = null) : base(db, keyBinder, messagePipe, specProvider, entityHandler) { }
public Policy GetContainingPolicy(Policy policy, IUnitOfWorkLinq <Policy, Guid> db) { if (!policy.ContainerId.HasValue) { return(null); } return(GetById(policy.ContainerId.Value, db)); }
/// <summary> /// Gets an entity by its primary key from a given unit of work. /// </summary> public TEntity GetById(TKey key, IUnitOfWorkLinq <TEntity, TKey> db) { //convert to hashset to make unique var entity = db.EntitySet.Reader.Get(key); if (entity != null) { Initialize(entity); } return(entity); }
/// <summary> /// Creates a new instance. /// </summary> /// <param name="db">The references database.</param> /// <param name="keyBinder"></param> /// <param name="messagePipe"></param> /// <param name="referencesSpec">Rules to validate a given reference entity.</param> public ReferenceProvider( IUnitOfWorkLinq <Reference, Guid> db, IEntityIdProvider <Reference, Guid> keyBinder, IMessagePipe messagePipe, IProvideSpecifications <Reference> referencesSpec = null, EntityHandler <Reference, Guid> entityHandler = null) : base(db, keyBinder, messagePipe, referencesSpec, entityHandler) { Guard.ArgumentNotNull(db, nameof(db)); _db = db; }
public PolicyProvider( IUnitOfWorkLinq <Policy, Guid> db, ReferenceProvider referencesProvider, IEntityIdProvider <Policy, Guid> idProvider, IMessagePipe messagePipe = null, IProvideSpecifications <Policy> specProvider = null, EntityHandler <Policy, Guid> entityHandler = null) : base(db, idProvider, messagePipe, specProvider, entityHandler) { _referencesProvider = referencesProvider; _db = db; }
/// <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> /// Creates a new generic service to add/update valid entities and/or remove them from the application. /// </summary> /// <param name="messagePipe">Message pipe to dispatch domain events.</param> /// <param name="db">The data store to use to persist the entity.</param> /// <param name="idProvider">Function to get a new key for a given entity.</param> /// <param name="specProvider"> /// The custom rule provider to use to validate entities added/updated to the service. By /// default service will use data annotations to build business rules to validate the object. /// </param> /// <param name="handler">A handler to use to process entities being added/removed or activated through the provider.</param> public ProviderLinq( IUnitOfWorkLinq <TEntity, TKey> db, IKeyProvider <TEntity, TKey> idProvider, IMessagePipe messagePipe = null, IProvideSpecifications <TEntity> specProvider = null, EntityHandler <TEntity, TKey> handler = null) { Guard.ArgumentNotNull(db, nameof(db)); Guard.ArgumentNotNull(messagePipe, nameof(messagePipe)); Db = db; MessagePipe = messagePipe ?? new NoOpMessagePipe(); _idProvider = idProvider; // wrap processors // ---------------------------------------------------- _onBeforeDelete = new Stack <Action <TKey> >(); _onBeforeInsert = new Stack <Action <TEntity> >(); _onInitialize = new Stack <Action <TEntity> >(); if (handler != null) { if (handler.RemoveHandler != null) { OnRemoving(handler.RemoveHandler.Handle); } if (handler.AddHandler != null) { OnAdding(handler.AddHandler.Handle); } if (handler.ActivateHandler != null) { OnInitializing(handler.ActivateHandler.Handle); } } // --------------------------------------------------- //assign specs // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression if (specProvider != null) { _specs = specProvider.GetSpecifications().ToList(); } else { _specs = _defaultSpecs; } }
/// <summary> /// Removes a set of entities matching a given set of criteria. /// </summary> /// <param name="criteria">The criteria to use to delete.</param> /// <param name="db">The unit of work to remove the entities matching the expression from.</param> public void Remove(Expression <Func <TEntity, bool> > criteria, IUnitOfWorkLinq <TEntity, TKey> db) { Guard.ArgumentNotNull(db, nameof(db)); Guard.ArgumentNotNull(criteria, nameof(criteria)); // todo: optimize with projections var ids = db.EntitySet.LinqReader.Where(criteria).Select(m => m.Id).ToCollection(); ids.Each(OnBeforeDelete); db.EntitySet.BulkDeleter.Delete(criteria); // TODO: send message via batch ids.Each(m => OnBeforeDeleteCommit(db, m)); }
/// <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)); }
/// <summary> /// Selects a set of entities by a given expression. /// </summary> public IEnumerable <TEntity> Where(Expression <Func <TEntity, bool> > whereClause, IUnitOfWorkLinq <TEntity, TKey> db) { Guard.ArgumentNotNull(whereClause, nameof(whereClause)); IList <TEntity> entities; entities = db.EntitySet.LinqReader.Where(whereClause).ToList(); foreach (var item in entities) { Initialize(item); } return(entities); }
public FsService( FSSecurityContext securityContext, ReferenceProvider referenceProvider, IUnitOfWorkLinq <TEntity, Guid> db, IEntityIdProvider <TEntity, Guid> idProvider, IMessagePipe messagePipe = null, IProvideSpecifications <TEntity> specProvider = null, EntityHandler <TEntity, Guid> entityHandler = null) : base(db, idProvider, messagePipe, specProvider, entityHandler) { Guard.ArgumentNotNull(db, nameof(db)); Guard.ArgumentNotNull(referenceProvider, nameof(referenceProvider)); Guard.ArgumentNotNull(securityContext, nameof(securityContext)); _securityContext = securityContext; this.referenceProvider = referenceProvider; }
/// <summary> /// Update a batch of entities. /// </summary> /// <param name="entities">The entities to update.</param> public virtual void Update(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(OnBeforeUpdate); DemandValid(entitiesAsArray); Db.EntitySet.Writer.Update(entitiesAsArray); // send domain events entitiesAsArray.Each(OnAfterItemUpdated); }
/// <summary> /// Removes all references associated to a given entity. /// </summary> public void RemoveReferences(Guid referenceId, IUnitOfWorkLinq <Reference, Guid> db) { db.EntitySet.BulkDeleter.Delete(m => m.ReferenceId == referenceId); }
/// <summary> /// Removes all memberships a given stakeholder may have. /// </summary> public void RemoveGroupMemberships(Stakeholder stakeholder, IUnitOfWorkLinq <UserGroupMembership, Guid> db) { db.EntitySet.BulkDeleter.Delete(m => m.ScenarioId == stakeholder.ScenarioId && m.MemberId == stakeholder.Id); }
protected virtual void OnBeforeDeleteCommit(IUnitOfWorkLinq <TEntity, TKey> db, TKey key) { }
/// <summary> /// Deletes all scenarios associated with a given project by its key and defers commiting changes to the caller. /// </summary> public void DeleteByProject(Guid projectId, IUnitOfWorkLinq <Scenario, Guid> db) { db.EntitySet.BulkDeleter.Delete(m => m.ProjectId == projectId); }