Giter Site home page Giter Site logo

Comments (4)

r2baka avatar r2baka commented on May 23, 2024

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.

fiseni avatar fiseni commented on May 23, 2024

Hi @lars-lindstrom

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.

fiseni avatar fiseni commented on May 23, 2024

Hey @lars-lindstrom,

Do you have any further questions or comments? Should we close this issue?

from specification.

fiseni avatar fiseni commented on May 23, 2024

Closing the issue due to inactivity. If you have further questions or comments, please feel free to reply.

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.