Nice video explanation sir. Thank you! Based on my experience, I don't usually use query filter as they are hard to troubleshoot for junior dev within the team. I do IQueryable extension like static IQueryable WhereIf(this IQueryable query, bool condition, Expression predicate) { if (condition) return query.Where(predicate) else return query }
Thanks for great video. I am using query filter on project, and also we have problem in legasy parts with with QueryFilter that you show in this video, which no one noticed for a 3 year 🤣 So I have just fixed it, and I received compliment from all team
@@MilanJovanovicTech Well i injected a service which would get the companyId from the user Claims and then onModelCreating (dbContext) i would have something like this: modelBuilder.Entity().HasQueryFilter(e => !e.IsDeleted && e.CompanyId == _tenant.getCurrentTenantCompany()); worked like a charm
@@luan_maik yes . If ur user A companyId is 1 and user B companyId is 2, everytime you hit an endpoint it would query based on those. If u request to get your emloyees it would return all employees which their companyId is 1. Now another user makes the same request but his companyId is 2. It would return all users from companyId 2. its nice. Only thing you need to do is create a service which would get your companyId , inject on your DbContext , and on model create set a global query filter where CompanyId= your method that returns the companyId
I wrote a bunch of needless code to create a user manager derived class to add a global filter. I scrapped it and used your method which is much cleaner.
Nice topic, love the videos you do on EF Core, as you mentioned EF Core is a feature-rich library so there are lot's to explore, thank you for the efforts you put in these videos.
Nice video, Milan! I was just looking for a feature like this in EF. I initially thought about adding a condition in my base specification, but wanted an approach like Eloquent Scopes from Laravel. Thanks for the content.
This is a nice explanation of global filters, but as shipped the global filters feature is somewhat limited: In only applies to the base class in an object hierarchy, so for example if I have a base class of shape and superclasses of square and circle with a discriminator column I can't use the filter when I query square or circle - even if I have DBSets for each. It doesn't support non boolean filters. I'm accessing a table which is partitioned by user id and just fixed a bug where I'd forgotten to include the .Where(c => c.UserID == currentUserId) clause. It would be great to be able to include that as a global filter. It's a good start!
c.UserID == currentUserId is a boolean expression and is supported. See the example in the docs that does this, but uses _tenantId. Same idea though. But yes, they list a limitation: Filters can only be defined for the root Entity Type of an inheritance hierarchy.
@@pilotboba Sure I get that it's boolean, but since it's defined in the DBContext config how would it pick up the current user id? Can you give me a link to the docs page please. Thanks
@@davearkley7014 I think the most common way is to inject a service into your dbcontext that can provide the value. I think the docs just say make it a field on the context, which you would probably have to manually set. I don't see a full example there though.
@@davearkley7014NOt sure what you mean by a compiled DbContext. Did you mean compiled models? If that's what you mean, I don't see how this would affect that.
Very nice. I did not think to use this in support of not querying IsActive or IsDeleted, etc.. I would like another video on the topic of Filters - Dynamic Filter Pattern??
@@MilanJovanovicTech Specification Pattern. You covered it in the next video. I was thinking of a generic version of specification pattern. I do like your strongly typed and named version better, though. +100000
Hi Milan, Is there a way to apply a query filter just once to a group of entities, say, to all domain entities that implement ISoftDeletableEntity interface?
Nice video. Recently, I've been dealing with an alike scenario where I have to read from DB where the entries' deletion is soft. However, we were using the database first approach, So, is this valid when we are generating models from an existing DB ?
Hey, great video, I have a question, if you have Default query filters where you ignore IsDeleted != null, and you want to use it but want to show foreign elements like user who created it. Lets say user is deleted, in that case search Table that you wanted will always return null, so my question is can you limit DefaultQuery to depth, or you need to go with different aproach?
I saw two approaches to achieve that: In the Expression level ie. AddQueryFilter(t => isSoftDeleteDisabled_fromDI || !t.IsDeleted) // EF core became smart enough to translate this into a single expression or just ignore it if the flag isSoftDeleteDisabled_fromDI is true The other approach is building the expression itself dynamically according to some configurations but it's not safe I believe
You're channel is not growing just because you keep big stuff 😢. And not showing not class, method associate where you use. You blindly useing any methods, props,. Just show the code bro for once.
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
Nice video explanation sir. Thank you! Based on my experience, I don't usually use query filter as they are hard to troubleshoot for junior dev within the team. I do IQueryable extension like
static IQueryable WhereIf(this IQueryable query, bool condition, Expression predicate)
{
if (condition) return query.Where(predicate)
else return query
}
That's a very nice extension method!
Zgodna stvar. Volim ove forice koje na elegantan nacin smanje kod i potencijalne glavobolje kad nesto ne radi.
Subbed :)
Hvala Jovane 😁 Spremam video za ovu nedelju - Specification Pattern, koje je u slicnom fazonu
6:42 What?? You're holding out on me?? I want the answer now!! Lol, already subscribed hoss, thanks for the great and informative vids :)
I have to employ those special tactics to keep growing 🤣
Thanks for great video. I am using query filter on project, and also we have problem in legasy parts with with QueryFilter that you show in this video, which no one noticed for a 3 year 🤣
So I have just fixed it, and I received compliment from all team
You're time to shine, buddy! Good job 😁
EF query filter is a great feature. I did 2 multitenant(Based on CompanyId) applications using it and works well.
How did you pass the tenandId to the filter at runtime?
@@MilanJovanovicTech Well i injected a service which would get the companyId from the user Claims and then onModelCreating (dbContext) i would have something like this:
modelBuilder.Entity().HasQueryFilter(e => !e.IsDeleted && e.CompanyId == _tenant.getCurrentTenantCompany());
worked like a charm
@@stunna4498 but the entity configuration will be reprocessed every request to get the TenantId from request?
@@luan_maik yes . If ur user A companyId is 1 and user B companyId is 2, everytime you hit an endpoint it would query based on those. If u request to get your emloyees it would return all employees which their companyId is 1. Now another user makes the same request but his companyId is 2. It would return all users from companyId 2. its nice. Only thing you need to do is create a service which would get your companyId , inject on your DbContext , and on model create set a global query filter where CompanyId= your method that returns the companyId
Just be careful with your injected services that they have the appropriate scope or are thread safe otherwise that can wreak havoc with query filters!
I wrote a bunch of needless code to create a user manager derived class to add a global filter. I scrapped it and used your method which is much cleaner.
Sweet, glad you found this EF feature useful.
Nice topic, love the videos you do on EF Core, as you mentioned EF Core is a feature-rich library so there are lot's to explore, thank you for the efforts you put in these videos.
I'm trying to cover some of the lesser known feature, I have a few more to cover still 😁
Thanks for sharing!
I was about to ask how to ignore the global filter and answered.
I try to think of those edge cases 😁
Thanks Milan your videos helps me a lot.
Happy to hear that :)
Right to the point the point Milan , awesome as usual
Thank you so much, Ibrahim!
Nice video, Milan!
I was just looking for a feature like this in EF. I initially thought about adding a condition in my base specification, but wanted an approach like Eloquent Scopes from Laravel.
Thanks for the content.
Awesome, glad you found it useful!
Very nice feature :) i have never heard about it. I am going to apply it because we are using soft delete in each project :)
That's going to be very useful in your case
Useful content. Keep going Milan ;)
Thank you! 😁
This is a nice explanation of global filters, but as shipped the global filters feature is somewhat limited:
In only applies to the base class in an object hierarchy, so for example if I have a base class of shape and superclasses of square and circle with a discriminator column I can't use the filter when I query square or circle - even if I have DBSets for each.
It doesn't support non boolean filters. I'm accessing a table which is partitioned by user id and just fixed a bug where I'd forgotten to include the .Where(c => c.UserID == currentUserId) clause. It would be great to be able to include that as a global filter.
It's a good start!
c.UserID == currentUserId is a boolean expression and is supported. See the example in the docs that does this, but uses _tenantId. Same idea though.
But yes, they list a limitation: Filters can only be defined for the root Entity Type of an inheritance hierarchy.
@@pilotboba Sure I get that it's boolean, but since it's defined in the DBContext config how would it pick up the current user id? Can you give me a link to the docs page please.
Thanks
@@davearkley7014 I think the most common way is to inject a service into your dbcontext that can provide the value. I think the docs just say make it a field on the context, which you would probably have to manually set. I don't see a full example there though.
@@pilotboba Does injecting a service to access the value negate using a compiled DbContext?
@@davearkley7014NOt sure what you mean by a compiled DbContext. Did you mean compiled models? If that's what you mean, I don't see how this would affect that.
Thank you Milan 💯✊
Me pleasure 😁
Very nice. I did not think to use this in support of not querying IsActive or IsDeleted, etc.. I would like another video on the topic of Filters - Dynamic Filter Pattern??
I'll have to research what that pattern is 🤔
@@MilanJovanovicTech Specification Pattern. You covered it in the next video. I was thinking of a generic version of specification pattern. I do like your strongly typed and named version better, though. +100000
Hi Milan,
Is there a way to apply a query filter just once to a group of entities, say, to all domain entities that implement ISoftDeletableEntity interface?
Yeah, you can apply global query filters. But note that you can have one and only one query filter per entity.
Very nice, I’m curious about extension you’re visual studio when is debugging , you can say what’s name ?
It's just the VS debugger really
How to implement query filter for common soft delete based on a interface. Do i need to add in each config are is there any global filter?
You have to write some reflection, but it's going to override any other query filters you have in place.
Great video!
Thank you, Richard!
Nice video.
Recently, I've been dealing with an alike scenario where I have to read from DB where the entries' deletion is soft. However, we were using the database first approach, So, is this valid when we are generating models from an existing DB ?
Yes, I would say so
Hey Milan, nice video! As you mentioned Quantum computing, do you consider making a video about this topic in the future ?
I need to do a lot of studying around that topic, but since Microsoft released Q# I don't see why not give it a try. 😁
@@MilanJovanovicTech You can create special/bonus video free for current development mantras about this topic. Like a philosophy talk.
Hey, great video, I have a question, if you have Default query filters where you ignore IsDeleted != null, and you want to use it but want to show foreign elements like user who created it. Lets say user is deleted, in that case search Table that you wanted will always return null, so my question is can you limit DefaultQuery to depth, or you need to go with different aproach?
I think you'll have to use a different approach
@@MilanJovanovicTech that is what I feared. Thank you for the response, legenda si
just curious why you are not using swagger?
I do, just prefer dark UI of Postman
Hello Milan you don’t have any course on Udemy!?!
No, I don't. My course about Clean Architecture is available on my website: www.milanjovanovic.tech/pragmatic-clean-architecture
Is there a way to disable a specific query filter dynamically?
if (condition)
{
query = query.IngoreQueryFilters()
}
This will ignore all filters, for example tenant id and soft deleted, and other filters that could probably shouldn't be ignored in that context
@@amrosamy8232 I understand what you mean now. I don't think there's a way to achieve that.
I saw two approaches to achieve that:
In the Expression level ie.
AddQueryFilter(t => isSoftDeleteDisabled_fromDI || !t.IsDeleted) // EF core became smart enough to translate this into a single expression or just ignore it if the flag isSoftDeleteDisabled_fromDI is true
The other approach is building the expression itself dynamically according to some configurations but it's not safe I believe
Nice tool. Only soft remove. Other options don't come to mind.
Tenant Id
Active/inactive rows
Discriminator
Svratim da lupim jedan lajk i bacim podrsku svaka cast na kanalu i rastu :)
Hvala puno Nemanja! 😊
Sir can I request a video on how to send 50k to 100k emails (efficient approach) using Amazon SES please. thank you.
I have no idea how to do that. But probably some background job, that would queue the emails, and send them in batches.
You're channel is not growing just because you keep big stuff 😢. And not showing not class, method associate where you use. You blindly useing any methods, props,. Just show the code bro for once.
Yeah, not growing at all 😅😅