Database master-Slave read/write separation architecture based on entityframework (1)-Principle Overview

Source: Internet
Author: User

In this previous Article blog post (based on the EntityFramework database master-Slave read-write separation service plug-in, http://www.cnblogs.com/cjw0511/p/4391092.html), outlining the introduction of their own based on EF6 Write a plugin for the database master-slave read-write separation service. Because of the temporal relationship, the previous blog post only describes the plug-in's Functional overview and basic usage. Today is free, take a moment to conceive of this blog post, and everyone together to exchange, I based on the EF6 database master and slave read and write separation service plug-in is how to conceive, design and code implementation.

    First of all, in reading and writing to carry out their own follow-up content, should have some knowledge, including but not limited to the following aspects:    1, ADO basic knowledge;    2, EntityFramework functions and basic usage;    3, database read and write separation concept (http://baike.baidu.com/view/3372624.htm);    4, Basic configuration of database master-slave copy function (different database system configuration method);    above is to read and implement the basic knowledge of the database master-slave reading and writing, this article does not do detailed, if you want to know in these aspects of the classmate, please make your own lessons.      OK, let's get down to the chase.     We all know that in the application development, to implement the master-slave read and write separation operation, that is, all the data change operation requests point to the Master (Master) server, and all the data query requests point to the Slave (slave) server, it is simply to build ADO When you connect to a. NET DbConnection, the ConnectionString point is different. Previously, when the ORM framework was not used, we could define two types of APIs in the tool type, one for database change operations and the other for data query operations, by customizing some tool classes such as Dataaccesshelper. In the database change operation, the connection address of the generated DbConnection points to the Master server, and the generated DbConnection points to another Slave server when the data query operation occurs.     Today, we use similar ORM frameworks such as EntityFramework, NHibernate, and so on, because of the need to isolate concerns, improve code reusability and coding efficiency, and reduce programmer knowledge dependency on SQL. In fact, in the ORM framework, to achieve the database read and write separation, and our early use of ADO directly to implement this function is the same principle. The main difference, however, is that the ORM framework generally provides an aspect injection of the AOP architecture, allowing us to add our own custom actions to a particular point of interest.     in version 6.1 of EntityFramework, a new namespace has been added System.Data.Entity.InfrasTructure. Interception, the namespace essentially provides a tangent-oriented program injection function that allows us to intercept the action and perform other additional actions we have customized before and after EF finally submits the DbCommand execution request to the database. The basic approach is:    1, customizing a subclass of the  system.data.entity.infrastructure.interception.dbcommandinterceptor type;     2, overriding methods in this custom type readerexecuting, readerexecuted, scalarexecuting, scalarexecuted, nonqueryexecuting, Nonqueryexecuted, the method body can implement its own query command, change the command before and after the execution of additional actions;    3, through   System.Data.Entity.Infrastructure.Interception.DbInterception.Add method to add the custom type instance to the EF execution context;    After completing these steps, EF will be able to add additional actions that we define before and after each execution of the add or delete or query command.      Nonsense, put a piece of code below that represents the basic implementation of the  DbCommandInterceptor  implementation of EF read-write separations:
1  Public classDbmasterslavecommandinterceptor:dbcommandinterceptor2     {3         Private stringMasterconnectionstring ="Server=192.168.0.99;port=3306;user Id=root;password=123456;persistsecurityinfo=true;database=testdb";4         Private stringSlaveconnectionstring ="Server=192.168.0.101;port=3306;user Id=root;password=123456;persistsecurityinfo=true;database=testdb";5          Public Override voidreaderexecuting (DbCommand command, dbcommandinterceptioncontext<dbdatareader>interceptioncontext)6         {7              This. Updateconnectionstring (Interceptioncontext, This. slaveconnectionstring);8         }9          Public Override voidscalarexecuting (DbCommand command, dbcommandinterceptioncontext<Object>interceptioncontext)Ten         { One              This. Updateconnectionstring (Interceptioncontext, This. slaveconnectionstring); A         } -          Public Override voidnonqueryexecuting (DbCommand command, dbcommandinterceptioncontext<int>interceptioncontext) -         { the              This. Updateconnectionstring (Interceptioncontext, This. masterconnectionstring); -         } -         Private voidUpdateconnectionstring (Dbinterceptioncontext Interceptioncontext,stringconnectionString) -         { +             foreach(varContextinchinterceptioncontext.dbcontexts) -             { +                  This. Updateconnectionstring (context. Database.connection, connectionString); A             } at         } -         Private voidUpdateconnectionstring (DbConnection Conn,stringconnectionString) -         { -ConnectionState State =Conn. State; -             if(state = =ConnectionState.Open) - Conn. Close (); inConn. ConnectionString =connectionString; -             if(state = =ConnectionState.Open) to Conn. Open (); +         } -}

Next, the entity of that type is injected into the EF global execution context in the startup code of Global.asax.

1  Public class myhttpapplication:httpapplication 2     {3         protectedvoid  Application_Start ()4         { 5             Dbinterception.add (new  dbmasterslavecommandinterceptor ()); 6         }7     }

How about, the principle is not very simple? Of course, if you want to implement a number of rich configuration and extension features, there will be a lot of other code, about these I will be in the follow-up article gradually introduced!

Database master-Slave read/write separation architecture based on entityframework (1)-Principle Overview

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.