Comments (7)
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.
Implemented in version 5.0.3
@RandomUser99 AsNoTrackingWithIdentityResolution is implemented as well.
from specification.
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.
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.
Fair enough; I wasn't thinking about the EF-specific bits so much as the basic spec functionality. Sounds good.
from specification.
I know it's only new in EF Core 5.0, but adding support for AsNoTrackingWithIdentityResolution() would be great.
from specification.
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, theGetById
has this formGetByIdAsync<T, TId>(TId id)
. Since theT
is not a parameter of the method, it can't be inferred, and it has to be written explicitly. TheTId
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 becomesGetByIdAsync<Store, int>
. Should we add an overload forint
,string
,Guid
, to simplify it for these types at least? - For the same reasons
GetBySpecAsync
has very bad usage in case we want to applyISingleResultSpecification
constraint. It's something likeGetBySpecAsync<SpecificationName,Entity>(specification)
. I have no better solution for it now.
- Since the
- Now that we added
ISingleResultSpecification
, it might be wise instead ofGetBySpecAsync
method to have more verbosely namedFirstOrDefaultAsync
andSingleOrDefaultAsync
methods. We should constrain only the Single method probably? Conceptually that makes more sense to me. - Do we want to apply
ISingleResultSpecification
to theGetBySpecAsync<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)
- Strange behavior when compare DateTime HOT 3
- Apply Distinct in specification query HOT 1
- How to combine specifications? HOT 17
- Implementing additional provider for Azure Storage Tables HOT 1
- Which Design Pattern is behind Evaluators? HOT 1
- Cannot unit test specifications which sort by a Smart enum HOT 2
- Apply Entity and Aggregate Domain-Driven Design approach to Specification and Repository HOT 2
- ardalis.specification.entityframeworkcore is missing NuGet package README file
- Many-to-Many w/ INNER JOIN HOT 1
- System.MissingMethodException: Method not found
- Ardalis.Specification.EntityFramework6.csproj doesn't target net7.0 or net8.0 HOT 5
- Allow combination of Specification with different projections HOT 6
- InMemory SearchExtension bug HOT 3
- Can ignore properties in Select Specifications? HOT 4
- How to use ExecuteUpdateAsync? HOT 8
- Search in result dto object instead of db class HOT 1
- Make the SaveChangesAsync call optional in RepositoryBaseOfT HOT 8
- Reading as 'System.Object' is not supported for fields having DataTypeName 'public.vector'
- Make repository methods protected to allow for subclassing HOT 2
- Search by value object's Value property: Translation error HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from specification.