SoftwareHospital.Package.EntityFramework
1.0.0
dotnet add package SoftwareHospital.Package.EntityFramework --version 1.0.0
NuGet\Install-Package SoftwareHospital.Package.EntityFramework -Version 1.0.0
<PackageReference Include="SoftwareHospital.Package.EntityFramework" Version="1.0.0" />
paket add SoftwareHospital.Package.EntityFramework --version 1.0.0
#r "nuget: SoftwareHospital.Package.EntityFramework, 1.0.0"
// Install SoftwareHospital.Package.EntityFramework as a Cake Addin #addin nuget:?package=SoftwareHospital.Package.EntityFramework&version=1.0.0 // Install SoftwareHospital.Package.EntityFramework as a Cake Tool #tool nuget:?package=SoftwareHospital.Package.EntityFramework&version=1.0.0
SoftwareHospital Package EntityFramework
Bu kütüphane, Entity Framework ile çalışan bir dizi yardımcı sınıf ve uzantı metodları içerir. Dinamik sorgulama, filtreleme, sıralama ve sayfalama işlemlerini kolaylaştırır. Ayrıca, temel varlık yönetimi ve depo (repository) desenleri sağlar.
İçindekiler
Kurulum
Bu kütüphaneyi projeye dahil etmek için NuGet paket yöneticisini kullanabilirsiniz:
Install-Package SoftwareHospital.Package.EntityFramework
Kullanım
Modeller
Paginate
Sayfalama işlemlerini kolaylaştırmak için kullanılan bir sınıftır.
public class Paginate<T>
{
public int Size { get; set; }
public int Index { get; set; }
public int Count { get; set; }
public int Pages { get; set; }
public IList<T> Items { get; set; }
public bool HasPrevious => Index > 0;
public bool HasNext => Index + 1 < Pages;
public Paginate()
{
Items = Array.Empty<T>();
}
}
BaseEntity
public class BaseEntity<TId> : IEntityTimestamps
{
public TId Id { get; set; }
public string ConcurrencyStamp { get; set; }
public DateTime CreatedDate { get; set; }
public string? CreatorId { get; set; }
public DateTime? UpdatedDate { get; set; }
public string? UpdaterId { get; set; }
public DateTime? DeletedDate { get; set; }
public string? DeleterId { get; set; }
public BaseEntity()
{
Id = default!;
CreatedDate = DateTime.UtcNow;
ConcurrencyStamp = Guid.NewGuid().ToString();
}
public BaseEntity(TId id)
{
Id = id;
CreatedDate = DateTime.UtcNow;
ConcurrencyStamp = Guid.NewGuid().ToString();
}
}
Dinamik Sorgulama
Dinamik sorgulama işlemleri için kullanılır. Filtreleme ve sıralama yapılabilir.
public class Sort
{
public string Field { get; set; }
public string Dir { get; set; }
}
public class Filter
{
public string Field { get; set; }
public string? Value { get; set; }
public string Operator { get; set; }
public string? Logic { get; set; }
public IEnumerable<Filter>? Filters { get; set; }
}
public class DynamicQuery
{
public IEnumerable<Sort>? Sort { get; set; }
public Filter? Filter { get; set; }
}
Kullanım Örneği
IQueryable<Entity> query = context.Entities.AsQueryable();
DynamicQuery dynamicQuery = new DynamicQuery
{
Filter = new Filter { Field = "Name", Operator = "contains", Value = "John" },
Sort = new List<Sort> { new Sort { Field = "Name", Dir = "asc" } }
};
var result = query.ToDynamic(dynamicQuery);
Sayfalama
Sayfalama işlemleri için uzantı metodları içerir.
public static class QueryablePaginateExtensions
{
public static async Task<Paginate<T>> ToPaginateAsync<T>(
this IQueryable<T> source, int index, int size, CancellationToken cancellationToken = default)
{
int count = await source.CountAsync(cancellationToken).ConfigureAwait(false);
List<T> items = await source.Skip(index * size).Take(size).ToListAsync(cancellationToken).ConfigureAwait(false);
return new Paginate<T> { Size = size, Index = index, Count = count, Pages = (int)Math.Ceiling(count / (double)size), Items = items };
}
public static Paginate<T> ToPaginate<T>(
this IQueryable<T> source, int index, int size)
{
int count = source.Count();
List<T> items = source.Skip(index * size).Take(size).ToList();
return new Paginate<T> { Size = size, Index = index, Count = count, Pages = (int)Math.Ceiling(count / (double)size), Items = items };
}
}
Repository Deseni
Temel CRUD işlemlerini ve özel sorgulamaları kapsayan depo (repository) desenini uygular.
Senkron (Synchronous) Repository
public interface IRepository<TEntity, TEntityId> : IQuery<TEntity> where TEntity : BaseEntity<TEntityId>
{
TEntity? Get(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null, bool withDeleted = false, bool enableTracking = true);
Paginate<TEntity> GetList(Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null, int index = 0, int size = 10, bool withDeleted = false, bool enableTracking = true);
Paginate<TEntity> GetListByDynamic(DynamicQuery dynamic, Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null, int index = 0, int size = 10, bool withDeleted = false, bool enableTracking = true);
bool Any(Expression<Func<TEntity, bool>>? predicate = null, bool withDeleted = false, bool enableTracking = true);
TEntity Add(TEntity entity);
ICollection<TEntity> AddRange(ICollection<TEntity> entities);
TEntity Update(TEntity entity);
ICollection<TEntity> UpdateRange(ICollection<TEntity> entities);
TEntity Delete(TEntity entity, bool permanent = false);
ICollection<TEntity> DeleteRange(ICollection<TEntity> entity, bool permanent = false);
}
Asenkron (Asynchronous) Repository
public interface IAsyncRepository<TEntity, TEntityId> : IQuery<TEntity> where TEntity : BaseEntity<TEntityId>
{
Task<TEntity?> GetAsync(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null, bool withDeleted = false, bool enableTracking = true, CancellationToken cancellationToken = default);
Task<Paginate<TEntity>> GetListAsync(Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null, int index = 0, int size = 10, bool withDeleted = false, bool enableTracking = true, CancellationToken cancellationToken = default);
Task<Paginate<TEntity>> GetListByDynamicAsync(DynamicQuery dynamic, Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>>? include = null, int index = 0, int size = 10, bool withDeleted = false, bool enableTracking = true, CancellationToken cancellationToken = default);
Task<bool> AnyAsync(Expression<Func<TEntity, bool>>? predicate = null, bool withDeleted = false, bool enableTracking = true, CancellationToken cancellationToken = default);
Task<TEntity> AddAsync(TEntity entity, string creatorId);
Task<ICollection<TEntity>> AddRangeAsync(ICollection<TEntity> entities, string creatorId);
Task<TEntity> UpdateAsync(TEntity entity, string? updaterId);
Task<ICollection<TEntity>> UpdateRangeAsync(ICollection<TEntity> entities, string? updaterId);
Task<TEntity> DeleteAsync(TEntity entity, string? deleterId, bool permanent = false);
Task<ICollection<TEntity>> DeleteRangeAsync(ICollection<TEntity> entities, string? deleterId, bool permanent = false);
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- EntityFrameworkCore.Query (>= 1.3.1.2)
- Microsoft.EntityFrameworkCore (>= 8.0.4)
- System.Linq.Dynamic.Core (>= 1.3.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|