IRepositoryOfTEntityAndTPrimaryKey.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Threading.Tasks;
  6. using Abp.Domain.Entities;
  7. using Abp.Domain.Uow;
  8. namespace Abp.Domain.Repositories
  9. {
  10. /// <summary>
  11. /// This interface is implemented by all repositories to ensure implementation of fixed methods.
  12. /// </summary>
  13. /// <typeparam name="TEntity">Main Entity type this repository works on</typeparam>
  14. /// <typeparam name="TPrimaryKey">Primary key type of the entity</typeparam>
  15. public interface IRepository<TEntity, TPrimaryKey> : IRepository where TEntity : class, IEntity<TPrimaryKey>
  16. {
  17. #region Select/Get/Query
  18. /// <summary>
  19. /// Used to get a IQueryable that is used to retrieve entities from entire table.
  20. /// </summary>
  21. /// <returns>IQueryable to be used to select entities from database</returns>
  22. IQueryable<TEntity> GetAll();
  23. /// <summary>
  24. /// Used to get a IQueryable that is used to retrieve entities from entire table.
  25. /// One or more
  26. /// </summary>
  27. /// <param name="propertySelectors">A list of include expressions.</param>
  28. /// <returns>IQueryable to be used to select entities from database</returns>
  29. IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors);
  30. /// <summary>
  31. /// Used to get all entities.
  32. /// </summary>
  33. /// <returns>List of all entities</returns>
  34. List<TEntity> GetAllList();
  35. /// <summary>
  36. /// Used to get all entities.
  37. /// </summary>
  38. /// <returns>List of all entities</returns>
  39. Task<List<TEntity>> GetAllListAsync();
  40. /// <summary>
  41. /// Used to get all entities based on given <paramref name="predicate"/>.
  42. /// </summary>
  43. /// <param name="predicate">A condition to filter entities</param>
  44. /// <returns>List of all entities</returns>
  45. List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate);
  46. /// <summary>
  47. /// Used to get all entities based on given <paramref name="predicate"/>.
  48. /// </summary>
  49. /// <param name="predicate">A condition to filter entities</param>
  50. /// <returns>List of all entities</returns>
  51. Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate);
  52. /// <summary>
  53. /// Used to run a query over entire entities.
  54. /// <see cref="UnitOfWorkAttribute"/> attribute is not always necessary (as opposite to <see cref="GetAll"/>)
  55. /// if <paramref name="queryMethod"/> finishes IQueryable with ToList, FirstOrDefault etc..
  56. /// </summary>
  57. /// <typeparam name="T">Type of return value of this method</typeparam>
  58. /// <param name="queryMethod">This method is used to query over entities</param>
  59. /// <returns>Query result</returns>
  60. T Query<T>(Func<IQueryable<TEntity>, T> queryMethod);
  61. /// <summary>
  62. /// Gets an entity with given primary key.
  63. /// </summary>
  64. /// <param name="id">Primary key of the entity to get</param>
  65. /// <returns>Entity</returns>
  66. TEntity Get(TPrimaryKey id);
  67. /// <summary>
  68. /// Gets an entity with given primary key.
  69. /// </summary>
  70. /// <param name="id">Primary key of the entity to get</param>
  71. /// <returns>Entity</returns>
  72. Task<TEntity> GetAsync(TPrimaryKey id);
  73. /// <summary>
  74. /// Gets exactly one entity with given predicate.
  75. /// Throws exception if no entity or more than one entity.
  76. /// </summary>
  77. /// <param name="predicate">Entity</param>
  78. TEntity Single(Expression<Func<TEntity, bool>> predicate);
  79. /// <summary>
  80. /// Gets exactly one entity with given predicate.
  81. /// Throws exception if no entity or more than one entity.
  82. /// </summary>
  83. /// <param name="predicate">Entity</param>
  84. Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate);
  85. /// <summary>
  86. /// Gets an entity with given primary key or null if not found.
  87. /// </summary>
  88. /// <param name="id">Primary key of the entity to get</param>
  89. /// <returns>Entity or null</returns>
  90. TEntity FirstOrDefault(TPrimaryKey id);
  91. /// <summary>
  92. /// Gets an entity with given primary key or null if not found.
  93. /// </summary>
  94. /// <param name="id">Primary key of the entity to get</param>
  95. /// <returns>Entity or null</returns>
  96. Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id);
  97. /// <summary>
  98. /// Gets an entity with given given predicate or null if not found.
  99. /// </summary>
  100. /// <param name="predicate">Predicate to filter entities</param>
  101. TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
  102. /// <summary>
  103. /// Gets an entity with given given predicate or null if not found.
  104. /// </summary>
  105. /// <param name="predicate">Predicate to filter entities</param>
  106. Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
  107. /// <summary>
  108. /// Creates an entity with given primary key without database access.
  109. /// </summary>
  110. /// <param name="id">Primary key of the entity to load</param>
  111. /// <returns>Entity</returns>
  112. TEntity Load(TPrimaryKey id);
  113. #endregion
  114. #region Insert
  115. /// <summary>
  116. /// Inserts a new entity.
  117. /// </summary>
  118. /// <param name="entity">Inserted entity</param>
  119. TEntity Insert(TEntity entity);
  120. /// <summary>
  121. /// Inserts a new entity.
  122. /// </summary>
  123. /// <param name="entity">Inserted entity</param>
  124. Task<TEntity> InsertAsync(TEntity entity);
  125. /// <summary>
  126. /// Inserts a new entity and gets it's Id.
  127. /// It may require to save current unit of work
  128. /// to be able to retrieve id.
  129. /// </summary>
  130. /// <param name="entity">Entity</param>
  131. /// <returns>Id of the entity</returns>
  132. TPrimaryKey InsertAndGetId(TEntity entity);
  133. /// <summary>
  134. /// Inserts a new entity and gets it's Id.
  135. /// It may require to save current unit of work
  136. /// to be able to retrieve id.
  137. /// </summary>
  138. /// <param name="entity">Entity</param>
  139. /// <returns>Id of the entity</returns>
  140. Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity);
  141. /// <summary>
  142. /// Inserts or updates given entity depending on Id's value.
  143. /// </summary>
  144. /// <param name="entity">Entity</param>
  145. TEntity InsertOrUpdate(TEntity entity);
  146. /// <summary>
  147. /// Inserts or updates given entity depending on Id's value.
  148. /// </summary>
  149. /// <param name="entity">Entity</param>
  150. Task<TEntity> InsertOrUpdateAsync(TEntity entity);
  151. /// <summary>
  152. /// Inserts or updates given entity depending on Id's value.
  153. /// Also returns Id of the entity.
  154. /// It may require to save current unit of work
  155. /// to be able to retrieve id.
  156. /// </summary>
  157. /// <param name="entity">Entity</param>
  158. /// <returns>Id of the entity</returns>
  159. TPrimaryKey InsertOrUpdateAndGetId(TEntity entity);
  160. /// <summary>
  161. /// Inserts or updates given entity depending on Id's value.
  162. /// Also returns Id of the entity.
  163. /// It may require to save current unit of work
  164. /// to be able to retrieve id.
  165. /// </summary>
  166. /// <param name="entity">Entity</param>
  167. /// <returns>Id of the entity</returns>
  168. Task<TPrimaryKey> InsertOrUpdateAndGetIdAsync(TEntity entity);
  169. #endregion
  170. #region Update
  171. /// <summary>
  172. /// Updates an existing entity.
  173. /// </summary>
  174. /// <param name="entity">Entity</param>
  175. TEntity Update(TEntity entity);
  176. /// <summary>
  177. /// Updates an existing entity.
  178. /// </summary>
  179. /// <param name="entity">Entity</param>
  180. Task<TEntity> UpdateAsync(TEntity entity);
  181. /// <summary>
  182. /// Updates an existing entity.
  183. /// </summary>
  184. /// <param name="id">Id of the entity</param>
  185. /// <param name="updateAction">Action that can be used to change values of the entity</param>
  186. /// <returns>Updated entity</returns>
  187. TEntity Update(TPrimaryKey id, Action<TEntity> updateAction);
  188. /// <summary>
  189. /// Updates an existing entity.
  190. /// </summary>
  191. /// <param name="id">Id of the entity</param>
  192. /// <param name="updateAction">Action that can be used to change values of the entity</param>
  193. /// <returns>Updated entity</returns>
  194. Task<TEntity> UpdateAsync(TPrimaryKey id, Func<TEntity, Task> updateAction);
  195. #endregion
  196. #region Delete
  197. /// <summary>
  198. /// Deletes an entity.
  199. /// </summary>
  200. /// <param name="entity">Entity to be deleted</param>
  201. void Delete(TEntity entity);
  202. /// <summary>
  203. /// Deletes an entity.
  204. /// </summary>
  205. /// <param name="entity">Entity to be deleted</param>
  206. Task DeleteAsync(TEntity entity);
  207. /// <summary>
  208. /// Deletes an entity by primary key.
  209. /// </summary>
  210. /// <param name="id">Primary key of the entity</param>
  211. void Delete(TPrimaryKey id);
  212. /// <summary>
  213. /// Deletes an entity by primary key.
  214. /// </summary>
  215. /// <param name="id">Primary key of the entity</param>
  216. Task DeleteAsync(TPrimaryKey id);
  217. /// <summary>
  218. /// Deletes many entities by function.
  219. /// Notice that: All entities fits to given predicate are retrieved and deleted.
  220. /// This may cause major performance problems if there are too many entities with
  221. /// given predicate.
  222. /// </summary>
  223. /// <param name="predicate">A condition to filter entities</param>
  224. void Delete(Expression<Func<TEntity, bool>> predicate);
  225. /// <summary>
  226. /// Deletes many entities by function.
  227. /// Notice that: All entities fits to given predicate are retrieved and deleted.
  228. /// This may cause major performance problems if there are too many entities with
  229. /// given predicate.
  230. /// </summary>
  231. /// <param name="predicate">A condition to filter entities</param>
  232. Task DeleteAsync(Expression<Func<TEntity, bool>> predicate);
  233. #endregion
  234. #region Aggregates
  235. /// <summary>
  236. /// Gets count of all entities in this repository.
  237. /// </summary>
  238. /// <returns>Count of entities</returns>
  239. int Count();
  240. /// <summary>
  241. /// Gets count of all entities in this repository.
  242. /// </summary>
  243. /// <returns>Count of entities</returns>
  244. Task<int> CountAsync();
  245. /// <summary>
  246. /// Gets count of all entities in this repository based on given <paramref name="predicate"/>.
  247. /// </summary>
  248. /// <param name="predicate">A method to filter count</param>
  249. /// <returns>Count of entities</returns>
  250. int Count(Expression<Func<TEntity, bool>> predicate);
  251. /// <summary>
  252. /// Gets count of all entities in this repository based on given <paramref name="predicate"/>.
  253. /// </summary>
  254. /// <param name="predicate">A method to filter count</param>
  255. /// <returns>Count of entities</returns>
  256. Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate);
  257. /// <summary>
  258. /// Gets count of all entities in this repository (use if expected return value is greather than <see cref="int.MaxValue"/>.
  259. /// </summary>
  260. /// <returns>Count of entities</returns>
  261. long LongCount();
  262. /// <summary>
  263. /// Gets count of all entities in this repository (use if expected return value is greather than <see cref="int.MaxValue"/>.
  264. /// </summary>
  265. /// <returns>Count of entities</returns>
  266. Task<long> LongCountAsync();
  267. /// <summary>
  268. /// Gets count of all entities in this repository based on given <paramref name="predicate"/>
  269. /// (use this overload if expected return value is greather than <see cref="int.MaxValue"/>).
  270. /// </summary>
  271. /// <param name="predicate">A method to filter count</param>
  272. /// <returns>Count of entities</returns>
  273. long LongCount(Expression<Func<TEntity, bool>> predicate);
  274. /// <summary>
  275. /// Gets count of all entities in this repository based on given <paramref name="predicate"/>
  276. /// (use this overload if expected return value is greather than <see cref="int.MaxValue"/>).
  277. /// </summary>
  278. /// <param name="predicate">A method to filter count</param>
  279. /// <returns>Count of entities</returns>
  280. Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate);
  281. #endregion
  282. }
  283. }