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

Specification usage improvement for preloaded data. #57

Closed
fiseni opened this issue Oct 27, 2020 · 1 comment
Closed

Specification usage improvement for preloaded data. #57

fiseni opened this issue Oct 27, 2020 · 1 comment
Labels
help wanted Extra attention is needed
Milestone

Comments

@fiseni
Copy link
Collaborator

fiseni commented Oct 27, 2020

These few days I dedicated some time to specifications, and I want to elaborate one more idea/feature. Then I finally have to get back to my real work :)

The usage (as it is for now), is quite straightforward for retrieving data from DB. You instantiate a specification, pass it to a repository method and that's it.
But, if you need to apply the specification to some collection that you already have in hand, then the usage is a bit tricky. Let's examine this example that you might have in some of your services

var spec = new CustomerSpecification(customerID);

var evaluator = new SpecificationEvaluator<Customer>();
var result = evaluator.GetQuery(SomeCollectionOfCustomers().AsQueryable(), spec).ToList();

This, at best looks ugly. First of all, the evaluator lives in the plugin package (in infrastructure project), so you won't be able to reference it here directly. You actually have to inject the ISpecificationEvaluator in the constructor, and work with that instead. And that means you have to wire up them in you IoC container. The next issue is that SpecificationEvaluator also will try to evaluate EF related IQueryable extensions like Include, Like etc. An in this scenario, that will throw an exception. And lastly, the usage is just complicated, too many conversions done by hand.

Suggestion:

  • The base package will have its own specification evaluator, which instead of operating on top of IQueryable, will operate on IEnumerable/List. And it will evaluate only standard operations (excluding Include, Like).
  • We should name this evaluator with distinct name, so it can be obvious that if specification contains Include or Like, they will be omitted during this evaluation.
  • Finally, the specification internally will hold an instance of this evaluator, and will expose the evaluation functionality through a public construct.

So, the usage would be something like:

var result = new CustomerSpecification(customerID).Evaluate(SomeCollectionOfCustomers);

I think this is much more clear, and will improve the usage of specification in these scenarios.
The Evaluate() method should have some better name to make it clear that some stuff are excluded.

@ardalis ardalis added the help wanted Extra attention is needed label Nov 2, 2020
@fiseni fiseni added this to the 5.0 milestone Mar 2, 2021
@fiseni
Copy link
Collaborator Author

fiseni commented Mar 5, 2021

Implemented in version 5.0.3

@fiseni fiseni closed this as completed Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants