Slavapp.Extensions.EF 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Slavapp.Extensions.EF --version 1.0.0
NuGet\Install-Package Slavapp.Extensions.EF -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Slavapp.Extensions.EF" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Slavapp.Extensions.EF --version 1.0.0
#r "nuget: Slavapp.Extensions.EF, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Slavapp.Extensions.EF as a Cake Addin
#addin nuget:?package=Slavapp.Extensions.EF&version=1.0.0

// Install Slavapp.Extensions.EF as a Cake Tool
#tool nuget:?package=Slavapp.Extensions.EF&version=1.0.0

Slavapp.Extension.EF

A set of extension methods for EntityFramework and IQueryable interface for quick sorting and filtering development

Description

Project originated as a set of methods for quick query string translations into expression trees valid enough to be used as where and order by clauses, generated by EntityFramework. Main purpose of it is to speed up development process by designing a simple and fast way to transform a HTTP query string (or any other string-based set of parameters) into corresponding expression trees, and in the end, into required SQL queries with very limited effort and code produced.

Getting Started

Dependencies

No dependencies

Installing

Install-Package Slavapp.Extensions.EF

Usage

To create a new map of sort expressions for a EF model simply define new class:

public class RegistrationSorting : BaseSorting<Registration>
{
    public RegistrationSorting()
    {
        SetSorting("RegistrationNumber", x => x.RegistrationNumber);
        SetSorting("FullName", x => x.LastName + " " + x.FirstName);
        SetSorting("PaymentBalance", x => x.PaymentBalance);
        SetSorting("Modified", x => x.Modified);
        SetSorting("Created", x => x.Created);
        SetSorting("AssignedTo", x => x.AssignedTo);
        SetSorting("JobCity", x => x.Addresses.OfType<JobAddress>().FirstOrDefault().City);
    }
}

SetSorting method accepts 2 arguments: a name and an expression. Name is a parameter which can be arbitrary string, not really connected to EF model, its something that client is responsible to know. When defining a sort option we need to specify how this name maps to EF model properties.

  1. ex.
SetSorting("RegistrationNumber", x => x.RegistrationNumber); 

will bind RegistrationNumber name to RegistrationNumber property, which means that we can sort by RegistrationNumber property using RegistrationNumber name.

SetSorting("FullName", x => x.LastName + " " + x.FirstName); 

on the other hand will bind FullName name to a result of LastName and FirstName concatenation, which means that if we specify FullName parameter to sort on it will be translated to LastName + " " + FirstName expression. There are many other, more complex ways to define how to sort given models, for instance by using child objects:

SetSorting("JobCity", x => x.Addresses.OfType<JobAddress>().FirstOrDefault().City);

will use Addresses child collection to sort on Address.City property by simple usage of JobCity parameter

Filters can be defined as follows:

public class RegistrationFilter : BaseFilter<Registration>
{
    public RegistrationFilter()
    {
        SetFilter("RegistrationNumber", x => x.RegistrationNumber);
        SetFilter("FullName", x => x.LastName + " " + x.FirstName);
        SetFilter("PaymentBalance", x => x.PaymentBalance);
        SetFilter("Modified", x => x.Modified);
        SetFilter("Created", x => x.Created);
        SetFilter("AssignedTo", x => x.AssignedTo);
        SetFilter("JobCity", x => x.Addresses.OfType<JobAddress>().FirstOrDefault().City);
    }
}

Once defined, filters and sortings can be used as IQueryable extensions:

...
using Slavapp.Extensions.EF;

public IHttpActionResult GetRegistrations(string sortCols = null, string filter = null)
{
    var regs = this.dbContext.RegistrationSet
                .WithSort(sortCols)
                .WithFilter(filter)
                .ToList();
    return regs;
}

Example query strings:

?sortCols=FullName:asc&filter=null
?sortCols=JobCity:asc&filter=FullName:~foo

By default following set of symbols will be used to parse the query string:

  • StartsWith = "^"
  • Equal = "*"
  • GreaterThan = ">"
  • LesserThan = "<" -NotEqual = "!"
  • Contains = "~"
  • SortAscending = "asc"
  • ColumnSeparator = "|"
  • ValueSeparator = ":"

They can be defined differently by implementing ISymbols interface and by registering them by calling

SetSymbols(new OtherSymbols())

in BaseFilter or BaseSorting inherited classes, whenever they need to be used.

Authors

Slawomir Gnatonski

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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
1.0.1 993 6/18/2018
1.0.0 867 6/13/2018

Initial release.