3 ms·
Follow up on my usage on Linq ( fyi) Now i design most of my pages through 1 service/method with a lot of if's and else and re-use that method all the time Fo
by NicoJuicy 10y ago
Follow up on my usage on Linq ( fyi)
Now i design most of my pages through 1 service/method with a lot of if's and else and re-use that method all the time
For example, a ecommerce-site i made has one function for querying the products, paging, searching, sorting by X, ...). Although, it get's rather complex lately, i wouldn't have it any other way. ( with complex, i mean i now also grouping them by category and category code, a category order number, a product order number, by product title ( varying by the current language), ...
And yes, it' seperated in my service layer ;) and no, i won't change it because this is the fastest way untill now.
Alternatively, you could use Extensions on IQueryable and IOrderedQueryable. Then you do something like db.Products.FilterBySearchTerm(searchTerm).OrderAllBy(sortPropertyName);
But i like ProductHelper.GetProducts(filterWithSettings,SortOnSettings,GroupBySettings); more, mostly because it's easier when i need to customize all the methods because of some "sudden" Change Request that changes all the internal workings :(
Also, if you are new to Linq To Sql. Don't forget DbFunctions / SqlFunctions for built-in SQL Functionality ( depending the EF version you are using). Eg. The LIKE operator has been added in the new EF.
Also, you can always using DataContext.Database.Sql() for using standard SQL.
=====
Pro Tip: DbContext is already a Repository Pattern if you are using DDD !
=====
PS. If you don't know LINQ. It generates a long SQL query and it's a bit slow, BUT it's very productive in development
. Here's an example with both Generated SQL (commented between /* */ ) and Linq in one of my subbmitted issues on EF : https://github.com/NicoJuicy/EF-ContainsAndStartsWith/blob/master/EF-ContainsAndStartsWith.Test/UnitTest1.cs https://github.com/NicoJuicy/EF-ContainsAndStartsWith/blob/m...
I like LINQ: Develop fast and improve when something executes slow :)