Upload
axilis
View
351
Download
0
Embed Size (px)
Citation preview
Road to Dynamic LINQ – Part 2
Vedran Maršić AKA Fosna
Warning: Expression Trees Ahead
About me Vedran Maršić AKA Fosna
dev at http://www.axilis.com/author/vmarsic/
organizing programming workshops for kids Logo, Scratch, HTML, CSS, JavaScript, Python, C++
We’ll cover Part 1
What is expression tree in C# How is it different from similar looking lambda delegates How to build expression trees How to parse expression trees Where are you already using them
Part 2 Check out few expression tree exploits Point to brilliant, not easy to understand, ideas or projects from some cool
developer maniacs
We won’t cover Implementing LINQ providers Implementing deep copy Implementing faster static dictionary Implementing IoC container
Need for Dynamic Queries
Applied Expression TreesC# 6.0 Taste Before C# 6.0
Sort by Custom Property... in LINQ to Entity Framework
Try to support sort by each column in grid on web page Chances are grid will send to the server
a string for property name and a sort order
Dynamic Searching... in LINQ to Entity Framework Try to support search by each column in grid on web page
Chances are grid will send to the server a property name and a value to compare to
Let’s not bother with more complex requirements for brevity of the presentation
Get Property or Method NamesSomething like nameof operators in C# 6.0
Similar thing was possible with older compiler
Hint:
Get Over NULL Reference Exceptions Null-conditional operators in C# 6.0
Empty string if person is null or person.Child is null
Simmilar thing was possible with older compilerstring childName = person.Safely(x => x.Child.Name);
Source Code Available at
https://github.com/axilis/RoadToDynamicLinq
Some Other Ideas LINQ update, insert, delete statement with where clause
without dbContext.SaveChanges() magic Entity Framework Extended Library by Paul Welter of LoreSoft https://github.com/loresoft/EntityFramework.Extended
TypeSafe property changed http://
stackoverflow.com/questions/3551947/in-c-why-expression-trees-and-when-do-you-need-to-use-them/3551991#3551991
Lambda equality comparer with delegates
Stay Away from Other Ideas Deep copy Customizable convention based copy, like AutoMapper Fast IoC Container
BarbarianIOC : A simple IOC Container by Sacha Barber, 25 Feb 2013 http://www.codeproject.com/Articles/552514/BarbarianIOC-A-simple-IOC-Container
Faster static dictionary StaticStringDictionary - Fast Switching With LINQ Revisited, AUG 28TH, 2008, by Gustavo Guerra http://functionalflow.co.uk/blog/2008/08/28/staticstringdictionary-fast-switching-with-linq-revisited/
Your own LINQ provider Using Expression Trees in Your APIs by Patrick Steele04/01/2011 https://visualstudiomagazine.com/Articles/2011/04/01/pccsp_Expression-Trees.aspx
Thanks. Bye!No questions, right?
Resources Start with
http://stackoverflow.com/a/403233/953338 Expression Tree Basics, Charlie Calvert 31 Jan 2008 3:49 AM
http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx
Published by Olexander Ivanitskyi on Tuesday, June 11, 2013 LINQ: Func<T> vs. Expression<Func<T>> http://ivanitskyi.blogspot.hr/2013/06/linq-func-vs-expression.html
asked Apr 27 '09 at 13:50, Richard Nagle Why would you use Expression<Func<T>> rather than Func<T>? http://
stackoverflow.com/questions/793571/why-would-you-use-expressionfunct-rather-than-funct
Resources Dynamic LINQ examples
https://www.nuget.org/packages/System.Linq.Dynamic/ C# 6.0 Features
https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6 Canonical Functions
https://msdn.microsoft.com/en-us/library/vstudio/bb738626(v=vs.100).aspx
The .NET Programmer’s Playground - LinqPad http://www.linqpad.net/