Panner 2.0.0
dotnet add package Panner --version 2.0.0
NuGet\Install-Package Panner -Version 2.0.0
<PackageReference Include="Panner" Version="2.0.0" />
paket add Panner --version 2.0.0
#r "nuget: Panner, 2.0.0"
// Install Panner as a Cake Addin #addin nuget:?package=Panner&version=2.0.0 // Install Panner as a Cake Tool #tool nuget:?package=Panner&version=2.0.0
Basic Usage - Sample
a. Configuration
Setting up your context to define your entities and rules.
var builder = new PContext();
// Entity wide configuration (Option 1)
builder.Entity<Post>()
.AllPropertiesAreSortableByName();
.AllPropertiesAreFilterableByName();
// A more granular approach (Option 2)
builder.Entity<Post>()
.Property(x => x.Id, o => o
.IsSortableByName()
.IsFilterableByName()
).Property(x => x.Title, o => o
.IsSortableByName()
.IsFilterableByName()
).Property(x => x.CreatedOn, o => o
.IsSortableAs("Creation")
.IsFilterableAs("Creation")
);
// And then we build our context.
IPContext context = builder.Build();
// Ta-daah! Now we have a context!
b. Parsing
Feeding Panner's context our CSV inputs and obtaining sort/filter particles
Sorts
var csvSort = "-Title, Id"; // Example
if (!context.TryParseCsv(
input: csvSort,
out IEnumerable<ISortParticle<TEntity>> sortParticles
)){
// Parsing failed
return;
}
// Ta-daah! Now we have sort particles!
Filters
var csvFilter = "Id<666, Id>13"; // Example
if (!context.TryParseCsv(
input: csvFilter,
out IEnumerable<IFilterParticle<TEntity>> filterParticles
)){
// Parsing failed
return;
}
// Ta-daah! Now we have filter particles!
c. Application
Using Panner's extension methods to apply our particles to any IEnumerable
, IQueryable
, DbSet
, etc..
var pannedQueryable = posts
.Apply(filterParticles)
.Apply(sortParticles);
// Ta-daah! Now we have a filtered and sorted IQueryable!
Advanced Usage - Samples
Custom sort for a specific entity
Ability to sort our fake entity Post
by "popularity", an arbitrary concept and not a property of the entity.
Leveraging ISortParticle
, ISortParticleGenerator
and extending PEntityBuilder
.
Particle - SortPostByPopularityParticle.cs
public class SortPostByPopularityParticle : ISortParticle<Post> // Panner's interface
{
readonly bool Descending;
public SortPostByPopularityParticle(bool descending)
{
this.Descending = descending;
}
public IOrderedQueryable<Post> ApplyTo(IOrderedQueryable<Post> source)
{
// Here's how the sorting is done when the particle is applied.
if (this.Descending)
return source
.ThenByDescending(x => x.AmtLikes)
.ThenByDescending(x => x.AmtComments);
else
return source
.ThenBy(x => x.AmtLikes)
.ThenBy(x => x.AmtComments);
}
}
Particle Generator - SortPostsByPopularityParticleGenerator.cs
public class SortPostsByPopularityParticleGenerator : ISortParticleGenerator<Post> // Panner's interface
{
public bool TryGenerate(IPContext context, string input, out ISortParticle<Post> particle)
{
var descending = input.StartsWith('-');
var remaining = descending ? input.Substring(1) : input;
if (!remaining.Trim().Equals("Popularity", System.StringComparison.OrdinalIgnoreCase))
{
// Not the input we're interested in.
particle = null;
return false;
}
particle = new SortPostByPopularityParticle(descending);
return true;
}
}
PEntityBuilder Extension - PEntityBuilder.Post.IsSortableByPopularity.cs
public static partial class PEntityBuilderExtensions
{
/// <summary>Marks the entity as sortable by popularity.</summary>
public static PEntityBuilder<Post> IsSortableByPopularity(this PEntityBuilder<Post> builder)
{
builder.GetOrCreateGenerator<ISortParticle<Post>, SortPostsByPopularityParticleGenerator>();
return builder; // So we can chain calls!
}
}
Setting it all up in the context builder
builder.Entity<Post>()
.IsSortableByPopularity();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- No dependencies.
-
net5.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Panner:
Package | Downloads |
---|---|
Panner.AspNetCore
Sorting and filtering made easy for your ASP.NET Core project! Go from a CSV input to a filtered/sorted IQueryable with no effort. |
GitHub repositories
This package is not used by any popular GitHub repositories.