Web Project Evolution Series--open distributed (separate data layer)

Source: Internet
Author: User

Objective

Originally the previous article is intended to write a separate data layer, but in the process of thinking about the separation of data layer, there are some operations to rely on distributed locks, so first write a distributed lock.

For some projects the data layer provides the business interface (return the data required by the business), then when the pressure of the data layer increases gradually, when the need to use the cache, the developers need to modify the corresponding data interface to use the cache, cache and various data query interface interleaved together, The entire data layer code becomes very confusing, even refactoring can not be done, can only be torn down redo. So many articles, when explaining the data layer, are using a unified data interface, such as: Find, ADD, save, etc., then when the need to cache, you can directly expand in Find to support the cache, or even introduce cache configuration management, The scheduling of cache cycles between different tables allows developers to not need to know the existence of the cache, they still use the original find, but faster, or hot and cold data management, search engine, and so on, this is the great God after years of development summed up the experience.

Separating the data layer from the project into a separate project and publishing it to a standalone server has several advantages over a stand-alone system:

1. Easy to integrate all the resources

2. Cost reduction

3, each layer of better performance and scalability, under the increasing load, you can conveniently increase the number of nodes.

4, some sub-service layer error, will not cause the overall paralysis of the project.

5, different levels can be implemented in different languages

There are advantages and disadvantages, the biggest drawback is that the system is more complex than the single-machine system, not only the structure will become complex, the network between different components will introduce the impact factors, debugging difficulties and so on.

Although there are many shortcomings, but not to practice is not to find another world, do not go to the challenge to see forever is confined to a single system, also do not have to worry about what problems, after all, only find the problem, can find a way to solve, which is from the book can not learn, then we start today's article.

Base Crud

Data layer is to provide the database to operate the middleware, the most basic function is crud, stand-alone system, call Crud interface, either by splicing SQL or directly connected to the database through ORM, and because now is distributed, so the original call method needs to be implemented through the communication Protocol, Suppose the original interface was:

public class dbresult{Public    bool Error {get; set;}    Public object Data {get; set;}} public interface idb{    Dbresult Find (dictionary<string, object> query);    Dbresult Add (object entity);    Dbresult Save (object entity);    Dbresult Remove (object entity);

The find on the end of the data layer is implemented in Query object mode, which can be viewed in this article-"Query object--querying object mode (top)", "Query object--querying object mode (bottom)").

Here the data layer project, if using the original extracted Mvchandler way to implement, then the IService derived class will need to use HttpWebRequest to implement, the approximate implementation of the idea is to convert the corresponding method to call the data service, the find implementation code is as follows:

Public Dbresult Find (dictionary<string, object> query) {var url = string.    Format ("{0}/{1}/{2}", Connectionuri, This.table, "find");    var req = (HttpWebRequest) httpwebrequest.create (URL); Req.    Method = "Post"; Req. Accept = "Text/plain, */*;    q=0.01 "; Req. ContentType = "application/x-www-form-urlencoded;    Charset=utf-8 "; Req.    Timeout = 1000 * 30; Req.    KeepAlive = true;        try {byte[] bytes = Encoding.UTF8.GetBytes (jsonconvert.serializeobject (query)); Req. contentlength = bytes.        Length; using (var Reqstream = req. GetRequestStream ()) {Stream Requeststream = req.            GetRequestStream (); requestStream.Write (bytes, 0, bytes.        Length);        } var res = new Dbresult (); using (var resp = (HttpWebResponse) req. GetResponse ()) {using (var Respstream = resp.                GetResponseStream ()) {using (var reader = new StreamReader (Respstream, Encoding.UTF8))    {                String respcontent = reader.                    ReadToEnd ();                Return Jsonconvert.deserializeobject (respcontent);    }}}} catch {return new Dbresult {Error = true}; }}

Transaction

With the base crud, the next thing to do is to implement the transaction, if you refer to a stand-alone system to implement the transaction, then after the transaction, if the business layer problems caused the host to restart or outage, then the data layer of the transaction will be unable to shut down, causing problems.

Therefore, it needs to be implemented in other ways, observing that transactions can be derived from the transaction opening to the commit is a whole, only when the transaction commits the State to determine the execution of the transaction, while the transaction is open and can not need to have the result of the decision, so the transaction can be considered a queue, Each of the CUD operations in the middle can be thought of as its elements, and each operation needs to know which table, what action to perform, and the corresponding table data, and the code is implemented as follows:

public class transactionaction{Public    string Table {get; set;}    public string Name {get; set;}    Public object Data {get; set;}} Idbpublic interface idb{    //other omitted    void Begintx ();    Dbresult Committx ();} IDB implements private list Actions;public void Begintx () {    this.actions = new List ();} Public Dbresult Committx () {    //http accesses the data layer and sets the actions to Null}public Dbresult Add (object entity) {    if (this.actions!). = null)    {        this.actions.Add (new transactionaction        {            Name = "Add",            Table = this.table,            Data = entity        });    else    {        //single increment    }}

Conclusion

The separation of data layers here is basically done, and if you want more efficient data acquisition, you can modify the access method to a socket, not using WPF or WebService directly because it is based on C #, and if it is used directly from a ready-made framework, There are not many things that can be learned.

Here, if there is any problem or error, please leave me a message, thank you.

Web Project Evolution Series--open distributed (separate data layer)

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.