-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implementing generic Get Entity By Id Specification
- Loading branch information
Edgar Mesquita
authored and
Edgar Mesquita
committed
Sep 4, 2023
1 parent
885e0d4
commit b63d205
Showing
5 changed files
with
73 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace eQuantic.Core.Data.EntityFramework.Repository.Extensions; | ||
|
||
public static class DbContextExtensions | ||
{ | ||
public static Expression<Func<TEntity, bool>> GetFindByKeyExpression<TEntity, TKey>(this DbContext dbContext, TKey id) | ||
{ | ||
var primaryKey = dbContext.Model.FindEntityType(typeof(TEntity))!.FindPrimaryKey()!.Properties | ||
.FirstOrDefault(); | ||
if (primaryKey == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var keyProperty = typeof(TEntity).GetProperty(primaryKey.Name); | ||
|
||
if (keyProperty == null) | ||
{ | ||
return null; | ||
} | ||
|
||
// Create entity => portion of lambda expression | ||
var parameter = Expression.Parameter(typeof(TEntity), "entity"); | ||
|
||
// create entity.Id portion of lambda expression | ||
var property = Expression.Property(parameter, keyProperty.Name); | ||
|
||
// create 'id' portion of lambda expression | ||
var equalsTo = Expression.Constant(id); | ||
|
||
// create entity.Id == 'id' portion of lambda expression | ||
var equality = Expression.Equal(property, equalsTo); | ||
|
||
// finally create entire expression - entity => entity.Id == 'id' | ||
var retVal = Expression.Lambda<Func<TEntity, bool>>(equality, new[] { parameter }); | ||
return retVal; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using eQuantic.Core.Data.EntityFramework.Repository; | ||
using eQuantic.Core.Data.EntityFramework.Repository.Extensions; | ||
using eQuantic.Core.Data.Repository; | ||
using eQuantic.Linq.Specification; | ||
|
||
namespace eQuantic.Core.Data.EntityFramework.Specifications; | ||
|
||
public class GetEntityByIdSpecification<TEntity, TKey> : Specification<TEntity> | ||
where TEntity : class, IEntity<TKey>, new() | ||
{ | ||
private readonly TKey _id; | ||
private readonly UnitOfWork _unitOfWork; | ||
|
||
public GetEntityByIdSpecification(TKey id, UnitOfWork unitOfWork) | ||
{ | ||
_id = id; | ||
_unitOfWork = unitOfWork; | ||
} | ||
public override Expression<Func<TEntity, bool>> SatisfiedBy() | ||
{ | ||
return _unitOfWork.GetDbContext().GetFindByKeyExpression<TEntity, TKey>(_id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters