-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: How to extend or define evaluators #231
Comments
Hey @fretje Here is what you do. public class MySearchEvaluator : IEvaluator
{
private MySearchEvaluator() { }
public static MySearchEvaluator Instance { get; } = new MySearchEvaluator();
public bool IsCriteriaEvaluator { get; } = true;
public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class
{
// Write your desired implementation
return query;
}
}
public class MySpecificationEvaluator : SpecificationEvaluator
{
public static MySpecificationEvaluator Instance { get; } = new MySpecificationEvaluator();
private static IEvaluator[] evaluators = new IEvaluator[] {
WhereEvaluator.Instance,
MySearchEvaluator.Instance,
IncludeEvaluator.Default,
OrderEvaluator.Instance,
PaginationEvaluator.Instance,
AsNoTrackingEvaluator.Instance,
IgnoreQueryFiltersEvaluator.Instance,
AsSplitQueryEvaluator.Instance,
AsNoTrackingWithIdentityResolutionEvaluator.Instance
};
private MySpecificationEvaluator()
: base(evaluators)
{
}
} In your application, usually, you have to create your own repository class so you can pass your DbContext. We have a constructor overload that accepts public interface IRepository<T> : IRepositoryBase<T> where T : class
{
}
public class Repository<T> : RepositoryBase<T>, IRepository<T> where T : class
{
public Repository(AppDbContext dbContext)
: base(dbContext, MySpecificationEvaluator.Instance)
{
}
}
public class AppDbContext : DbContext
{
} |
Thanks again for the clear explanation! I guess this issue can stay open until this is up on the docs site? (I might have a stab at making a PR for it once I find the time ;-)). |
Sure, any help is welcome. |
Hi @fiseni, public static MySpecificationEvaluator Instance { get; } = new MySpecificationEvaluator();
InnerException:
Unfortunatelly I have no idea what is the problem here. |
@fiseni This is a working specification evaluator: public sealed class MySpecificationEvaluator : SpecificationEvaluator
{
public static MySpecificationEvaluator Instance { get; } = new MySpecificationEvaluator();
private MySpecificationEvaluator()
: base(new IEvaluator[]
{
WhereEvaluator.Instance,
MySearchEvaluator.Instance,
IncludeEvaluator.Default,
OrderEvaluator.Instance,
PaginationEvaluator.Instance,
AsNoTrackingEvaluator.Instance,
IgnoreQueryFiltersEvaluator.Instance,
AsSplitQueryEvaluator.Instance,
AsNoTrackingWithIdentityResolutionEvaluator.Instance
})
{
}
} |
Hello, I stumbled across issue #291 and this issue because I was looking for a way to ignore auto includes within our application. The evaluators are a great way to do this, thanks for this feature! From only looking at the docs I didn't make the connection that an evaluator can be used to ignore auto includes. That's why I think it would be better to update the current docs (because these don't show us a "real" scenario), with with the example shared in #291. If you agree, I'm happy to create a PR for this. It would be similar to "Example: Configure caching behavior through specification builder extension method" in http://specification.ardalis.com/extensions/extend-specification-builder.html |
I see an important page in the documentation is still missing:
https://ardalis.github.io/Specification/extensions/extend-define-evaluators.html
And I didn't find any issue about it?
Maybe also an example in the sample app of how to override a single evaluator would be handy.
A good example might be a custom SearchEvaluator which uses the progresql specific EF.Functions.ILike in stead of the default EF.Functions.Like? (Related to #230 ==> I was actually looking up the documentation to try and create a specific evaluator for this, but kind of hit this wall...)
The text was updated successfully, but these errors were encountered: