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

Adding evaluator for IgnoreAutoIncludes #291

Closed
jeffreymonroe opened this issue Aug 24, 2022 · 2 comments
Closed

Adding evaluator for IgnoreAutoIncludes #291

jeffreymonroe opened this issue Aug 24, 2022 · 2 comments
Assignees

Comments

@jeffreymonroe
Copy link

I use .AutoInclude() sparingly when configuring an EF model. These are usually for relationships which utilize .HasOne().

Any thoughts on extending the Specification evaluator to add IgnoreAutoIncludes()? This would allow for loading an object graph for delete using .includes and circumventing cyclic exceptions.

@fiseni
Copy link
Collaborator

fiseni commented Jul 22, 2023

Hi @jeffreymonroe,

Sorry for the late reply here, we missed this issue.
We're a bit cautious about adding new features that are not widely used. Once we have many requests, then we include them in the library. But, we tried to make the specs and the infrastructure fairly extendable, so users can write their extensions for almost anything. You can find examples of how to do that in our sample projects. Anyhow, let me provide a specific example here.

Create specification extensions

public static class SpecExtensions
{
    public static ISpecificationBuilder<T> IgnoreAutoIncludes<T>(this ISpecificationBuilder<T> builder) where T : class
    {
        builder.Specification.Items.TryAdd("IgnoreAutoIncludes", true);
        return builder;
    }
}

Create the evaluator for it, and hook it up to your repository

public class IgnoreAutoIncludesEvaluator : IEvaluator
{
    public bool IsCriteriaEvaluator { get; } = true;

    public IQueryable<T> GetQuery<T>(IQueryable<T> query, ISpecification<T> specification) where T : class
    {
        if (specification.Items.ContainsKey("IgnoreAutoIncludes"))
        {
            query = query.IgnoreAutoIncludes();
        }

        return query;
    }
}

public class AppSpecificationEvaluator : SpecificationEvaluator
{
    public static AppSpecificationEvaluator Instance { get; } = new AppSpecificationEvaluator();

    public AppSpecificationEvaluator() : base()
    {
        Evaluators.Add(new IgnoreAutoIncludesEvaluator());
    }
}
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, AppSpecificationEvaluator.Instance)
    {
    }
}

And now you can simply use it from any specification.

public class CustomerSpec : Specification<Customer>
{
    public CustomerSpec()
    {
        Query.IgnoreAutoIncludes();
    }
}

Let me know if this helps. I'm closing the issue, feel free to re-open it if you have further questions.
If we get another similar request we might include it in the library in the future.

@jeffreymonroe
Copy link
Author

Thanks for the reply and the work you all have done with this. I will implement your suggested change. Cheers.

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

No branches or pull requests

2 participants