Skip to content

Commit

Permalink
v5.8.0 has been released.
Browse files Browse the repository at this point in the history
1. EF Core 6.0 support has been added.
2. ExistsByIdAsync method has been added.
  • Loading branch information
Tanvir Ahmad Arjel committed Nov 12, 2021
1 parent fe68d25 commit dccd6b9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions demo/AspNetCore5.0/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public async Task<IActionResult> Details(long? id)
return NotFound();
}
Employee employee = await _repository.GetByIdAsync<Employee>(id);
bool isExistent = await _repository.ExistsByIdAsync<Employee>(id);
if (employee == null)
{
return NotFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
<PackageTags>EFCore, RepositoryLayer, GenericRepository, UnitOfWork, .NET, .NETCore, ASP.NETCore</PackageTags>
<PackageReleaseNotes>
1. EF Core 6.0 support has been added.
2. ExistsByIdAsync method has been added.
</PackageReleaseNotes>
<Version>5.7.0</Version>
<Version>5.8.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>icon.png</PackageIcon>
<Authors>TanvirArjel</Authors>
Expand Down Expand Up @@ -74,7 +75,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0-*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static async Task<List<T>> GetFromQueryAsync<T>(

List<T> list = new List<T>();
T obj = default;
while (result.Read())
while (await result.ReadAsync(cancellationToken))
{
if (!(typeof(T).IsPrimitive || typeof(T).Equals(typeof(string))))
{
Expand Down Expand Up @@ -94,7 +94,7 @@ private static async Task<List<T>> GetFromQueryAsync2<T>(

List<T> list = new List<T>();
T t = default;
while (dr.Read())
while (await dr.ReadAsync(cancellationToken))
{
if (!(typeof(T).IsPrimitive || typeof(T).Equals(typeof(string))))
{
Expand Down
11 changes: 11 additions & 0 deletions src/TanvirArjel.EFCore.QueryRepository/IQueryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,17 @@ Task<bool> ExistsAsync<TEntity>(CancellationToken cancellationToken = default)
Task<bool> ExistsAsync<TEntity>(Expression<Func<TEntity, bool>> condition, CancellationToken cancellationToken = default)
where TEntity : class;

/// <summary>
/// This method takes primary key value of the entity whose existence be determined
/// and returns <see cref="Task"/> of <see cref="bool"/>.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="id">The primary key value of the entity whose the existence will checked.</param>
/// <param name="cancellationToken"> A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>Returns <see cref="bool"/>.</returns>
Task<bool> ExistsByIdAsync<TEntity>(object id, CancellationToken cancellationToken = default)
where TEntity : class;

/// <summary>
/// This method returns all count in <see cref="int"/> type.
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions src/TanvirArjel.EFCore.QueryRepository/QueryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,47 @@ public async Task<bool> ExistsAsync<T>(Expression<Func<T, bool>> condition, Canc
return isExists;
}

public async Task<bool> ExistsByIdAsync<T>(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<Func<T, bool>> expressionTree = Expression.Lambda<Func<T, bool>>(body, new[] { pe });

IQueryable<T> query = _dbContext.Set<T>();

bool isExistent = await query.AnyAsync(expressionTree, cancellationToken).ConfigureAwait(false);
return isExistent;
}

public async Task<int> GetCountAsync<T>(CancellationToken cancellationToken = default)
where T : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
<RepositoryType>Git</RepositoryType>
<PackageTags>EFCore, RepositoryLayer, GenericRepository, QueryRepository, .NET, .NETCore, ASP.NETCore</PackageTags>
<PackageReleaseNotes>
1. This is the initial release of the library.
1. EF Core 6.0 support has been added.
2. ExistsByIdAsync method has been added.
</PackageReleaseNotes>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIcon>icon.png</PackageIcon>
<Authors>TanvirArjel</Authors>
Expand Down Expand Up @@ -70,7 +71,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0-*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.*" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down

0 comments on commit dccd6b9

Please sign in to comment.