Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Yuxi.SharedKernel.Data.Abstractions/Entities/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Yuxi.SharedKernel.Data.Abstractions.Entities
{
public abstract class Entity
{
#region Public Properties

public string Id { get; set; }

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Yuxi.SharedKernel.Data.Abstractions.PagedList
{
using System;
using System.Collections.Generic;

public static class IEnumerablePagedListExtensions
{
#region Public Static Methods

public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex, int pageSize,
int indexFrom = 0) => new PagedList<T>(source, pageIndex, pageSize, indexFrom);

public static IPagedList<TResult> ToPagedList<TSource, TResult>(this IEnumerable<TSource> source,
Func<IEnumerable<TSource>, IEnumerable<TResult>> converter, int pageIndex, int pageSize,
int indexFrom = 0) => new PagedList<TSource, TResult>(source, converter, pageIndex, pageSize, indexFrom);

#endregion
}
}
27 changes: 27 additions & 0 deletions Yuxi.SharedKernel.Data.Abstractions/PagedList/IPagedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Yuxi.SharedKernel.Data.Abstractions.PagedList
{
using System.Collections.Generic;

public interface IPagedList<T>
{
#region Public Read Only Properties

int IndexFrom { get; }

int PageIndex { get; }

int PageSize { get; }

int TotalCount { get; }

int TotalPages { get; }

IList<T> Items { get; }

bool HasPreviousPage { get; }

bool HasNextPage { get; }

#endregion
}
}
127 changes: 127 additions & 0 deletions Yuxi.SharedKernel.Data.Abstractions/PagedList/PagedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
namespace Yuxi.SharedKernel.Data.Abstractions.PagedList
{
using System;
using System.Collections.Generic;
using System.Linq;

public class PagedList<T> : IPagedList<T>
{
#region Public Properties

public int PageIndex { get; set; }

public int PageSize { get; set; }

public int TotalCount { get; set; }

public int TotalPages { get; set; }

public int IndexFrom { get; set; }

public IList<T> Items { get; set; }

#endregion

#region Public Read Only Properties

public bool HasPreviousPage => PageIndex - IndexFrom > 0;

public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;

#endregion

#region Constructors

public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int indexFrom)
{
if (indexFrom > pageIndex)
{
throw new ArgumentException(
$"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
}

PageIndex = pageIndex;
PageSize = pageSize;
IndexFrom = indexFrom;
TotalCount = source.Count();
TotalPages = (int) Math.Ceiling(TotalCount / (double) PageSize);

Items = source.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToList();
}

public PagedList() => Items = new T[0];

#endregion
}

public class PagedList<TSource, TResult> : IPagedList<TResult>
{
#region Public Read Only Properties

public int PageIndex { get; }

public int PageSize { get; }

public int TotalCount { get; }

public int TotalPages { get; }

public int IndexFrom { get; }

public IList<TResult> Items { get; }

public bool HasPreviousPage => PageIndex - IndexFrom > 0;

public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;

#endregion

#region Constructors

public PagedList(IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> converter,
int pageIndex, int pageSize, int indexFrom)
{
if (indexFrom > pageIndex)
{
throw new ArgumentException(
$"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
}

PageIndex = pageIndex;
PageSize = pageSize;
IndexFrom = indexFrom;
TotalCount = source.Count();
TotalPages = (int) Math.Ceiling(TotalCount / (double) PageSize);

var items = source.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToArray();

Items = new List<TResult>(converter(items));
}

public PagedList(IPagedList<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> converter)
{
PageIndex = source.PageIndex;
PageSize = source.PageSize;
IndexFrom = source.IndexFrom;
TotalCount = source.TotalCount;
TotalPages = source.TotalPages;

Items = new List<TResult>(converter(source.Items));
}

#endregion
}

public static class PagedList
{
#region Public Static Methods

public static IPagedList<T> Empty<T>() => new PagedList<T>();

public static IPagedList<TResult> From<TResult, TSource>(IPagedList<TSource> source,
Func<IEnumerable<TSource>, IEnumerable<TResult>> converter) =>
new PagedList<TSource, TResult>(source, converter);

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Yuxi.SharedKernel.Data.Abstractions.Repository
{
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

public interface IQueryableRepository<TEntity>
{
#region Public Methods

IQueryable<TEntity> FromSql(string sql, params object[] parameters);

TEntity Find(params object[] keyValues);

Task<TEntity> FindAsync(params object[] keyValues);

Task<TEntity> FindAsync(object[] keyValues, CancellationToken cancellationToken);

int Count(Expression<Func<TEntity, bool>> predicate = null);

#endregion
}
}
67 changes: 67 additions & 0 deletions Yuxi.SharedKernel.Data.Abstractions/Repository/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace Yuxi.SharedKernel.Data.Abstractions.Repository
{
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public interface IRepository<in TEntity> where TEntity : class
{
#region Public Methods

#region Inserts

void Insert(TEntity entity);

void Insert(params TEntity[] entities);

void Insert(IEnumerable<TEntity> entities);

Task InsertAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));

Task InsertAsync(params TEntity[] entities);

Task InsertAsync(IEnumerable<TEntity> entities,
CancellationToken cancellationToken = default(CancellationToken));

#endregion

#region Updates

void Update(TEntity entity);

void Update(params TEntity[] entities);

void Update(IEnumerable<TEntity> entities);

Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));

Task UpdateAsync(params TEntity[] entities);

Task UpdateAsync(IEnumerable<TEntity> entities,
CancellationToken cancellationToken = default(CancellationToken));

#endregion

#region Deletes

void Delete(object id);

void Delete(TEntity entity);

void Delete(params TEntity[] entities);

void Delete(IEnumerable<TEntity> entities);

Task DeleteAsync(object id);

Task DeleteAsync(TEntity entity);

Task DeleteAsync(params TEntity[] entities);

Task DeleteAsync(IEnumerable<TEntity> entities);

#endregion

#endregion
}
}
21 changes: 21 additions & 0 deletions Yuxi.SharedKernel.Data.Abstractions/UnitOfWork/IUnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Yuxi.SharedKernel.Data.Abstractions.UnitOfWork
{
using System;
using System.Linq;
using System.Threading.Tasks;

public interface IUnitOfWork : IDisposable
{
#region Public Methods

int SaveChanges(bool ensureAutoHistory = false);

Task<int> SaveChangesAsync(bool ensureAutoHistory = false);

int ExecuteSqlCommand(string sql, params object[] parameters);

IQueryable<TEntity> FromSql<TEntity>(string sql, params object[] parameters) where TEntity : class;

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="UnitOfWork\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Yuxi.SharedKernel.Specification\Yuxi.SharedKernel.Patterns.Specification.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Yuxi.SharedKernel.Data.EntityFrameworkCore.Abstractions
{
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query;
using Data.Abstractions.PagedList;
using Patterns.Specification.Contracts;

public interface IQueryableRepository<TEntity> : Data.Abstractions.Repository.IQueryableRepository<TEntity>
{
#region Public Methods

IPagedList<TEntity> GetPagedList(ISpecification<TEntity> businessSpecification = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
int pageIndex = 0,
int pageSize = 20,
bool disableTracking = true);

Task<IPagedList<TEntity>> GetPagedListAsync(ISpecification<TEntity> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
int pageIndex = 0,
int pageSize = 20,
bool disableTracking = true,
CancellationToken cancellationToken = default(CancellationToken));

IPagedList<TResult> GetPagedList<TResult>(Expression<Func<TEntity, TResult>> selector,
ISpecification<TEntity> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
int pageIndex = 0,
int pageSize = 20,
bool disableTracking = true) where TResult : class;

Task<IPagedList<TResult>> GetPagedListAsync<TResult>(Expression<Func<TEntity, TResult>> selector,
ISpecification<TEntity> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
int pageIndex = 0,
int pageSize = 20,
bool disableTracking = true,
CancellationToken cancellationToken = default(CancellationToken)) where TResult : class;

TEntity GetFirstOrDefault(ISpecification<TEntity> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
bool disableTracking = true);

TResult GetFirstOrDefault<TResult>(Expression<Func<TEntity, TResult>> selector,
ISpecification<TEntity> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
bool disableTracking = true);

#endregion
}
}
Loading