using System.Linq.Expressions; using ZeroFramework.DeviceCenter.Domain.Specifications; using ZeroFramework.DeviceCenter.Domain.UnitOfWork; namespace ZeroFramework.DeviceCenter.Domain.Repositories { public interface IRepository { /// /// Inserts a new entity. /// /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. /// Inserted entity Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default); /// /// Inserts multiple new entities. /// /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. /// Entities to be inserted. /// Awaitable . Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default); /// /// Updates an existing entity. /// /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. /// Entity Task UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default); /// /// Updates multiple entities. /// /// Entities to be updated. /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// A to observe while waiting for the task to complete. /// Awaitable . Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default); /// /// Deletes an entity. /// /// Entity to be deleted /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default); /// /// Deletes many entities by function. /// Notice that: All entities fits to given predicate are retrieved and deleted. /// This may cause major performance problems if there are too many entities with /// given predicate. /// /// A condition to filter entities /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. Task DeleteAsync(Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default); /// /// Deletes multiple entities. /// /// Entities to be deleted. /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. /// Awaitable . Task DeleteManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default); /// /// Gets a list of all the entities. /// /// Set true to include all children of this entity /// A to observe while waiting for the task to complete. /// Entity Task> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default); /// /// Gets total count of all entities. /// Task GetCountAsync(CancellationToken cancellationToken = default); /// /// Gets a list of paged the entities. /// Task> GetListAsync(int pageNumber, int pageSize, Expression> sorting, bool includeDetails = false, CancellationToken cancellationToken = default); #region Related Navigation Property /// /// Specifies related entities to include in the query results. The navigation property to be included is specified starting with the type of entity being queried. /// Task> IncludeRelatedAsync(params Expression>[] propertySelectors); /// /// Provides access to change tracking and loading information for a collection navigation property that associates this entity to a collection of another entities. /// Task LoadRelatedAsync(TEntity entity, Expression>> propertyExpression, CancellationToken cancellationToken = default) where TProperty : class; /// /// Provides access to change tracking and loading information for a reference (i.e. non-collection) navigation property that associates this entity to another entity. /// Task LoadRelatedAsync(TEntity entity, Expression> propertyExpression, CancellationToken cancellationToken = default) where TProperty : class; #endregion /// /// Get a single entity by the given . /// It returns null if no entity with the given . /// It throws if there are multiple entities with the given . /// /// A condition to find the entity /// Set true to include all children of this entity /// A to observe while waiting for the task to complete. Task FindAsync(Expression> predicate, bool includeDetails = true, CancellationToken cancellationToken = default); /// /// Get a single entity by the given . /// It throws if there is no entity with the given . /// It throws if there are multiple entities with the given . /// /// A condition to filter entities /// Set true to include all children of this entity /// A to observe while waiting for the task to complete. Task GetAsync(Expression> predicate, bool includeDetails = true, CancellationToken cancellationToken = default); #region Specification Pattern /// /// Filters the entities of , to those that match the encapsulated query logic of the /// . /// /// The encapsulated query logic. /// The filtered entities as an . IQueryable ApplySpecification(ISpecification specification); /// /// Filters all entities of , that matches the encapsulated query logic of the /// , from the database. /// /// Projects each entity into a new form, being . /// /// /// The type of the value returned by the projection. /// The encapsulated query logic. /// The filtered projected entities as an . IQueryable ApplySpecification(ISpecification specification); /// /// Gets a list of specification the entities. /// Task> GetListAsync(ISpecification specification, CancellationToken cancellationToken = default); /// /// Gets a list of specification the entities. /// Task> GetListAsync(ISpecification specification, CancellationToken cancellationToken = default); /// /// Gets total count of all entities. /// Task GetCountAsync(ISpecification specification, CancellationToken cancellationToken = default); /// /// Get a single entity by the given /// Task GetAsync(ISpecification specification, CancellationToken cancellationToken = default); /// /// Get a single entity by the given /// Task GetAsync(ISpecification specification, CancellationToken cancellationToken = default); #endregion /// /// Provides functionality to evaluate queries against a specific data source wherein the type of the data is known. /// IQueryable Query { get; } /// ///LINQ related extension methods. /// IAsyncQueryableProvider AsyncExecuter { get; } /// ///Using the Repository and Unit Of Work Pattern /// IUnitOfWork UnitOfWork { get; } } public interface IRepository : IRepository { /// /// Deletes an entity by primary key. /// /// Primary key of the entity /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default); //TODO: Return true if deleted /// /// Deletes multiple entities by primary keys. /// /// Primary keys of the each entity. /// /// Set true to automatically save changes to database. /// This is useful for ORMs / database APIs those only save changes with an explicit method call, but you need to immediately save changes to the database. /// /// A to observe while waiting for the task to complete. /// Awaitable . Task DeleteManyAsync(IEnumerable ids, bool autoSave = false, CancellationToken cancellationToken = default); /// /// Gets an entity with given primary key. /// Throws if can not find an entity with given id. /// /// Primary key of the entity to get /// Set true to include all children of this entity /// A to observe while waiting for the task to complete. /// Entity Task GetAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default); /// /// Gets an entity with given primary key or null if not found. /// /// Primary key of the entity to get /// Set true to include all children of this entity /// A to observe while waiting for the task to complete. /// Entity or null Task FindAsync(TKey id, bool includeDetails = true, CancellationToken cancellationToken = default); } }