Is there a code everywhere in your program:
Db. Customers.insertonsubmit (customer);
Db. Submitchange ();
If one day, because the database of the Customers table is huge, you need to split it into two tables, whether you will be frightened??? Of course, for queries, we can solve them through views. For inserts, or updates? It is said that in some cases, the current database can be updated and inserted into the view for data. But we have to consider the situation is not possible. Do you have to change every place? For a large project, this change is terrible, some place less change, it is bad. However, in this case, LINQ to SQL has already figured out a solution for us. Implementing the InsertCustomer (Customer customer) method in the DataContext inheritance class is OK, as shown in the following illustration.
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/
Pseudocode for Implementing a method:
partial void InsertCustomer (Customer instance)
{
//Insert data into table 1
//Insert data into table 2
}
When the InsertCustomer method is implemented, LINQ to SQL then inserts data into the Customers table, which is called to insert the data instead of the default method.
A little attention, we found that there are updatecustomer,deletecustomer these two methods, I guess you can guess their use, yes, is used to replace the default update, delete method.
Let's now consider a question, how did Linq to SQL learn about InsertCustomer, UpdateCustomer, and deletecustomer that have been implemented? by reflection. So now we know that Linq to SQL is a process for inserting, updating, and deleting data:
First, by reflection, to see if there are InsertCustomer, UpdateCustomer, deletecustomer whether these methods exist, if they exist, call these methods, or use the default method to add a deletion. However, there is a problem, if you insert 10 data, you need to call the reflection method 10 times, which is a bit stupid, it is well known that reflection is a performance-consuming, and that this is unnecessary loss of performance. So LINQ to SQL is cached and reused after it has been found by reflection of the several methods. So, where are these methods cached?
In the MetaTable class, we can see the three properties of InsertMethod, UpdateMethod, and DeleteMethod. Yes, additions and deletions of the extension method, is to cache here.
Let's sort this out now:
1. When creating the DataContext, a mapping source (Mappingsource) is created. The mapping source creates a meta table (metatable) for each class to describe the mapping between the table and the entity. In the creation of this meta table, through reflection, check whether there is a corresponding entity class DataContext extension method, if there are, InsertMethod, UpdateMethod, DeleteMethod these three properties are assigned accordingly.
2, when inserting a number of times, first of the entity class corresponding to the Wenzu to find out whether the Insert property of the meta table is null (NULL), if empty, call the default insertion method, which is equivalent to the DataContext class in the Executedynamicinsert Method.
Linq to SQL's process of changing data additions and deletions is just like this.