. Net distributed architecture development practice III. In-depth thoughts on data access

Source: Internet
Author: User
Preface:

First, thank you for choosingArticleThank you for your support. I hope this series of articles will help you a little. Thank you again.

You may want to ask whenCodeThe code will surely come out. I don't want to throw a lot of code as soon as I come up, and then I will explain that the design of the architecture is in the process of thinking. When I think about it, the code will come to the fore.

The previous article describes some of the sketch drawn by Richard at the beginning of the design. This article further considers the previous sketch.

The topics in this article are as follows:

1. Where are some sketch problems?

2. Review the data layer issues in the previous project

3. A breakthrough in thinking

4. Let's look back at the data access layer.

1. Where are some issues with the sketch?

When Richard drew a sketch, he thought of another problem: the interface layer provided between the Dal data layer should not be a service interface. In fact, this interface layer is a common interface layer or service interface, and Richard may not be able to decide, because the original reason why this interface layer should be changed to service interface is because in the data source provider (service agent) that inspired him to use remote services for data sources. Based on this idea, Richard also considered:

Maybe, which day the designed Dal will serve other users?ProgramThe provision of data is not fixed.

Although this question is not so important for the moment, in Richard's mind, he cannot convince himself which interface layer to use (maybe it is related to the character of Richard, you must give yourself a reason to persuade yourself, but you cannot confuse yourself with this reason ).

Richard thought of the fact that the services provided by other companies were used as data sources during the development project. Although the call was just a simple operation of querying, adding, deleting, and modifying, many procedures have been defined by the service provider. For example, when sending a batch of goods, richard only calls a createproduct (product) method of the service interface,ServerThat end has done a lot of things: Calculate inventory, generate orders, and select goods suppliers. In this case, if Richard adds a service interface layer to the Dal, the Dal or other layers must provide many logical operations, or not necessarily logical operations, it can also be data format authentication and identity authentication.

If this is the case, there will be a lot of data layer operations and a lot of logic. These logics are a good choice in BLL. At this point, Richard seems to understand that the services interface layer is placed on the BLL layer. In this way, the logic verification function of BLL can be fully utilized. Therefore, the interface layer above the Dal still decides to use a common interface.

2. Review the data layer issues in the previous project

There is a reason why Richard spent a lot of time thinking about the data-layer Dal. In the previous project, the design of the data layer was very bloated, and in Richard's view, the code can already be written in a common way, there is no need to write such complex code.

For example, employeedal has the following methods:

Code

   code highlighting produced by actipro codehighlighter (freeware) 
http://www.CodeHighlighter.com/
--> Public employee getemployeebyid ( string employeeid);
Public emplpyee getemployeebyname ( string employename);
Public string getemployeepositionbyid ( string employeeid );
Public int getemployeecount ();

There is no error in this writing. But one thing caused Richard's thinking: In the Dal class, many methods are basically query methods, while add, update, and delete methods are comparatively unified in many Dal classes, especially after using the LINQ and Entity Framework, the ADD, update, and delete methods of all data entity classes are almost the same. For example, you can use datacontext in the linq. gettablet (). insertonsubmit (entity) method to insert any data entity class.

However, it is difficult to unify the query methods with these different conditions. Almost every different Dal needs to implement its own specific query methods. However, Richard thinks that it is possible to unify these methods, or even achieve the same effect. He started many attempts with this idea.

3. A breakthrough in thinking

Richard tries his best to search for solutions in his mind. He also looks at the books he has downloaded and bought before to see if they are related to data query, richard won't let it go. Also refer to the design idea of a well-known open-source framework of. net.

The book "Fowler enterprise application architecture model" Reminds him that the query object mentioned in the book is indeed good, but it is too abstract, no examples are provided.

As a matter of fact, when Richard was reading this book, he thought that the book was too high. Richard was hit by a number of times. So Richard feared the book, but he still wanted to win it one day. Now there are some solutions and implementations for querying objects in the book, and Richard is hard on it.

Richard believes that many things are not done only when all your conditions are met. Things often happen when you are not ready, there is a beginning for everything, and the architecture is also like this. When you design an architecture, it doesn't mean that you have fullyTechnologyIt's a perfect process. It may be a lot of loopholes at the beginning of the design, but the closeness of thinking is also slowly developed.

When thinking about it, you can look at the problem from a higher perspective (currently a developer, but the developer can think like this during design and development: what can I do if I am the architect ?), With a high degree of thinking, the opportunity will be easy to come.

Richard began to understand the query object again. In fact, the query object is hidden to a certain extent.SQLThe query object is an application of the interpreter mode. In fact, the query object must be interpreted as an SQL statementDatabase(No matter how the query object is operated step by step in the intermediate process, because the database only knows SQL, so the query object still needs to generate SQL statements ).

The main purpose of a query object is to allow the customer program to construct various queries, and the query uses all the attributes related to the business class. This means that the customer program does not need to understand the table name and column name of the database. The most direct advantage of this approach is that business developers do not need to know the database structure.

In addition, if the query object is designed as an interface, the flexibility is even greater. For example, design an IQUERY query object interface, and then there are two query objects in the architecture that implement the IQUERY interface, such as linqquery, efquery, these two specific query objects are finally interpreted as statements that can be recognized by the LINQ and entity frameworks respectively, and then perform data operations through the LINQ and entity frameworks. In a program, you can configure the query object and the data access technology.

More and more ideas appear in Richard's mind, and the problems that arise are also accumulating. The basic idea is yes, but it is another thing to implement it. How to clarify these thoughts and how to turn these clear thoughts into code implementation has become the most practical challenge facing Richard.

But in any case, there has been a little progress. I remember that my mind was blank when I first thought about it. Now I think one after another, and I have improved my thinking. Richard felt a little pleased.

4. Check the data access layer again.

With the above thinking, Richard once again examined the dal and began to find that the dal is actually just a place to access and operate data, which is the main function of the Dal. Now, the design of the data layer can be basically implemented, and the data can be retained without changing. No matter what operations are performed on the data in Bll, the four types of incremental data can be used in the Dal layer, delete, query, and modify the method.

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.