Skip to content
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

Add support for extending default evaluator list #328

Merged
merged 1 commit into from
May 26, 2023

Conversation

fiseni
Copy link
Collaborator

@fiseni fiseni commented May 15, 2023

closes #308

The PR enables us to more easily extend the evaluators list. We have exposed the state as protected.

public class MyPartialEvaluator : IEvaluator
{
  private MyPartialEvaluator () { }
  public static MyPartialEvaluator Instance { get; } = new MyPartialEvaluator();

  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 MySpecificationEvaluator() : base()
  {
    Evaluators.Add(MyPartialEvaluator .Instance);
  }
}

@fiseni fiseni requested a review from ardalis May 16, 2023 14:17
@ardalis
Copy link
Owner

ardalis commented May 26, 2023

In your example here, how is MySpecificationEvaluator discovered? Is there some registration logic or some reflection-based discovery or what?

@fiseni
Copy link
Collaborator Author

fiseni commented May 26, 2023

Our base implementation has a ctor overload that accepts ISpecificationEvaluator.
Usually, you'll always define your Repository (so you can pass your specific DbContext), and that's the place where you'll wire it up.

public class Repository<T> : RepositoryBase<T>, IRepository<T> where T : class
{
  public Repository(AppDbContext dbContext) 
    : base(dbContext, MySpecificationEvaluator.Instance)
  {
  }
}

Note: In the example I have defined it as a singleton instance since there is no real reason why you'd want to create a new object. But, if the users have stateful evaluators, then nothing prevents them from newing up. Also, they may register it in DI with a desired lifetime if they wish, and accept it in ctor.

builder.Services.AddScoped<ISpecificationEvaluator, MySpecificationEvaluator>();

@ardalis ardalis merged commit 0805a1e into main May 26, 2023
@fiseni fiseni deleted the expose-evaluators-state branch July 24, 2023 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for extending default evaluator list
2 participants