Giter Club home page Giter Club logo

Comments (10)

siegeon avatar siegeon commented on June 10, 2024

I do something like what you are talking about. But you need to define the filter first. Since the filters are dynamic you can change the filter value on the fly. I have a static singleton class that contains my company level filtering, it is set per request.

modelBuilder.Filter("CompanyId", (ICompanyOwned t) => t.CompanyId, () => CompanyRequestManager.Instance.CompanyId);

In my application the request to the service always contains the CompanyId so that I can set the value per request. If you want to get that value from the database on every request then I would suggest toggling the filter on and off.

                   using (var context = new TestContext())
        {
            context.DisableFilter("CompanyId");

            CompanyRequestManager.Instance.CompanyId = context.CompanyEntity.GetIdSomehow()

            //  Re-enable and check again
            context.EnableFilter("CompanyId");

            //run filtered query.
        }

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 10, 2024

You can create your filters before you actually set the value to filter on. Take a look at how the "BlogEntryFilter" filter is configured in ExampleContext.cs of the Example project. It is creating a filter against a static property "CurrentAccountID". The value for that property just needs to be set before you actually execute a query that would invoke the filter (otherwise, in this case, it would use the default value for a Guid).

And it's not necessary to disable filters before doing that.

from entityframework.dynamicfilters.

siegeon avatar siegeon commented on June 10, 2024

jcachat If you did not disable the filter before running the query wouldn't it filter all the results by guid.default?

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 10, 2024

No, the value to filter on isn't read until the filter is actually executed - and it's read each time it's executed (and set into a sql parameter). That is what makes it dynamic vs. the EntityFramework.Filters project which reads the values when the query is COMPILED. So when you use a delegate to get the value of a filter parameter (as in this example), you can freely change it any time you want and the filter will always use the current value each time it executes.

Disabling the filter actually just sets a bool property value on the query that causes the conditions (which have already been compiled into the query) to not be applied at query execution time. If you use SQL Profiler to capture the sql when the filter is disabled vs. enabled, you'll see that is the only difference is in the value of a sql parameter named like "@DynamicFilterParam_[filtername]_DynamicFilterIsDisable".

from entityframework.dynamicfilters.

siegeon avatar siegeon commented on June 10, 2024

Let me rephrase, and please correct me if I am wrong. Looking at the OP I understood that he wanted to query the database for the Company Id before the filter that needed the Id is set up. In that instance the first query against the databases to get the Company Id would be filtered by Guid.Empty when the query is executed the first time.

from entityframework.dynamicfilters.

jcachat avatar jcachat commented on June 10, 2024

Oh I see what you mean now. I was assuming that the query to get the Company ID is being done against something that is not filtered (some kind of configuration table that contains the Company ID) while the filters are set on entities that have a CompanyID column/FK.

It looks like your first post is much more detailed than the email git sent me so I didn't see all that (maybe you edited). So in your example, if your call to:

CompanyRequestManager.Instance.CompanyId = context.CompanyEntity.GetIdSomehow()

does a query against an entity that is ICompanyOwned, then yes, you need to disable the filter first. If not, you do not need to disable.

That is a good point to make but otherwise it looks like we answered the original question the same way.

from entityframework.dynamicfilters.

siegeon avatar siegeon commented on June 10, 2024

Yeah I totally edited my first response after re-reading the OP. =)

from entityframework.dynamicfilters.

jwillmer avatar jwillmer commented on June 10, 2024

Thanks for the fast response! Your code example and the discussion helped me a lot :-)

from entityframework.dynamicfilters.

jwillmer avatar jwillmer commented on June 10, 2024

I created a singelton class that queries for the company id but I get an exception.

The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method..

I understand the problem, the company id is instantly retrieved:

modelBuilder.Filter("GlobalCompanyFilter", (ICompanyReference r) => r.CompanyId, SingletonPerRequest.Current.CompanyId);

And another problem is that disabling the filter wouldn't stop retrieving the value (if retrive is executed in the get of the property and not the constructor) because as @jcachat said:

Disabling the filter actually just sets a bool property value on the query that causes the conditions (which have already been compiled into the query) to not be applied at query execution time.

@siegeon how did you resolve this problem?

from entityframework.dynamicfilters.

jwillmer avatar jwillmer commented on June 10, 2024

I think I solved it:

modelBuilder.Filter("GlobalCompanyFilter", (ICompanyReference r) => r.CompanyId, () => SingletonPerRequest.Current.CompanyId);

from entityframework.dynamicfilters.

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.