From dccd6b95544b2947acfa590dac50a8ddc956f57d Mon Sep 17 00:00:00 2001 From: Tanvir Ahmad Arjel Date: Fri, 12 Nov 2021 23:16:20 +0600 Subject: [PATCH] v5.8.0 has been released. 1. EF Core 6.0 support has been added. 2. ExistsByIdAsync method has been added. --- .../Controllers/EmployeeController.cs | 1 + ...anvirArjel.EFCore.GenericRepository.csproj | 5 ++- .../Extensions/SqlQueryExtensions.cs | 4 +- .../IQueryRepository.cs | 11 +++++ .../QueryRepository.cs | 41 +++++++++++++++++++ .../TanvirArjel.EFCore.QueryRepository.csproj | 7 ++-- 6 files changed, 62 insertions(+), 7 deletions(-) diff --git a/demo/AspNetCore5.0/Controllers/EmployeeController.cs b/demo/AspNetCore5.0/Controllers/EmployeeController.cs index 1009061..af1fb16 100644 --- a/demo/AspNetCore5.0/Controllers/EmployeeController.cs +++ b/demo/AspNetCore5.0/Controllers/EmployeeController.cs @@ -52,6 +52,7 @@ public async Task Details(long? id) return NotFound(); } Employee employee = await _repository.GetByIdAsync(id); + bool isExistent = await _repository.ExistsByIdAsync(id); if (employee == null) { return NotFound(); diff --git a/src/TanvirArjel.EFCore.GenericRepository/TanvirArjel.EFCore.GenericRepository.csproj b/src/TanvirArjel.EFCore.GenericRepository/TanvirArjel.EFCore.GenericRepository.csproj index bd7399b..3830b34 100644 --- a/src/TanvirArjel.EFCore.GenericRepository/TanvirArjel.EFCore.GenericRepository.csproj +++ b/src/TanvirArjel.EFCore.GenericRepository/TanvirArjel.EFCore.GenericRepository.csproj @@ -44,8 +44,9 @@ EFCore, RepositoryLayer, GenericRepository, UnitOfWork, .NET, .NETCore, ASP.NETCore 1. EF Core 6.0 support has been added. + 2. ExistsByIdAsync method has been added. - 5.7.0 + 5.8.0 LICENSE icon.png TanvirArjel @@ -74,7 +75,7 @@ - + diff --git a/src/TanvirArjel.EFCore.QueryRepository/Extensions/SqlQueryExtensions.cs b/src/TanvirArjel.EFCore.QueryRepository/Extensions/SqlQueryExtensions.cs index 0b3ea85..f501e51 100644 --- a/src/TanvirArjel.EFCore.QueryRepository/Extensions/SqlQueryExtensions.cs +++ b/src/TanvirArjel.EFCore.QueryRepository/Extensions/SqlQueryExtensions.cs @@ -58,7 +58,7 @@ public static async Task> GetFromQueryAsync( List list = new List(); T obj = default; - while (result.Read()) + while (await result.ReadAsync(cancellationToken)) { if (!(typeof(T).IsPrimitive || typeof(T).Equals(typeof(string)))) { @@ -94,7 +94,7 @@ private static async Task> GetFromQueryAsync2( List list = new List(); T t = default; - while (dr.Read()) + while (await dr.ReadAsync(cancellationToken)) { if (!(typeof(T).IsPrimitive || typeof(T).Equals(typeof(string)))) { diff --git a/src/TanvirArjel.EFCore.QueryRepository/IQueryRepository.cs b/src/TanvirArjel.EFCore.QueryRepository/IQueryRepository.cs index af650a1..75db97d 100644 --- a/src/TanvirArjel.EFCore.QueryRepository/IQueryRepository.cs +++ b/src/TanvirArjel.EFCore.QueryRepository/IQueryRepository.cs @@ -441,6 +441,17 @@ Task ExistsAsync(CancellationToken cancellationToken = default) Task ExistsAsync(Expression> condition, CancellationToken cancellationToken = default) where TEntity : class; + /// + /// This method takes primary key value of the entity whose existence be determined + /// and returns of . + /// + /// The type of the entity. + /// The primary key value of the entity whose the existence will checked. + /// A to observe while waiting for the task to complete. + /// Returns . + Task ExistsByIdAsync(object id, CancellationToken cancellationToken = default) + where TEntity : class; + /// /// This method returns all count in type. /// diff --git a/src/TanvirArjel.EFCore.QueryRepository/QueryRepository.cs b/src/TanvirArjel.EFCore.QueryRepository/QueryRepository.cs index ada70fb..5e910bb 100644 --- a/src/TanvirArjel.EFCore.QueryRepository/QueryRepository.cs +++ b/src/TanvirArjel.EFCore.QueryRepository/QueryRepository.cs @@ -529,6 +529,47 @@ public async Task ExistsAsync(Expression> condition, Canc return isExists; } + public async Task ExistsByIdAsync(object id, CancellationToken cancellationToken = default) + where T : class + { + if (id == null) + { + throw new ArgumentNullException(nameof(id)); + } + + IEntityType entityType = _dbContext.Model.FindEntityType(typeof(T)); + + string primaryKeyName = entityType.FindPrimaryKey().Properties.Select(p => p.Name).FirstOrDefault(); + Type primaryKeyType = entityType.FindPrimaryKey().Properties.Select(p => p.ClrType).FirstOrDefault(); + + if (primaryKeyName == null || primaryKeyType == null) + { + throw new ArgumentException("Entity does not have any primary key defined", nameof(id)); + } + + object primayKeyValue = null; + + try + { + primayKeyValue = Convert.ChangeType(id, primaryKeyType, CultureInfo.InvariantCulture); + } + catch (Exception) + { + throw new ArgumentException($"You can not assign a value of type {id.GetType()} to a property of type {primaryKeyType}"); + } + + ParameterExpression pe = Expression.Parameter(typeof(T), "entity"); + MemberExpression me = Expression.Property(pe, primaryKeyName); + ConstantExpression constant = Expression.Constant(primayKeyValue, primaryKeyType); + BinaryExpression body = Expression.Equal(me, constant); + Expression> expressionTree = Expression.Lambda>(body, new[] { pe }); + + IQueryable query = _dbContext.Set(); + + bool isExistent = await query.AnyAsync(expressionTree, cancellationToken).ConfigureAwait(false); + return isExistent; + } + public async Task GetCountAsync(CancellationToken cancellationToken = default) where T : class { diff --git a/src/TanvirArjel.EFCore.QueryRepository/TanvirArjel.EFCore.QueryRepository.csproj b/src/TanvirArjel.EFCore.QueryRepository/TanvirArjel.EFCore.QueryRepository.csproj index 4e9382d..056bd76 100644 --- a/src/TanvirArjel.EFCore.QueryRepository/TanvirArjel.EFCore.QueryRepository.csproj +++ b/src/TanvirArjel.EFCore.QueryRepository/TanvirArjel.EFCore.QueryRepository.csproj @@ -39,9 +39,10 @@ Git EFCore, RepositoryLayer, GenericRepository, QueryRepository, .NET, .NETCore, ASP.NETCore - 1. This is the initial release of the library. + 1. EF Core 6.0 support has been added. + 2. ExistsByIdAsync method has been added. - 1.0.0 + 1.1.0 LICENSE icon.png TanvirArjel @@ -70,7 +71,7 @@ - +