Skip to content
This repository has been archived by the owner. It is now read-only.
Filters implementation for Entity Framework
C#
Branch: master
Clone or download
jbogard Merge pull request #19 from pedroreys/FixNoConfigurationIssue
Skip FilterQueryVisitor when can't find config metadata
Latest commit c100a6e Aug 31, 2016
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Skip FilterQueryVisitor when can't find config metadata Dec 18, 2014
.gitattributes Git attributes file May 22, 2014
.gitignore Initial commit May 22, 2014
LICENSE Initial commit May 22, 2014
README.md add csharp syntax highlighting Sep 29, 2014

README.md

EntityFramework.Filters

Filters implementation for Entity Framework, on NuGet as the EntityFramework.Filters package.

Filters allow you to define a parameterized filter at configuration time. At runtime, you turn on the filter and apply parameters, and every query for that entity will include the filter.

Configuration

The FilterInterceptor must be registered with Entity Framework, either through a DbConfiguration class:

public class ExampleConfiguration : DbConfiguration
{
    public ExampleConfiguration()
    {
        AddInterceptor(new FilterInterceptor());
    }
}

Or through the OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    DbInterception.Add(new FilterInterceptor());
}

Examples

Filters are first defined, then configured. You define the filter against a single entity:

modelBuilder.Entity<Listing>()
    .Filter("ActiveListings", c => c.Condition<ListingStatus>(
        listing => listing.Status == ListingStatus.Active));

Or against a set of entities that match a type (interface or base class):

modelBuilder.Conventions.Add(
    FilterConvention.Create<IAgencyEntity, int>("Agency", (e, agencyId) => e.AgencyId == agencyId);

Filters are then enabled and parameter values filled in on a DbContext basis:

dbContext.EnableFilter("ActiveListings");
dbContext.EnableFilter("Agency")
    .SetParameter("agencyId", _userContext.CurrentUser.AgencyId);

Filters are disabled by default, and you can disable them selectively after enabling:

dbContext.DisableFilter("ActiveListings");

The filter names must be unique, and filter parameter names are matched by the parameter name you supply to the filter definition's expression.

Common Usages

Filters are used to define a predicate that will be applied to every entity in a DbContext, without a developer needing to remember to include it for every query. Common applications include:

  • Security
  • Multi-tenancy
  • Logical data partitioning
  • Soft deletes
  • Active/inactive records

There are some limitations, however:

  • No access to context for complex joins
  • Collection properties not available
You can’t perform that action at this time.