Skip to content

Commit

Permalink
Removed unneeded dependency and simplified models. Removed need to se…
Browse files Browse the repository at this point in the history
…rialize complex object and thus eliminated complexities surrounding serialization options.
  • Loading branch information
jasonmwebb-lv committed Nov 22, 2024
1 parent a83fdce commit 2e06554
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 135 deletions.
2 changes: 1 addition & 1 deletion Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected override void OnBuildInitialized()
{
Log.Information("Generating NuGet packages for projects in solution");
int commitNum = 0;
string NuGetVersionCustom = "2.1.1.1";
string NuGetVersionCustom = "2.1.1.2";


//if it's not a tagged release - append the commit number to the package version
Expand Down
149 changes: 20 additions & 129 deletions Src/RCommon.Models/PaginatedListModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Text;
using RCommon.Collections;

namespace RCommon.Models
{
Expand All @@ -25,56 +24,30 @@ protected PaginatedListModel(IQueryable<TSource> source, PaginatedListRequest pa
PaginateQueryable(source, paginatedListRequest, skipTotal);
}

/// <summary>
/// Accepts <see cref="IPaginatedList{T}"/> and overrides total record count with the total count provided in the parameter.
/// </summary>
/// <param name="source">Pre-filtered list of data.</param>
/// <param name="paginatedListRequest">Request model that contains populated state date for page number, page size, sort by, etc.</param>
/// <param name="totalCount">Total count of records contained by pre-filtered data source. This should be different than the actual record
/// count in <see cref="IPaginatedList{T}"/></param>
/// <param name="skipSort">Instructions on whether or not to skip sorting. Default is false</param>
/// <remarks></remarks>
protected PaginatedListModel(IPaginatedList<TSource> source, PaginatedListRequest paginatedListRequest, int totalCount, bool skipSort = false)
{
PaginateList(source, paginatedListRequest, totalCount, skipSort);
}

private IQueryable<TSource> Sort(IQueryable<TSource> source)
{

if (this.SortExpression == null)
if (this._sortExpression == null)
{
return source;
}

return SortDirection == SortDirectionEnum.Descending
? source.OrderByDescending(this.SortExpression)
: source.OrderBy(this.SortExpression);
? source.OrderByDescending(this._sortExpression)
: source.OrderBy(this._sortExpression);
}

private IList<TSource> Sort(IPaginatedList<TSource> source)
protected void PaginateQueryable(IQueryable<TSource> source, PaginatedListRequest paginatedListRequest, bool skipTotal = false, bool skipSort = false)
{

if (this.SortExpression == null)
if (source == null)
{
return source;
throw new ArgumentException("Source Data cannot be null");
}

var sortFunc = this.SortExpression.Compile(); // How much overhead is this?

var list = SortDirection == SortDirectionEnum.Descending
? source.OrderByDescending(sortFunc).ToList()
: source.OrderBy(sortFunc).ToList();
return list;
}

protected void PaginateQueryable(IQueryable<TSource> source, PaginatedListRequest paginatedListRequest, bool skipTotal = false, bool skipSort = false)
{
if (paginatedListRequest == null)
{
return;
throw new ArgumentException("Request input cannot be null");
}
Guard.IsNotNull(source, nameof(source));

SortBy = paginatedListRequest.SortBy ?? "id";
SortDirection = paginatedListRequest.SortDirection;
Expand All @@ -98,40 +71,12 @@ protected void PaginateQueryable(IQueryable<TSource> source, PaginatedListReques
Items = CastItems(query).ToList();
}

protected void PaginateList(IPaginatedList<TSource> source, PaginatedListRequest paginatedListRequest, int totalCount,
bool skipSort = false)
{
if (paginatedListRequest == null)
{
return;
}
Guard.IsNotNull(source, nameof(source));

SortBy = paginatedListRequest.SortBy ?? "id";
SortDirection = paginatedListRequest.SortDirection;

PageSize = paginatedListRequest.PageSize;
PageNumber = paginatedListRequest.PageNumber;

if (totalCount > 0)
{
TotalCount = totalCount;
TotalPages = TotalCount / PageSize + (TotalCount % PageSize > 0 ? 1 : 0) ?? 1;
}

var query = skipSort ? source : Sort(source);

/*if (PageSize.HasValue) // No need to implement paging as list should contain current view
{
query = query.Skip(PageSize.Value * (PageIndex - 1)).Take(PageSize.Value).ToList();
}*/

Items = CastItems(query.AsQueryable()).ToList();
}

protected abstract IQueryable<TOut> CastItems(IQueryable<TSource> source);

public virtual Expression<Func<TSource, object>> SortExpression { get => _sortExpression; set => _sortExpression = value; }
public virtual Expression<Func<TSource, object>> GetSortExpression()
{
return _sortExpression;
}

[DataMember]
public List<TOut> Items { get; set; }
Expand Down Expand Up @@ -189,56 +134,30 @@ protected PaginatedListModel(IQueryable<TSource> source, PaginatedListRequest pa
PaginateQueryable(source, paginatedListRequest, skipTotal);
}

/// <summary>
/// Accepts <see cref="IPaginatedList{T}"/> and overrides total record count with the total count provided in the parameter.
/// </summary>
/// <param name="source">Pre-filtered list of data.</param>
/// <param name="paginatedListRequest">Request model that contains populated state date for page number, page size, sort by, etc.</param>
/// <param name="totalCount">Total count of records contained by pre-filtered data source. This should be different than the actual record
/// count in <see cref="IPaginatedList{T}"/></param>
/// <param name="skipSort">Instructions on whether or not to skip sorting. Default is false</param>
/// <remarks></remarks>
protected PaginatedListModel(IPaginatedList<TSource> source, PaginatedListRequest paginatedListRequest, int totalCount, bool skipSort = false)
{
PaginateList(source, paginatedListRequest, totalCount, skipSort);
}

private IQueryable<TSource> Sort(IQueryable<TSource> source)
{

if (this.SortExpression == null)
if (this._sortExpression == null)
{
return source;
}

return SortDirection == SortDirectionEnum.Descending
? source.OrderByDescending(this.SortExpression)
: source.OrderBy(this.SortExpression);
? source.OrderByDescending(this._sortExpression)
: source.OrderBy(this._sortExpression);
}

private IList<TSource> Sort(IPaginatedList<TSource> source)
protected void PaginateQueryable(IQueryable<TSource> source, PaginatedListRequest paginatedListRequest, bool skipTotal = false, bool skipSort = false)
{

if (this.SortExpression == null)
if (source == null)
{
return source;
throw new ArgumentException("Source Data cannot be null");
}

var sortFunc = this.SortExpression.Compile(); // How much overhead is this?

var list = SortDirection == SortDirectionEnum.Descending
? source.OrderByDescending(sortFunc).ToList()
: source.OrderBy(sortFunc).ToList();
return list;
}

protected void PaginateQueryable(IQueryable<TSource> source, PaginatedListRequest paginatedListRequest, bool skipTotal = false, bool skipSort = false)
{
if (paginatedListRequest == null)
{
return;
throw new ArgumentException("Request input cannot be null");
}
Guard.IsNotNull(source, nameof(source));

SortBy = paginatedListRequest.SortBy ?? "id";
SortDirection = paginatedListRequest.SortDirection;
Expand All @@ -262,39 +181,11 @@ protected void PaginateQueryable(IQueryable<TSource> source, PaginatedListReques
Items = query.ToList();
}

protected void PaginateList(IPaginatedList<TSource> source, PaginatedListRequest paginatedListRequest, int totalCount,
bool skipSort = false)
public virtual Expression<Func<TSource, object>> GetSortExpression()
{
if (paginatedListRequest == null)
{
return;
}
Guard.IsNotNull(source, nameof(source));

SortBy = paginatedListRequest.SortBy ?? "id";
SortDirection = paginatedListRequest.SortDirection;

PageSize = paginatedListRequest.PageSize;
PageNumber = paginatedListRequest.PageNumber;

if (totalCount > 0)
{
TotalCount = totalCount;
TotalPages = TotalCount / PageSize + (TotalCount % PageSize > 0 ? 1 : 0) ?? 1;
}

var query = skipSort ? source : Sort(source);

/*if (PageSize.HasValue) // No need to implement paging as list should contain current view
{
query = query.Skip(PageSize.Value * (PageIndex - 1)).Take(PageSize.Value).ToList();
}*/

Items = query.AsQueryable().ToList();
return _sortExpression;
}

public virtual Expression<Func<TSource, object>> SortExpression { get => _sortExpression; set => _sortExpression = value; }

[DataMember]
public List<TSource> Items { get; set; }

Expand Down
6 changes: 1 addition & 5 deletions Src/RCommon.Models/RCommon.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

<PropertyGroup>
<TargetFrameworks>net8.0;</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\RCommon.Core\RCommon.Core.csproj" />
</ItemGroup></Project>
</PropertyGroup></Project>

0 comments on commit 2e06554

Please sign in to comment.