Comments (4)
do I have to use DbContext (which I try to avoid)
I do know the answer to that is no.
You can do something like q.Where(p => (p.Firstname + ' ' + p.Lastname).Contains(searchname));
That doesn't solve problems like the duplication of code to calculate the fullname, but it does help you avoid DbContext.
For the general case like this, where I want to avoid DbContext, but still have deeper flexibility on my queries, I just made a ListBySQL method on the repository, which comes in very handy for difficult use cases for the Query. but you kinda have to like SQL.
public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
{
...
public async Task<IReadOnlyList> ListBySQLAsync(string sql, object[] parameters)
{
return await _dbContext.Set<T>().FromSqlRaw(sql, parameters).ToListAsync();
}
Alternatively, you could also create a calculated field on the data set where a column FullName = Firstname + ' ' + Lastname
https://learn.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-ver16
from specification.
You can have the following specification
public class CustomerSpec : Specification<Customer>
{
public CustomerSpec(string someValue)
{
Query.Where(x => EF.Property<string>(x, "Fullname").Contains(someValue));
}
}
Also, you have to configure the shadow properties in your EF Core
configuration. It would be something as follows:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Property<string>("Fullname");
}
You don't depend on DbContext
in the specification, but still, you depend on the static EF
class which is defined in Microsoft.EntityFrameworkCore
. So, you'll depend on EF Core
, if that's what you mean.
If you're asking for a mechanism to abstract that dependency, then we don't plan to implement that, unfortunately. It can be implemented through reflection and dynamically build the expression. But, we're trying to avoid reflection as much as possible, especially now that .NET is serious about AOT. Also, there is no good way to generalize the usage. You're thinking only in terms of Contains
, but the expression inside the Where
can be anything (e.g. Any
, Equals
, etc.). What would the new API look like?
Specifications are intended to operate against a defined domain model. The shadow properties, by definition, are not part of it. You might reconsider the need for a shadow property. What's exactly your case? I assume you have such a column in the database right? Then, why not add the property to the model?
from specification.
Hey @lars-lindstrom,
Do you have any further questions or comments? Should we close this issue?
from specification.
Closing the issue due to inactivity. If you have further questions or comments, please feel free to reply.
from specification.
Related Issues (20)
- Request to multi-target EntityFramework6.csproj and include net6.0 HOT 3
- Query.Dstinct() HOT 1
- Update LangVersion to C# 10 or 11
- Update conventions and styling
- Update Sample projects
- Remove obsolete API
- Update pipelines.
- Including nullable/optional navigation property causes only non-nullable connected objects to be returned HOT 3
- Unable to apply Take() to a navigation property HOT 5
- Refactor and improve integration test fixtures.
- Why do the Update and Delete not returning anything? HOT 3
- Logging in the Package HOT 4
- Feature Request: Deletion of entities via specification HOT 5
- Query to return anonymous type HOT 4
- Question: Can I some how join table "B" when my entity T does not have a navigation property to "B" ? HOT 2
- Expression Evaluation HOT 8
- Simplify Cache usage HOT 5
- DbSet<T>.WithSpecification together with ISpecification<T, TDestination> throws SelectorNotFoundException HOT 4
- Conceptual question about the ORM specific implementations HOT 2
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.