From c2bd61b72d5e0f8f6dfc90657c2f1c79e038f64a Mon Sep 17 00:00:00 2001 From: Sergei Calabonga Date: Tue, 3 Dec 2024 11:11:55 +0700 Subject: [PATCH] LastSaveChagesResult renamed --- .../Calabonga.UnitOfWork.csproj | 4 +- src/Calabonga.UnitOfWork/IRepository.cs | 330 +++++++- src/Calabonga.UnitOfWork/IRepositoryV2.cs | 340 -------- src/Calabonga.UnitOfWork/Repository.cs | 733 ++++++++++++++++- src/Calabonga.UnitOfWork/RepositoryV2.cs | 746 ------------------ src/Calabonga.UnitOfWork/SaveChangesResult.cs | 2 +- 6 files changed, 1060 insertions(+), 1095 deletions(-) delete mode 100644 src/Calabonga.UnitOfWork/IRepositoryV2.cs delete mode 100644 src/Calabonga.UnitOfWork/RepositoryV2.cs diff --git a/src/Calabonga.UnitOfWork/Calabonga.UnitOfWork.csproj b/src/Calabonga.UnitOfWork/Calabonga.UnitOfWork.csproj index 3d53e40..e422b86 100644 --- a/src/Calabonga.UnitOfWork/Calabonga.UnitOfWork.csproj +++ b/src/Calabonga.UnitOfWork/Calabonga.UnitOfWork.csproj @@ -2,14 +2,14 @@ net8.0;net9.0 - 6.0.0-beta.1 + 6.0.0-beta.2 Calabonga Calabonga SOFT Calabonga SOFT © 2001-$([System.DateTime]::Now.ToString(yyyy)) Unit of Work implementation for EntityFramework Core. For more information please see Calabonga.UnitOfWork package. MIT logo.png - Calabonga EntityFrameworkCore UnitOfWork Repository Extenstion Helper unitofowrk ORM pagination PagedList pattern + Calabonga EntityFrameworkCore UnitOfWork Repository Extension Helper unitofowrk ORM pagination pattern changes Tracking entites Obsolete methods were removed. Please use TrackingType parameter for DbContext operations: NoTraking, Tracking and NoTrackingWithIdentityResolution. true https://www.calabonga.net diff --git a/src/Calabonga.UnitOfWork/IRepository.cs b/src/Calabonga.UnitOfWork/IRepository.cs index bb0ccbf..434a21f 100644 --- a/src/Calabonga.UnitOfWork/IRepository.cs +++ b/src/Calabonga.UnitOfWork/IRepository.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using Calabonga.PagedListCore; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Query; using System; @@ -14,10 +15,8 @@ namespace Calabonga.UnitOfWork; /// Defines the interfaces for generic repository. /// /// The type of the entity. -public partial interface IRepository where TEntity : class +public interface IRepository where TEntity : class { - - #region Find /// @@ -386,4 +385,327 @@ Task SumAsync( IQueryable FromSql(string sql, params object[] parameters); #endregion + + #region GetPagedList + + /// + /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query.. This method default no-tracking query. + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// The index of page. + /// The size of the page. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method default no-tracking query. + IPagedList GetPagedList( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + int pageIndex = 0, + int pageSize = 20, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + /// + /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// The index of page. + /// The size of the page. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// + /// A to observe while waiting for the task to complete. + /// + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method default no-tracking query. + Task> GetPagedListAsync( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + int pageIndex = 0, + int pageSize = 20, + TrackingType trackingType = TrackingType.NoTracking, + CancellationToken cancellationToken = default, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + /// + /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// The index of page. + /// The size of the page. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method default no-tracking query. + IPagedList GetPagedList( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + int pageIndex = 0, + int pageSize = 20, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) where TResult : class; + + /// + /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// The index of page. + /// The size of the page. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// + /// A to observe while waiting for the task to complete. + /// + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method default no-tracking query. + Task> GetPagedListAsync( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + int pageIndex = 0, + int pageSize = 20, + TrackingType trackingType = TrackingType.NoTracking, + CancellationToken cancellationToken = default, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) where TResult : class; + + #endregion + + #region GetFirstOrDefault + + /// + /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method defaults to a read-only, no-tracking query. + TEntity? GetFirstOrDefault( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + /// + /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method defaults to a read-only, no-tracking query. + TResult? GetFirstOrDefault( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + /// + /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + Task GetFirstOrDefaultAsync( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + /// + /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + Task GetFirstOrDefaultAsync + ( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + #endregion + + #region GetAll + + /// + /// Gets all entities. This method is not recommended + /// + /// The . + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + IQueryable GetAll(TrackingType trackingType = TrackingType.NoTracking); + + /// + /// Gets all entities. This method is not recommended + /// + /// The selector for projection. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// The . + IQueryable GetAll( + Expression> selector, + TrackingType trackingType = TrackingType.NoTracking); + + /// + /// Gets all entities. This method is not recommended + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// The . + IQueryable GetAll( + Expression> selector, + Expression>? predicate = null, + TrackingType trackingType = TrackingType.NoTracking); + + /// + /// Gets all entities. This method is not recommended + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + IQueryable GetAll( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + /// + /// Gets all entities. This method is not recommended + /// + /// A function to test each element for a condition. + /// The selector for projection. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + IQueryable GetAll( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + /// + /// Gets all entities. This method is not recommended + /// + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// The . + Task> GetAllAsync(TrackingType trackingType = TrackingType.NoTracking); + + /// + /// Gets all entities. This method is not recommended + /// + /// The selector for projection. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// The . + Task> GetAllAsync( + Expression> selector, + TrackingType trackingType = TrackingType.NoTracking); + + /// + /// Gets all entities. This method is not recommended + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + Task> GetAllAsync( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + /// + /// Gets all entities. This method is not recommended + /// + /// A function to test each element for a condition. + /// The selector for projection. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + Task> GetAllAsync( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false); + + #endregion } \ No newline at end of file diff --git a/src/Calabonga.UnitOfWork/IRepositoryV2.cs b/src/Calabonga.UnitOfWork/IRepositoryV2.cs deleted file mode 100644 index 89a159d..0000000 --- a/src/Calabonga.UnitOfWork/IRepositoryV2.cs +++ /dev/null @@ -1,340 +0,0 @@ -using Calabonga.PagedListCore; -using Microsoft.EntityFrameworkCore.Query; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; - -namespace Calabonga.UnitOfWork; - -/// -/// Defines the interfaces for generic repository. -/// -/// The type of the entity. -public partial interface IRepository where TEntity : class -{ - #region GetPagedList - - /// - /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query.. This method default no-tracking query. - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// The index of page. - /// The size of the page. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method default no-tracking query. - IPagedList GetPagedList( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - int pageIndex = 0, - int pageSize = 20, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - /// - /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// The index of page. - /// The size of the page. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// - /// A to observe while waiting for the task to complete. - /// - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method default no-tracking query. - Task> GetPagedListAsync( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - int pageIndex = 0, - int pageSize = 20, - TrackingType trackingType = TrackingType.NoTracking, - CancellationToken cancellationToken = default, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - /// - /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// The index of page. - /// The size of the page. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method default no-tracking query. - IPagedList GetPagedList( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - int pageIndex = 0, - int pageSize = 20, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) where TResult : class; - - /// - /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// The index of page. - /// The size of the page. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// - /// A to observe while waiting for the task to complete. - /// - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method default no-tracking query. - Task> GetPagedListAsync( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - int pageIndex = 0, - int pageSize = 20, - TrackingType trackingType = TrackingType.NoTracking, - CancellationToken cancellationToken = default, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) where TResult : class; - - #endregion - - #region GetFirstOrDefault - - /// - /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method defaults to a read-only, no-tracking query. - TEntity? GetFirstOrDefault( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - /// - /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method defaults to a read-only, no-tracking query. - TResult? GetFirstOrDefault( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - /// - /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - Task GetFirstOrDefaultAsync( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - /// - /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - Task GetFirstOrDefaultAsync - ( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - #endregion - - #region GetAll - - /// - /// Gets all entities. This method is not recommended - /// - /// The . - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - IQueryable GetAll(TrackingType trackingType = TrackingType.NoTracking); - - /// - /// Gets all entities. This method is not recommended - /// - /// The selector for projection. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// The . - IQueryable GetAll( - Expression> selector, - TrackingType trackingType = TrackingType.NoTracking); - - /// - /// Gets all entities. This method is not recommended - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// The . - IQueryable GetAll( - Expression> selector, - Expression>? predicate = null, - TrackingType trackingType = TrackingType.NoTracking); - - /// - /// Gets all entities. This method is not recommended - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - IQueryable GetAll( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - /// - /// Gets all entities. This method is not recommended - /// - /// A function to test each element for a condition. - /// The selector for projection. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - IQueryable GetAll( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - /// - /// Gets all entities. This method is not recommended - /// - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// The . - Task> GetAllAsync(TrackingType trackingType = TrackingType.NoTracking); - - /// - /// Gets all entities. This method is not recommended - /// - /// The selector for projection. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// The . - Task> GetAllAsync( - Expression> selector, - TrackingType trackingType = TrackingType.NoTracking); - - /// - /// Gets all entities. This method is not recommended - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - Task> GetAllAsync( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - /// - /// Gets all entities. This method is not recommended - /// - /// A function to test each element for a condition. - /// The selector for projection. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - Task> GetAllAsync( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false); - - #endregion -} \ No newline at end of file diff --git a/src/Calabonga.UnitOfWork/Repository.cs b/src/Calabonga.UnitOfWork/Repository.cs index f7ca7f1..af2159c 100644 --- a/src/Calabonga.UnitOfWork/Repository.cs +++ b/src/Calabonga.UnitOfWork/Repository.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using Calabonga.PagedListCore; +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Query; @@ -16,7 +17,7 @@ namespace Calabonga.UnitOfWork; /// Represents a default generic repository implements the interface. /// /// The type of the entity. -public sealed partial class Repository : IRepository where TEntity : class +public sealed class Repository : IRepository where TEntity : class { private readonly DbContext _dbContext; private readonly DbSet _dbSet; @@ -466,4 +467,732 @@ public void Delete(object id) public Task ExecuteDeleteAsync(CancellationToken cancellationToken = default) => _dbSet.ExecuteDeleteAsync(cancellationToken); #endregion + + + /// + /// Gets all entities. This method is not recommended + /// + /// The . + public IQueryable GetAll(TrackingType trackingType = TrackingType.NoTracking) => + trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + /// + /// Gets all entities. This method is not recommended + /// + /// The selector for projection. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// The . + public IQueryable GetAll( + Expression> selector, + TrackingType trackingType = TrackingType.NoTracking) => + trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking().Select(selector), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution().Select(selector), + TrackingType.Tracking => _dbSet.Select(selector), + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + /// + /// Gets all entities. This method is not recommended + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// The . + public IQueryable GetAll( + Expression> selector, + Expression>? predicate = null, + TrackingType trackingType = TrackingType.NoTracking) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (predicate is not null) + { + query = query.Where(predicate); + } + + return query.Select(selector); + } + + /// + /// Gets all entities. This method is not recommended + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + public IQueryable GetAll( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? orderBy(query) + : query; + } + + /// + /// Gets all entities. This method is not recommended + /// + /// A function to test each element for a condition. + /// The selector for projection. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + public IQueryable GetAll( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy != null + ? orderBy(query).Select(selector) + : query.Select(selector); + } + + /// + /// Gets all entities. This method is not recommended + /// + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// The . + public async Task> GetAllAsync(TrackingType trackingType = TrackingType.NoTracking) => + trackingType switch + { + TrackingType.NoTracking => await _dbSet.AsNoTracking().ToListAsync(), + TrackingType.NoTrackingWithIdentityResolution => await _dbSet.AsNoTrackingWithIdentityResolution().ToListAsync(), + TrackingType.Tracking => await _dbSet.ToListAsync(), + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + /// + /// Gets all entities. This method is not recommended + /// + /// The selector for projection. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// The . + public async Task> GetAllAsync(Expression> selector, TrackingType trackingType = TrackingType.NoTracking) => + trackingType switch + { + TrackingType.NoTracking => await _dbSet.AsNoTracking().Select(selector).ToListAsync(), + TrackingType.NoTrackingWithIdentityResolution => await _dbSet.AsNoTrackingWithIdentityResolution().Select(selector).ToListAsync(), + TrackingType.Tracking => await _dbSet.Select(selector).ToListAsync(), + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + /// + /// Gets all entities. This method is not recommended + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + public async Task> GetAllAsync(Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + if (orderBy is not null) + { + return await orderBy(query).ToListAsync(); + } + + return await query.ToListAsync(); + } + + /// + /// Gets all entities. This method is not recommended + /// + /// A function to test each element for a condition. + /// The selector for projection. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + public async Task> GetAllAsync( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? await orderBy(query).Select(selector).ToListAsync() + : await query.Select(selector).ToListAsync(); + } + + /// + /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query.. This method default no-tracking query. + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// The index of page. + /// The size of the page. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method default no-tracking query. + public IPagedList GetPagedList( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + int pageIndex = 0, + int pageSize = 20, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? orderBy(query).ToPagedList(pageIndex, pageSize) + : query.ToPagedList(pageIndex, pageSize); + } + + /// + /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// The index of page. + /// The size of the page. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// + /// A to observe while waiting for the task to complete. + /// + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method default no-tracking query. + public Task> GetPagedListAsync( + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + int pageIndex = 0, + int pageSize = 20, + TrackingType trackingType = TrackingType.NoTracking, + CancellationToken cancellationToken = default, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? orderBy(query).ToPagedListAsync(pageIndex, pageSize, 0, cancellationToken) + : query.ToPagedListAsync(pageIndex, pageSize, 0, cancellationToken); + } + + /// + /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// The index of page. + /// The size of the page. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method default no-tracking query. + public IPagedList GetPagedList( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + int pageIndex = 0, + int pageSize = 20, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + where TResult : class + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? orderBy(query).Select(selector).ToPagedList(pageIndex, pageSize) + : query.Select(selector).ToPagedList(pageIndex, pageSize); + } + + /// + /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// The index of page. + /// The size of the page. + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// + /// A to observe while waiting for the task to complete. + /// + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method default no-tracking query. + public Task> GetPagedListAsync(Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + int pageIndex = 0, + int pageSize = 20, + TrackingType trackingType = TrackingType.NoTracking, + CancellationToken cancellationToken = default, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + where TResult : class + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include != null) + { + query = include(query); + } + + if (predicate != null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy != null + ? orderBy(query).Select(selector).ToPagedListAsync(pageIndex, pageSize, 0, cancellationToken) + : query.Select(selector).ToPagedListAsync(pageIndex, pageSize, 0, cancellationToken); + } + + /// + /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. + /// + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// This method defaults to a read-only, no-tracking query. + public TEntity? GetFirstOrDefault(Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? orderBy(query).FirstOrDefault() + : query.FirstOrDefault(); + } + + public async Task GetFirstOrDefaultAsync(Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? await orderBy(query).FirstOrDefaultAsync() + : await query.FirstOrDefaultAsync(); + } + + public TResult? GetFirstOrDefault( + Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? orderBy(query).Select(selector).FirstOrDefault() + : query.Select(selector).FirstOrDefault(); + } + + /// + /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. + /// + /// The selector for projection. + /// A function to test each element for a condition. + /// A function to order elements. + /// A function to include navigation properties + /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. + /// Ignore query filters + /// Ignore automatic includes + /// An that contains elements that satisfy the condition specified by . + /// Ex: This method defaults to a read-only, no-tracking query. + public async Task GetFirstOrDefaultAsync(Expression> selector, + Expression>? predicate = null, + Func, IOrderedQueryable>? orderBy = null, + Func, IIncludableQueryable>? include = null, + TrackingType trackingType = TrackingType.NoTracking, + bool ignoreQueryFilters = false, + bool ignoreAutoIncludes = false) + { + var query = trackingType switch + { + TrackingType.NoTracking => _dbSet.AsNoTracking(), + TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), + TrackingType.Tracking => _dbSet, + _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) + }; + + if (include is not null) + { + query = include(query); + } + + if (predicate is not null) + { + query = query.Where(predicate); + } + + if (ignoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } + + if (ignoreAutoIncludes) + { + query = query.IgnoreAutoIncludes(); + } + + return orderBy is not null + ? await orderBy(query).Select(selector).FirstOrDefaultAsync() + : await query.Select(selector).FirstOrDefaultAsync(); + } } \ No newline at end of file diff --git a/src/Calabonga.UnitOfWork/RepositoryV2.cs b/src/Calabonga.UnitOfWork/RepositoryV2.cs deleted file mode 100644 index 8f375fb..0000000 --- a/src/Calabonga.UnitOfWork/RepositoryV2.cs +++ /dev/null @@ -1,746 +0,0 @@ -using Calabonga.PagedListCore; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Query; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; - -namespace Calabonga.UnitOfWork; - -/// -/// Represents a default generic repository implements the interface. -/// -/// The type of the entity. -public sealed partial class Repository : IRepository where TEntity : class -{ - - /// - /// Gets all entities. This method is not recommended - /// - /// The . - public IQueryable GetAll(TrackingType trackingType = TrackingType.NoTracking) => - trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - /// - /// Gets all entities. This method is not recommended - /// - /// The selector for projection. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// The . - public IQueryable GetAll( - Expression> selector, - TrackingType trackingType = TrackingType.NoTracking) => - trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking().Select(selector), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution().Select(selector), - TrackingType.Tracking => _dbSet.Select(selector), - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - /// - /// Gets all entities. This method is not recommended - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// The . - public IQueryable GetAll( - Expression> selector, - Expression>? predicate = null, - TrackingType trackingType = TrackingType.NoTracking) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (predicate is not null) - { - query = query.Where(predicate); - } - - return query.Select(selector); - } - - /// - /// Gets all entities. This method is not recommended - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - public IQueryable GetAll( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? orderBy(query) - : query; - } - - /// - /// Gets all entities. This method is not recommended - /// - /// A function to test each element for a condition. - /// The selector for projection. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - public IQueryable GetAll( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy != null - ? orderBy(query).Select(selector) - : query.Select(selector); - } - - /// - /// Gets all entities. This method is not recommended - /// - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// The . - public async Task> GetAllAsync(TrackingType trackingType = TrackingType.NoTracking) => - trackingType switch - { - TrackingType.NoTracking => await _dbSet.AsNoTracking().ToListAsync(), - TrackingType.NoTrackingWithIdentityResolution => await _dbSet.AsNoTrackingWithIdentityResolution().ToListAsync(), - TrackingType.Tracking => await _dbSet.ToListAsync(), - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - /// - /// Gets all entities. This method is not recommended - /// - /// The selector for projection. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// The . - public async Task> GetAllAsync(Expression> selector, TrackingType trackingType = TrackingType.NoTracking) => - trackingType switch - { - TrackingType.NoTracking => await _dbSet.AsNoTracking().Select(selector).ToListAsync(), - TrackingType.NoTrackingWithIdentityResolution => await _dbSet.AsNoTrackingWithIdentityResolution().Select(selector).ToListAsync(), - TrackingType.Tracking => await _dbSet.Select(selector).ToListAsync(), - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - /// - /// Gets all entities. This method is not recommended - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - public async Task> GetAllAsync(Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - if (orderBy is not null) - { - return await orderBy(query).ToListAsync(); - } - - return await query.ToListAsync(); - } - - /// - /// Gets all entities. This method is not recommended - /// - /// A function to test each element for a condition. - /// The selector for projection. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - public async Task> GetAllAsync( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? await orderBy(query).Select(selector).ToListAsync() - : await query.Select(selector).ToListAsync(); - } - - /// - /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query.. This method default no-tracking query. - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// The index of page. - /// The size of the page. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method default no-tracking query. - public IPagedList GetPagedList( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - int pageIndex = 0, - int pageSize = 20, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? orderBy(query).ToPagedList(pageIndex, pageSize) - : query.ToPagedList(pageIndex, pageSize); - } - - /// - /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// The index of page. - /// The size of the page. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// - /// A to observe while waiting for the task to complete. - /// - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method default no-tracking query. - public Task> GetPagedListAsync( - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - int pageIndex = 0, - int pageSize = 20, - TrackingType trackingType = TrackingType.NoTracking, - CancellationToken cancellationToken = default, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? orderBy(query).ToPagedListAsync(pageIndex, pageSize, 0, cancellationToken) - : query.ToPagedListAsync(pageIndex, pageSize, 0, cancellationToken); - } - - /// - /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// The index of page. - /// The size of the page. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method default no-tracking query. - public IPagedList GetPagedList( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - int pageIndex = 0, - int pageSize = 20, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - where TResult : class - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? orderBy(query).Select(selector).ToPagedList(pageIndex, pageSize) - : query.Select(selector).ToPagedList(pageIndex, pageSize); - } - - /// - /// Gets the based on a predicate, orderBy delegate and page information. This method default no-tracking query. - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// The index of page. - /// The size of the page. - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// - /// A to observe while waiting for the task to complete. - /// - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method default no-tracking query. - public Task> GetPagedListAsync(Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - int pageIndex = 0, - int pageSize = 20, - TrackingType trackingType = TrackingType.NoTracking, - CancellationToken cancellationToken = default, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - where TResult : class - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include != null) - { - query = include(query); - } - - if (predicate != null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy != null - ? orderBy(query).Select(selector).ToPagedListAsync(pageIndex, pageSize, 0, cancellationToken) - : query.Select(selector).ToPagedListAsync(pageIndex, pageSize, 0, cancellationToken); - } - - /// - /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. - /// - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// This method defaults to a read-only, no-tracking query. - public TEntity? GetFirstOrDefault(Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? orderBy(query).FirstOrDefault() - : query.FirstOrDefault(); - } - - public async Task GetFirstOrDefaultAsync(Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? await orderBy(query).FirstOrDefaultAsync() - : await query.FirstOrDefaultAsync(); - } - - public TResult? GetFirstOrDefault( - Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? orderBy(query).Select(selector).FirstOrDefault() - : query.Select(selector).FirstOrDefault(); - } - - /// - /// Gets the first or default entity based on a predicate, orderBy delegate and include delegate. This method defaults to a read-only, no-tracking query. - /// - /// The selector for projection. - /// A function to test each element for a condition. - /// A function to order elements. - /// A function to include navigation properties - /// NoTracking to disable changing tracking; Tracking to enable changing tracking; NoTrackingWithIdentityResolution to disable changing tracking but identity resolving. Default to NoTracking. - /// Ignore query filters - /// Ignore automatic includes - /// An that contains elements that satisfy the condition specified by . - /// Ex: This method defaults to a read-only, no-tracking query. - public async Task GetFirstOrDefaultAsync(Expression> selector, - Expression>? predicate = null, - Func, IOrderedQueryable>? orderBy = null, - Func, IIncludableQueryable>? include = null, - TrackingType trackingType = TrackingType.NoTracking, - bool ignoreQueryFilters = false, - bool ignoreAutoIncludes = false) - { - var query = trackingType switch - { - TrackingType.NoTracking => _dbSet.AsNoTracking(), - TrackingType.NoTrackingWithIdentityResolution => _dbSet.AsNoTrackingWithIdentityResolution(), - TrackingType.Tracking => _dbSet, - _ => throw new ArgumentOutOfRangeException(nameof(trackingType), trackingType, null) - }; - - if (include is not null) - { - query = include(query); - } - - if (predicate is not null) - { - query = query.Where(predicate); - } - - if (ignoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } - - if (ignoreAutoIncludes) - { - query = query.IgnoreAutoIncludes(); - } - - return orderBy is not null - ? await orderBy(query).Select(selector).FirstOrDefaultAsync() - : await query.Select(selector).FirstOrDefaultAsync(); - } -} \ No newline at end of file diff --git a/src/Calabonga.UnitOfWork/SaveChangesResult.cs b/src/Calabonga.UnitOfWork/SaveChangesResult.cs index 3456c0d..10b7432 100644 --- a/src/Calabonga.UnitOfWork/SaveChangesResult.cs +++ b/src/Calabonga.UnitOfWork/SaveChangesResult.cs @@ -30,7 +30,7 @@ public sealed class SaveChangesResult /// Adds new message to result /// /// - private void AddMessage(string message) => Messages.Add(message); + public void AddMessage(string message) => Messages.Add(message); /// /// List of the error should appear there