Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@addkim7889 Жыл бұрын
Really a clever way to apply condition, making the input as a Func instead of bool would make it more adaptable, plus the Expression Api is used for Linq, and EfCore, and other ORM, used that to compose the actual query.
@MilanJovanovicTech Жыл бұрын
Func would just add complexity since it would always be () => somethingThatReturnBool; What's the advantage over returning a bool directly?
@addkim7889 Жыл бұрын
@@MilanJovanovicTech you are right that it would be more complicated, for more complicated filtering that would be cleaner if extracted into a function.
@JavierAcrich Жыл бұрын
If I call the extension method WhereIf, I would have the predicate as a first parameter and the condition second. Otherwise I would call it 'IfWhere'. So it has concordance with the parameter order.
@MilanJovanovicTech Жыл бұрын
Good point, honestly. Haven't thought about it!
@Knusperfunk Жыл бұрын
I originally had it the other way around, but having the conditions as the first parameter makes them nicely line up when chaining a lot of filters, which gives a slightly better overview. 🤷♂️ 🥔🥔🍅🍅
@majora2007-joe Жыл бұрын
Great tip, I usually put if statements in my repo, but this makes me realize I'm under utilizing extensions yet again.
@MilanJovanovicTech Жыл бұрын
Extension methods are awesome 😁
@pilotboba Жыл бұрын
I always compose my queries this way, but adding an extension method is a great tip.
@MilanJovanovicTech Жыл бұрын
Awesome, glad you liked it :)
@drozdgabriel Жыл бұрын
I always learn new things with your videos 😄 Always a new perspective on things that are familiar to me. It's great! 😅
@MilanJovanovicTech Жыл бұрын
That's awesome to hear! Thank you :)
@MortvmMM Жыл бұрын
Nice extension method :) A nice fluent alternative to building your queries. The basic way of doing it is marking your query AsQueryable and adding to it like if (condition) query = query.Where()
@MilanJovanovicTech Жыл бұрын
There's no need to do AsQueryable if you get the Queryable directly from the DbContext
@winchester2581 Жыл бұрын
Simple, but awesome and useful video, used this approach on one of the previous projects :) Also when I worked there, I've got a hint of not using Where statement in the loop, 'cuz it's gonna generate a lot of queries which is not really convenient in terms of performance, so we used LinqKit for that (to combine specific filters to apply to query) So I think that such videos really helps developers to write LINQ expressions wisely, my kudos)
@MilanJovanovicTech Жыл бұрын
I'll have to research about LinqKit 🤔🤔
@winchester2581 Жыл бұрын
@@MilanJovanovicTech great! I guess that we can just write our own extension methods with the same logic but I used it as it was introduced in our project :)
@NAngelGr Жыл бұрын
You are a star mate!! Thanks for your tutorials!!
@MilanJovanovicTech Жыл бұрын
Happy to help
@mocococo2877 Жыл бұрын
Thank you and again greetings from Bulgaria. As per your instruction and per my liking I smashed the like button.
@MilanJovanovicTech Жыл бұрын
Awesome, thank you! 💪😁
@NavedDeshmukh Жыл бұрын
Great approach. such simple tweak but adds so much clarity to code
@MilanJovanovicTech Жыл бұрын
I only recently found out about this, but it's really helpful
@mohamed-hassan- Жыл бұрын
i used it in various queries, thanks for the video 👏
@MilanJovanovicTech Жыл бұрын
Nice! I knew I can't be the only one 😁
@88spaces9 ай бұрын
I like the extension method. I might use it soon. You've also given me ideas about how to reduce code using extension methods and EF. Thanks.
@MilanJovanovicTech9 ай бұрын
Glad it was helpful!
Жыл бұрын
Interesting approach. As every permutation of conditions in code would generate new SQL, and therefor new query plans... that again could impact performance. It would be interesting to hear a DBAs view on this. Might be a good thing, and might be a bad thing. I am actually not sure.
@MilanJovanovicTech Жыл бұрын
How is the query plan cache? 🤔 My thinking is if these queries are frequently ran anyway, they would constantly be in the cache regardless. Although your concern is perfectly valid.
@nepalxplorer Жыл бұрын
Very useful, adding it on my project 😛
@MilanJovanovicTech Жыл бұрын
Great!
@mylesdavies9476 Жыл бұрын
Hi Milan, really enjoyed your tech talk on modular monolith. It would be great if you could do a video outlining the basic building blocks, i.e. your preferred approach for bringing the modules together and explaining some of the core concepts
@MilanJovanovicTech Жыл бұрын
Oh I'm planning to 😁 In fact, more than one video. And I'll even be open sourcing an existing implementation of mine.
@mylesdavies9476 Жыл бұрын
@@MilanJovanovicTech awesome, look forward to it 😁
@ayasaber25675 ай бұрын
Thanks for the explanation, 'where' part will be ignored (incase the filter variable are null or empty) in the statement whether we use the .Where twice or use the extension method, so what's the benefit from the extension method rather than reusability?
@MilanJovanovicTech5 ай бұрын
From my perspective, it makes it easier to compose multiple optional conditions. Imagine you want to have 5-10 optional filters that we want to apply only if the filter is specified by the user. It could be one big WHERE statement, with null check + condition -or- It could be many WHERE statement with null check + condition -or- Using the approach presented here to make the above a bit easier for the developer
@Mohamad-jz1qz Жыл бұрын
Hi, I want to know that if we have a large amount of data, this approach won't be failed? because firstly your are going to select all and then filter outside of sql. Thanks in advanced.
@MilanJovanovicTech Жыл бұрын
It's still a filter in the DB
@arunbm123 Жыл бұрын
another brilliant tutorial !!!
@MilanJovanovicTech Жыл бұрын
Thanks!
@nico11323 Жыл бұрын
Hi, there is a way to remove the check empty in the generate sql query?
@MilanJovanovicTech Жыл бұрын
Nope, that's how EF Core translates it to SQL
@majormartintibor Жыл бұрын
Good one Milan, thanks!
@MilanJovanovicTech Жыл бұрын
Glad you liked it!
@Tamer_Ali Жыл бұрын
Is there a better approach (generic) to filter data from 2 lists to save to database? e.g: list1 is the existing users from database list2 is the users that the admin want to add, update or delete after comparing with the existing users in the database
@MilanJovanovicTech Жыл бұрын
Not that I know of, honestly 🤔
@stefan-d.grigorescu Жыл бұрын
Convert both to HashSets if you can have all the entries in memory, then use set1.Exclude(set2) or something like that. If the persisted entities are too many to be worth it loading them all into memory at once, you have to check foreach item in list1 if(dbSet2.FirstOrDefault...). Also consider checking the local cache of dbContext before checking the DbSet That's all know for now about this. Choosing one approach or another should be based on some testing. Or just nvm and keep it simple if it already performs ok
@fitstrong167 Жыл бұрын
Just a random suport comment .... great staff keep em coming ... ajde :)
@MilanJovanovicTech Жыл бұрын
You're the best!
@codingbloke Жыл бұрын
Very nice bit of technique.
@MilanJovanovicTech Жыл бұрын
Thank you, sir!
@tsatsosgregorian9410 Жыл бұрын
Hello Milan, great video again!! Any good advice or video for efcore bulk insert?
@MilanJovanovicTech Жыл бұрын
Not really, I would consider SqlBulkCopy
@hassanOnYoutube Жыл бұрын
Love from Pakistan ! love that extension part.
@MilanJovanovicTech Жыл бұрын
Thanks a lot, Hassan! :)
@WahidRezgui Жыл бұрын
Hello first of all thanks for the video . Ihave one question do you have any idea how to make the operator like =,>,< generic and selected by user like the sample you provided Age =10 or age>10
@MilanJovanovicTech Жыл бұрын
Generic in what way?
@WahidRezgui Жыл бұрын
I meant the operator also is given by the user so searchvalue, operator and field are given as parameter something like this var res= dbcontext.entity.where(x=>x.fieldA operator searchedvalue) So here the search is done on fieldA and also operator is a property set by the function so finally the searchfunction will take fieldA . Operator and searched value as a parameter
@리오날정 Жыл бұрын
Thanks Milan😊
@MilanJovanovicTech Жыл бұрын
You're very welcome :)
@AhmedKhaled-g8t Жыл бұрын
Amazing channel
@MilanJovanovicTech Жыл бұрын
Thank you very much!
@volodymyrliashenko1024 Жыл бұрын
I'm a big fun of this extension method and I have different versions of it Wondering, how devs who like Dapper implement such logic.
@MilanJovanovicTech Жыл бұрын
In SQL 😁😁
@volodymyrliashenko1024 Жыл бұрын
@@MilanJovanovicTech the response is too short :) Would be nice to see like an example.
@waleedbensumaidea3947 Жыл бұрын
You're amazing
@MilanJovanovicTech Жыл бұрын
Thanks!
@ahmedh2482 Жыл бұрын
U are awesome man
@MilanJovanovicTech Жыл бұрын
Thanks a lot! :)
@joehernandez3231 Жыл бұрын
This is great. I just added this WhereIf extension method to a new API I started working on. Thanks!
@MilanJovanovicTech Жыл бұрын
Awesome, glad you found it practical 😁
@Tamer_Ali Жыл бұрын
Thanks Milan, you always amazing me 👍.
@MilanJovanovicTech Жыл бұрын
Happy to be of help 😁
@VishnuKumar-fq1zo Жыл бұрын
How did you managed to print actual query in the console?
@MilanJovanovicTech Жыл бұрын
There's a method to configure EF output
@VishnuKumar-fq1zo Жыл бұрын
@@MilanJovanovicTech Can share code for doing that?
@meirkr Жыл бұрын
Does OData take care of empty filter internally?
@MilanJovanovicTech Жыл бұрын
I have no idea
@TheSead750 Жыл бұрын
very nice😄
@MilanJovanovicTech Жыл бұрын
Thanks! Got any EF extension method of your own?
@andresjimenez3140 Жыл бұрын
Great content !!! One question, where would you implement this extension method in a clean architecture? Is it in infrastructure layer?
@ugochukwuumerie6378 Жыл бұрын
Exactly, cause that's a reasonable place to do that. Ef core itself goes in there.☺️
@MilanJovanovicTech Жыл бұрын
I guess so, yeah. Or Persistence - if you have a separate layer for that.
@Alupl Жыл бұрын
I'm using this Extension method since .NET Framework 4 :D Today in .NET Core 8.0 I'm surprised there still isn't such build in method (or I don't know of it). Funny thing is that very few programmers know this but as you showed in your video, It's so much simpler and effective. I don't get it why this isn't a standard ... Anyway thanks for your vid - I didn't remember the syntax and came here :P
@MilanJovanovicTech Жыл бұрын
I assume it's not added because you can achieve the same using just Where
@mhar583810 ай бұрын
Hey Milan thanks for the video - just a question: in clean code architecture which project/folder do you suggest to put such extensions? Domain? Application? Persistance?
@MilanJovanovicTech10 ай бұрын
Wherever you'd need them to write LINQ queries
@mhar583810 ай бұрын
@@MilanJovanovicTech I said where not when! I mean do you put the extension method classes in domain project or persistance project? sometimes you may want to write some business logic in domain / application layer. but some people recommend to put Queryable operations in repository or persistance layer. What is your recommendation?
@krunalsisodiya Жыл бұрын
I find it very helpful
@MilanJovanovicTech Жыл бұрын
Glad it was helpful!
@haythambiadse5137 Жыл бұрын
can you think of situations it could help to improve performance? also ,can you please do a video for queries that runs inside a loop ... ? i bet you have great tips over this :)