forked from urfnet/URF.Core
-
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.
Refactored IQuery Api to handle paging, paging implementation, now re…
…moved from URF.
- Loading branch information
Showing
12 changed files
with
192 additions
and
247 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,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace URF.Core.Abstractions | ||
{ | ||
public interface IQuery<TEntity> where TEntity : class | ||
{ | ||
IQuery<TEntity> Where(Expression<Func<TEntity, bool>> filter); | ||
IQuery<TEntity> Include(Expression<Func<TEntity, object>> include); | ||
IQuery<TEntity> OrderBy(Expression<Func<TEntity, object>> sortBy); | ||
IQuery<TEntity> OrderByDescending(Expression<Func<TEntity, object>> sortBy); | ||
Task<IEnumerable<TEntity>> SelectAsync(CancellationToken cancellationToken = default); | ||
IQuery<TEntity> Skip(int skip); | ||
IQuery<TEntity> Take(int take); | ||
IQuery<TEntity> ThenBy(Expression<Func<TEntity, object>> sortBy); | ||
IQuery<TEntity> ThenByDescending(Expression<Func<TEntity, object>> sortBy); | ||
Task<int> CountAsync(CancellationToken cancellationToken = default); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace URF.Core.EF.Tests.Models | ||
{ | ||
class Page<TEntity> | ||
{ | ||
public Page(IEnumerable<TEntity> value, int count) | ||
{ | ||
Value = value; | ||
Count = count; | ||
} | ||
public IEnumerable<TEntity> Value { get; set; } | ||
public int Count { get; set; } | ||
} | ||
} |
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,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using URF.Core.Abstractions; | ||
|
||
namespace URF.Core.EF | ||
{ | ||
class Page<TEntity> | ||
{ | ||
public Page(IEnumerable<TEntity> value, int count) | ||
{ | ||
Value = value; | ||
Count = count; | ||
} | ||
public IEnumerable<TEntity> Value { get; set; } | ||
public int Count { get; set; } | ||
} | ||
} |
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,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Urf.Core.Abstractions; | ||
using URF.Core.Abstractions; | ||
|
||
namespace URF.Core.EF | ||
{ | ||
class Query<TEntity> : IQuery<TEntity> where TEntity : class | ||
{ | ||
private int? _skip; | ||
private int? _take; | ||
private IQueryable<TEntity> _queryable; | ||
private IOrderedQueryable<TEntity> _orderedQuery; | ||
|
||
public Query(IRepository<TEntity> repository) | ||
{ | ||
_queryable = repository.Queryable(); | ||
} | ||
|
||
public IQuery<TEntity> Where(Expression<Func<TEntity, bool>> filter) | ||
{ | ||
_queryable =_queryable.Where(filter); | ||
return this; | ||
} | ||
|
||
public IQuery<TEntity> Include(Expression<Func<TEntity, object>> include) | ||
{ | ||
_queryable =_queryable.Include(include); | ||
return this; | ||
} | ||
|
||
public IQuery<TEntity> OrderBy(Expression<Func<TEntity, object>> sortBy) | ||
{ | ||
if (_orderedQuery == null) _orderedQuery = _queryable.OrderBy(sortBy); | ||
else _orderedQuery.OrderBy(sortBy); | ||
return this; | ||
} | ||
|
||
public IQuery<TEntity> ThenBy(Expression<Func<TEntity, object>> sortBy) | ||
{ | ||
_orderedQuery.ThenBy(sortBy); | ||
return this; | ||
} | ||
|
||
public IQuery<TEntity> OrderByDescending(Expression<Func<TEntity, object>> sortBy) | ||
{ | ||
if (_orderedQuery == null) _orderedQuery = _queryable.OrderByDescending(sortBy); | ||
else _orderedQuery.OrderByDescending(sortBy); | ||
return this; | ||
} | ||
public IQuery<TEntity> ThenByDescending(Expression<Func<TEntity, object>> sortBy) | ||
{ | ||
_orderedQuery.ThenByDescending(sortBy); | ||
return this; | ||
} | ||
|
||
public async Task<int> CountAsync(CancellationToken cancellationToken = default ) | ||
{ | ||
return await _queryable.CountAsync(cancellationToken); | ||
} | ||
|
||
public IQuery<TEntity> Skip(int skip) | ||
{ | ||
_skip = skip; | ||
return this; | ||
} | ||
|
||
public IQuery<TEntity> Take(int take) | ||
{ | ||
_take = take; | ||
return this; | ||
} | ||
|
||
public virtual async Task<IEnumerable<TEntity>> SelectAsync(CancellationToken cancellationToken = default ) | ||
{ | ||
_queryable = _orderedQuery ?? _queryable; | ||
|
||
if(_skip.HasValue) _queryable = _queryable.Skip(_skip.Value); | ||
if (_take.HasValue) _queryable = _queryable.Take(_take.Value); | ||
|
||
return await _queryable.ToListAsync(cancellationToken); | ||
} | ||
} | ||
} |
Oops, something went wrong.