. NET in-depth parsing of the LINQ framework (III: The Prelude to LINQ Elegance)

Source: Internet
Author: User

Read the catalogue:

    • 1. Dynamic LINQ Query (dynamically build expression<t> expression tree)
    • 2.DLR Dynamic Language runtime (Dynamic Language runtime on top of the CLR)
1 ". Dynamic LINQ Query (dynamically build expression<t> expression tree)

What is a dynamic LINQ query? LINQ is written statically, because C # is designed based on the principle of static type systems, which, when written, determines what kind of query will be executed at compile time, what the condition is, how it is sorted, and so on. So a large part of the application we need to query the data source according to the user's choice, in the past we have been judged by the way to splice the query SQL string, but now we face is a strong type of LINQ query, whether it can be very convenient to do similar queries. In fact, there is no good mystery, the basic implementation of the principle is through the dynamic construction of expression tree to implement the Iqueryable<t> interface query.

In fact, the most critical factor that dynamic LINQ queries can perform is that the Expression<t> object can be dynamically compiled into a delegate object that can be executed, and the delegate object is an executable snippet that can be used entirely directly, which provides the basis for dynamic LINQ queries. For a query expression method of the Ienumerable<t> type, knowing that its execution does not directly accept the Expression<t> type Object, will dynamic LINQ work on the Ienumerable<t> interface? Actually, there's a very subtle trick. Hidden in the Iqueryable<t> extension method object queryable, which is the Asqueryable<t> method, it returns an implementation iqueryable<t> interface of the Enumerablequery object, the implementation of the object is not very complex, the dynamically stitched data structure expression<t> object compiled into an anonymous function can be executed, and then directly execute the query.

Let's take a look at the focus of the Enumerablequery object, it must have a place where the Expression<t> object compiler.

View Code

In the "(1) Focus" area of the code above, we clearly see that the expression tree is dynamically compiled and then executed, and here we can see why the Ienumerable<t> object needs to be able to be converted to a Iqueryable<t> object. This eliminates the dynamic query bottlenecks between the two interfaces, Ienumerable<t>, iqueryable<t>.

Why do we need dynamic LINQ queries that say the problem is that we have no way to write lambda expressions at run time, knowing that the lambda expression is finally compiled into an expression tree object, So we can build the expression object dynamically at runtime, so that we can pass the dynamically constructed expression tree object directly into the desired method. If the data object of the query is ienumerable<t>, it is dynamically compiled into a delegate that can be executed and then executed directly, and if the query is iqueryable<t>, the provider resolves it naturally.

Let's look at a simple dynamic query example:

View Code

This is a set of data that is not so cumbersome for simple testing as a LINQ to SQL data source. We're going to use the dynamic build expression tree as a query logic, and our lambda used to be useless at this point in time, and we can't build the delegate type at runtime.

The requirement now is to accept the input of a name value from the interface, and LINQ queries only need to be written directly.

View Code

But we need a dynamic build expression tree to execute the query, and any node in the expression tree has a corresponding expression derived type, so we just have to assemble the correlation type. Since the type of sample program I built is a console program, let's demonstrate how to build an expression tree in a short way.

View Code

Legend:

The focus of this example is how to build logic dynamically, which can be encapsulated for later reuse according to different project requirements. If you find it cumbersome to write an expression tree manually, it is recommended that you find a tool that can be used to print the object tree of a lambda expression, and then it is much easier to write to the tree.

About dynamic LINQ third-party API is not many, more commonly used is the use of Dynamic.cs, specifically I have not used, read the relevant documents should be more convenient. Its internal principle is actually a dynamic construction of the expression tree, but this part of the work has been done by others, and we use a lot of simple.

2 ". DLR Dynamic Language runtime (Dynamic Language runtime on top of the CLR)

From C#1, it has become more and more powerful. NET platform becomes omnipotent. A lot of people have been biting. NET cannot cross the platform, cannot support dynamic objects, does not support unmanaged, and so on to repel it, but what they do not know is. NET has quietly made a big move to embed the dynamic Language runtime environment on the static language runtime. I don't think Microsoft can support the so-called shortcomings, but it does have its intentions.

The dynamic language runtime is in the. The runtime environment, introduced in NET4.0, is built on the CLR in order to be able to draw on the advantages of dynamic language runtime in static languages, such as powerful types of arbitrary transformations, which is especially important when designing application development frameworks, where any good feature requires a large area of usage patterns to become more perfect.

When it comes to dynamic operation, we have to mention the object characteristics of the exciting var definition in JS, but it's hard to find the good and bad between dynamic running and static language if we don't pay attention to the troubles in the design frame. The obvious example is that when we define an object of a data type, it can no longer be used for other types of use at the later runtime, see a simple example:

View Code

At runtime we can design object types at will, and I boldly assume that you can use dynamic runtime features to design a similar AI system, provide a basic prototype, and then build any tree of objects according to the user's own way of thinking. Technical research is a very good direction, business applications may yet to be discussed.

In the past, it was difficult to dynamically add properties, behaviors, events to objects at runtime, and we could easily add what we wanted through dynamic language runtime.

Let's look at a simple example of dynamically building an object type at runtime, which we've only implemented with dynamic compilation and CodeDom technology, and it's going to be very simple.

View Code

A very simple example that we can write in C #, such as JS in the dynamic object function, but is not very mature, dynamic object Members do not have smart tips, should not have been a large area of use, the future is certainly a big meal;

Summary: The basic use principle of the LINQ framework is all over, and we'll learn how to get LINQ to query our custom data sources later. Many friends like to write their own ORM framework, so you must have support for LINQ? We will explain in detail how to expand the Iqueryable<t>, iqueryableprovider<t> two heavyweight interface, only two of them to let us and LINQ dialogue, these two interfaces is still very mysterious.

. NET in-depth parsing of the LINQ framework (III: The Prelude to LINQ Elegance)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.