Recently I was working on a project and used these technologies, So I sorted it out a little. I hope it will be helpful for my cainiao-level tasks.
- L3 Architecture
The. NET hierarchical structure recommended by Microsoft is generally divided into three layers ,:
|
Business logic layer (BBL) |
(1) data access layer: it is also called the persistent layer. Its function is mainly to access the database. Simply put
Is to implement the insert (ADD), delete (delete), update (change), select (query) operations on the data table.
(2) Business logic layer: it is the core of the entire system and is related to the business (domain) of the system. Taking LongYuan online mall City as an example, the design of the business logic layer is related to the unique logic of the mall information management system.
The data access layer is called.
(3) Presentation Layer: it is the UI part of the system and is responsible for the interaction between users and the entire system. In this layer, the ideal
The status should not include the business logic of the system. The logic code in the presentation layer is only related to the interface elements.
Advantages of layered structure:
(1): developers can only pay attention to one of the layers in the entire structure.
(2): it is easy to replace the original implementation with the new implementation.
(3): reduces the dependency between layers.
(4): it is conducive to standardization.
(5): facilitates the reuse of various logics.
In summary, the hierarchical design can achieve the following objectives: decentralized attention, loose coupling, logic reuse, and standard definition.
A good layered structure can make the Division of Labor clearer for developers. Once the interfaces at different levels are defined
Developers responsible for different logic designs can focus on and go hand in hand. For example, you only need to consider
Experience and operation, developers in the business field can focus on the design of business logic, so that database designers do not have
It is a headache for tedious user interaction. The task of each developer is confirmed, and the development speed can be quickly increased.
High.
The benefits of loose coupling are obvious. If a system is not layered, the logic is closely related.
They are mutually dependent and cannot be replaced. Once a change occurs, the entire body is triggered.
The objective impact is extremely serious. Reducing the dependency between layers can ensure the future development and reusability.
It also has obvious advantages. Once a unified interface is defined for each function module, it can be called by each module instead
Use the same features for repeated development.
The layered structure also has some defects:
(1) reduces the system performance. If the hierarchical structure is not used, many businesses can directly access the database
The corresponding data must be obtained through the intermediate layer.
(2) cascade modifications may sometimes occur. This kind of modification is especially reflected in the top-down direction. In the presentation layer
To ensure that the design conforms to the hierarchical structure, you may need to add a function at the corresponding business logic layer and Data
Add the corresponding code in the access.
- Entity class
Only the fields and attributes of the class are defined in the object class. Why not define the method in it? If the method is defined, yes
Define the business logic layer method or the data access layer method? Or do they both need to be defined? If their methods are in the real
How do I implement the layer defined in the object class? In addition, you do not want to call the objects in the data access layer at the business logic layer. Actually
You only need to define the interface. The method is defined in the interface, so you don't have to worry about damaging the layered architecture. You can also
When accessing the next layer, you do not need to define the object of this class. You only need an interface.
This is the entity class. in an abstract sense, it is only responsible for Object Representation and data transmission. It is not required by other functions.
- Tool
The tool class is only an encapsulation of the tools used in the system. In fact, it has little to do with the layered architecture,
Three tool classes are used in LongYuan mall. One is cache, the other is encryption, and the other is common operations.
Caching can improve system performance. ASP. NET provides three types of caches available for Web applications:
Output cache: It caches the dynamic response generated by the request.
Fragment cache: It caches the parts generated by the request.
Data Cache: It caches arbitrary objects programmatically.
Below are the cache classes used by my mall, which may be faulty. I hope you can point them out:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Web;
Using System. Web. Caching ;//
Namespace FlowerShop. Utility
{
Public sealed class CacheAccess
{
/// <Summary>
/// Save it to the cache
/// </Summary>
/// <Param name = "cacheKey"> the cache key used to reference this item </param>
/// <Param name = "cacheObject"> object to be inserted into the cache </param>
/// <Param name = "dependency"> the file dependency or cache key dependency of the inserted object. When any dependency item is changed, the object is invalid and removed from the cache. If no dependency exists, this parameter contains null. </Param>
Public static void SaveToCache (string cacheKey, object cacheObject, CacheDependency dependency)
{
Cache cache = HttpRuntime. Cache;
Cache. Insert (cacheKey, cacheObject, dependency );
}
/// <Summary>
/// Retrieve from Cache
/// </Summary>
/// <Param name = "cacheKey"> key </param>
/// <Returns> value </returns>
Public static object GetFromCache (string cacheKey)
{
Cache cache = HttpRuntime. Cache;
Return cache [cacheKey];
}
}
}
The advantage of declaring this method as static is that you can directly call this method using the class name without declaring an object.
Method. In this method, the cacheKey parameter indicates the cache key used to reference this item, and the cacheObject parameter indicates the cache key to be inserted.
Object in memory. dependency indicates the file dependency or cache key dependency of the inserted object. When any dependency
The object is invalid and removed from the cache. If no dependency exists, this parameter is null.
Cache cache = HttpRuntime. Cache; indicates to obtain
System. Web. Caching. Cache;
Cache. Insert (cacheKey, cacheObject, dependency); inserts the cacheObject
To the cacheKey.
This class extracts objects from the cache. First, get a cache class object, and then pass in the key value,
Returns the corresponding object.
- Factory Model
The factory is the core of the layered architecture. The understanding of the factory is very helpful for understanding the three-tier architecture.
Learn more about the factory. You can refer to books on design patterns.
A layered architecture is designed to replace layers. If you want to access data in another way
The question layer, but does not want to modify any code in the presentation layer and business logic layer. Instead, you can modify the configuration file at most.
. How can this be achieved? In addition, the system can carry out parallel development, that is, the people who want the presentation layer and the people at the business logic layer
Developers at the same time with those at the data access layer can greatly improve the development efficiency. The previously implemented interface is to make the upper class
You cannot directly rely on lower layers, that is, upper classes cannot be directly instantiated. What if the lower-level classes are implemented?
? For example, A Class A is defined in the business logic layer, and A class B is defined in the data access layer.
A B object is defined to complete an operation. However, if you suddenly want to implement the data access layer in another way, you do not need
What should I do if I want to modify the code at the business logic layer? The answer is yes, of course, because a B object is defined in A, which is
Is the reason for defining the interface. If an interface is defined, the upper-layer class does not depend on the lower-layer class, but only on the lower-Layer
Is called loose coupling.
Factory class and dependency injection are required in the factory class. The concept of a factory is a knowledge of design patterns. Factory customers
The namespace refers to a specially defined class, which is used to create instances of other classes. This class is passed
The input parameter dynamically determines the product category to be created. Dependency injection, also known as IOC (Inversion of Control ),
The Chinese character is the control inversion. Many applications use two or more classes to implement business logic through mutual cooperation, which makes
Each object must be referenced by the object it cooperates with. If this acquisition process depends on its own implementation, it will lead
Code is highly coupled and difficult to test. Therefore, after application dependency injection, if object A needs to call another object B
When B is created, it is called by dependency injection to pass the reference of object B to A, that is, B's interface is returned to A,
You can perform operations directly through interface B.
In. NET, how does one dynamically decide which product category to create based on input parameters? If
But the reflection mechanism of the. NET platform provides a solution. How to use factory classes and dependencies
What about the layered architecture of the data integration system? First, consider the requirements of layering. layering is implemented without being changed at other layers.
Now we can change the implementation of different layers. This is not to say that if the parameter passed into the factory class is an implementation of a certain level,
The implementation of this layer can be dynamically called through reflection. If different implementations are dynamically loaded
Can the program still run stably after dynamic loading? Or how to make the program run stably.
This still requires interfaces. The interfaces at the business logic layer and data access layer are defined before.
The number of implementations is only a method of rewriting. Therefore, interfaces can be used to achieve seamless integration. Dependency injection is only one type
The purpose of this mechanism is to reduce coupling. In general, it is to avoid using new to implement classes, but through interfaces.
- Simple differences between Anemia and congestion
Anemia Model: only get and set methods in a domain object, or a small number of CRUD methods are included. All Business Logic is not included, but placed on the Business Logic layer.
The advantage is that the system has a clear hierarchy and one-way dependency between layers. Client> (Business Facade)> Business Logic> Data Access (ADO. NET ). Of course, Business Logic depends on Domain objects. It seems that the current popular architecture is like this. Of course, the layers can be subdivided.
The disadvantage of this model is that it is not object-oriented. domain objects are used only for storage or transmission. Therefore, only objects with no data behavior are not real objects. Processing all Business Logic in Business Logic is called the Transaction Script Mode in POEAA (enterprise application architecture mode.
Congestion model: the hierarchy is similar to the above, but most of the Business Logic and persistence are placed in the Domain Object. Business Logic simply encapsulates some Business Logic and controls transactions and permissions, in this way, the hierarchy becomes Client> (Business Facade)> Business Logic> Domain Object> Data Access.
It has the advantage of object-oriented, and Business Logic conforms to a single responsibility, not as heavy as containing all Business Logic in the anemia model.
The disadvantage is that how to divide Business Logic, what Logic should be put in Domain Object, and what Business Logic should be put in Business Logic is vague. Even if the Business Logic is divided, it cannot be better developed by modules because it is scattered in the Business Logic and Domain Object layers. Developers familiar with business Logic need to penetrate into Domain Logic, while Domian Logic contains persistence, which is very confusing for developers. Second, because Business Logic needs to control transactions and provide a unified service call entry point for the upper layer, it must repackage all the Business Logic implemented in Domain Logic, it is completely repetitive.
If technology supports the congestion model, it is of course the perfect solution. But now. the NET Framework does not have ORM tools (not open-source NHibernate, Castle, and so on). Without ORM, there will be no transparent persistence support. In the Domain Object layer, the Data Access layer will be dependent, if the business logic of Domain Object is separated from the Data Access layer, unit tests cannot be performed. This is also fatal. If there is support for Dynamic Injection of Spring and transparent persistence of Hibernate, the congestion model can still be implemented.
Selected. NET platform development is to select a highly efficient and powerful application, the most suitable model is the anemia model, or table data entry, both compiler and language platform support, it is also in line with the development model promoted by Microsoft. If the congestion model is used in projects with the trend, the existing ORM tools are complicated and difficult to use. Maintaining a large number of ing files alone becomes a problem. It is said that anemia is not OO, its Domain Object is not rich enough, it can do a good job of the project, there is a certain degree of scalability and scaling line on the line, it does not have to pay attention to elegant object-oriented. Just like SOA, it focuses on loose coupling, high cohesion, and the services provided by the Service end to the client, that is, a series of method calls plus DTO, no matter whether the internal implementation of the service is object-oriented or not, at least it exposes a procedural service.