GithubHelp home page GithubHelp logo

Comments (7)

fiseni avatar fiseni commented on May 20, 2024 1

I gave a thought to this. We can do better. We won't define constraints to the specifications, but we'll play with the builder extensions. The extension methods will appear only if applicable. For example, if you define Specification<int>, the Include method won't appear at all.
There won't be an additional specification type, users will work with the same base class Specification, and the behavior will change based on the given context.

from specification.

fiseni avatar fiseni commented on May 20, 2024 1

Implemented in version 5.0.3
@RandomUser99 AsNoTrackingWithIdentityResolution is implemented as well.

from specification.

ardalis avatar ardalis commented on May 20, 2024

I'm not sure what generic constraints we could apply. I suppose we could restrict to only reference types or newable types but those seem arbitrarily restrictive. Technically you could use a Specification as a kind of validator or to encapsulate some range of values that that is complex to describe but simple to name. Imagine someone finds this useful as an example:

PrimesLessThanLimitSpec : Specification<int>
{
    public PrimesLessThanLimitSpec(int limit)
    {
        Query.Where(x => x.IsPrime() && x < limit);
    }
}

Other than that I think it's a good list.

from specification.

fiseni avatar fiseni commented on May 20, 2024

I meant to restrict it for reference types only where T : class. As a matter of fact, we're already confined in this context, e.g. the extension methods of EF have constraints for reference types.

public static IQueryable<TEntity> Include<TEntity>([NotNull] this IQueryable<TEntity> source, [NotNull][NotParameterized] string navigationPropertyPath) where TEntity : class;

Also, we have a generic constraint for the evaluator (T : class), since it utilizes these EF methods. In the existing infrastructure we have, if the specifications are not of reference type, it will throw on evaluation anyway. So, it's about being explicit of the intentions, and what is allowed and not.

Your example is great, but for those cases, we might define another type of specification that will work exclusively with BCL. Otherwise, we end up with a lot of implicit rules. The rule would be something like "Ok you can define it as of type int, but in that case you can't use Include, Search, AsNoTracking, etc. The users will have to have a deeper knowledge of the inner logic, so they don't end up with improper usages. The usage becomes ambiguous.

from specification.

ardalis avatar ardalis commented on May 20, 2024

Fair enough; I wasn't thinking about the EF-specific bits so much as the basic spec functionality. Sounds good.

from specification.

RandomUser99 avatar RandomUser99 commented on May 20, 2024

I know it's only new in EF Core 5.0, but adding support for AsNoTrackingWithIdentityResolution() would be great.

from specification.

fiseni avatar fiseni commented on May 20, 2024

We implemented all of the topics. Some additional stuff too, we'll provide more detailed documentation later on.

Few stuff that we might consider:

  • We're using Tuples for some of the properties in the specification. Should we add new specific types or not? It will add to clarity, but we'll have additional allocations.
  • Should we keep the non-generic repository as part of the package. If decide to keep, then we should clarify the following:
    • Since the T is not class generic, the GetById has this form GetByIdAsync<T, TId>(TId id). Since the T is not a parameter of the method, it can't be inferred, and it has to be written explicitly. The TId can be inferred, but since you have to write the other parameter, then you must write the second one too (either everything is inferred, or everything is written explicitly, you can't do just one of them). Then, the usage becomes GetByIdAsync<Store, int>. Should we add an overload for int, string, Guid, to simplify it for these types at least?
    • For the same reasons GetBySpecAsync has very bad usage in case we want to apply ISingleResultSpecification constraint. It's something like GetBySpecAsync<SpecificationName,Entity>(specification). I have no better solution for it now.
  • Now that we added ISingleResultSpecification, it might be wise instead of GetBySpecAsync method to have more verbosely named FirstOrDefaultAsync and SingleOrDefaultAsync methods. We should constrain only the Single method probably? Conceptually that makes more sense to me.
  • Do we want to apply ISingleResultSpecification to the GetBySpecAsync<T, TResult> (for the specifications which have selector defined). What would be the use case here?

@RandomUser99
As for your request. EF Core provides way too many options, we have to draw a line somewhere. Otherwise, we'll be way too EF Core centric. In case of an additional provider, we'll have to throw exceptions or just ommit all these features. But, on the other hand, I like AsNoTrackingWithIdentityResolution() :). Hard choice :)

from specification.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.