ASP. NET design mode Reading Note 1

Source: Internet
Author: User
Tags net domain
Document directory
  • Necessity for. NET developers to learn
  • Design principles
  • S. O. L. I. D. Design Principles

Statement

This article is a summary of reading this book. The content comes from the network and is manually entered by the user. Published on a blog does not serve as any commercial application. There is a large text reference. If there is a copyright problem, please contact me and I will delete it.

 

Professional ASP. NET Design Patterns

Why learning design patterns?

Applies to the design patterns, principles, and best practices in ASP. NET applications. The design patterns and principles support loosely coupled and highly cohesive code, which improves code readability, flexibility, and maintainability.
There is no reason to repeat tasks that have a good solution.

Famous architect Christopher Alexander once said:
Each mode describes a recurring problem around us and the core of the solution, so that you can use the solution again and again without repetitive work.

John, "No problem, only the way out"

Necessity for. NET developers to learn

Microsoft's rad (Rapid Application Development) development tool Visual Studio. Net quickly develops form-based Web applications. By simple drag and WYSIWYG application design interfaces, people can get started quickly. Consistent programming models are also conducive to the transfer of desktop application developers to Web application development. In addition, since the encoding mode and design mode can be easily switched, the graphic designer can see the interface close to the runtime in the design phase, without the need to frequently run the debugging mode or refresh the webpage, this enables the graphic designer to participate in application development throughout the process, thus improving the development efficiency. However, this solidified form-based application design model also has inherent limitations. As the business needs change and the scale grows, if you still put all the business logic in the back-end code, the code will become increasingly bloated and there will be a lot of repeated code. At the same time, this web form design is not conducive to the use of Ajax technology in applications, it is difficult to share code between web forms and web service programs. Understanding the design concepts and implementation tools mentioned in the book is helpful for a better understanding of the concepts in the ASP. net mvc framework.

Patterns and design principles

The design pattern is a high-level and abstract solution template. These models can be regarded as the blueprint of the solution rather than the solution itself. We cannot find a framework that can be simply applied to applications. On the contrary, we usually refactor our code and generalize the problem to implement the design pattern.

"Four-person group" (gof ). They included 23 design patterns and divided them into three groups.
● Creational: Processes Object Construction and references.
● Structual: processes the relationship between objects and how they interact to form more complex objects.
● Behavior mode (behavioural): Processes communication between objects, especially in terms of responsibility and algorithm.

Mode is an effective way to describe complex problems. If you have a strong knowledge of the design model, you can quickly and smoothly communicate with other members of the team without having to worry about the implementation details at the underlying layer. The mode is language unknown. The knowledge gained through the learning model can be applied to any excellent object-oriented language used in specific programming.

The purpose of the design model is to reuse the solution. Of course, not all problems are the same, but if we can break down a problem and find its similarities with the previously solved problems, we can use these solutions. Even if you think that your problem is unique, you can break it into several basic elements and generalize them to a certain extent to find a suitable solution.

The design pattern name is very useful because it reflects the pattern's behavior and purpose and provides a frequently used vocabulary for people to brainstorm and discuss solutions.

Trap: try to apply the design pattern to everything you do, but the final result is opposite to the original intention of the design pattern (that is, to make things simple. A good way to use the pattern is to find a suitable solution by identifying the basic problems that are being solved.

 

Design principles

The design principles constitute the foundation of the design model. By following the proven design principles, your code pipeline becomes more flexible, adaptable to changes, and has better maintainability.

1. Simple Principle (kiss)
A common problem in the field of software programming is that the solution must be overly complicated. The goal of the kiss principle is to keep the code concise but not too simple, so as to avoid any unnecessary complexity.

2. Do not repeat yourself (dry)
The dry principle aims to avoid repeating any part of the system by extracting the public part and placing it in a separate place. This principle involves not only code but also any logic repeated in the system. In the end, any part of the system's knowledge should have only one representation.

3. Do not ask (tell, don't ask)
The principle of "tell but not ask" embodies the idea of encapsulating and assigning responsibility to the correct class. This principle requires that you tell the object what actions you want them to perform, instead of asking questions about the object status, and then you decide what actions you want to perform. This principle helps to match responsibilities and avoid close coupling between classes.

4. You don't need it (yagni)
The yagni principle means that you only need to include the functions required by the application, rather than trying to add any other functions that you think may be needed. Test-driven development (TDD) is a design methodology that adheres to the yagni principle. The purpose of TDD is to write a test to verify the functions of the system. Then you only need to write the code that can pass the test.

5. Separation of concerns (SOC)
The SOC process breaks down software into multiple different features, each of which encapsulates unique behaviors and data that can be used by other classes. Generally, a focus is a function or action of a class. The division of programs into several independent roles significantly improves code reuse, maintainability, and testability.

S. O. L. I. D. Design Principles

S. O. L. I. D. Design Principles are a set of best practices for object-oriented design. Term S. o. l. i. d. from Robert C. the first letter of the names of the five design principles collected in Agile principles, patterns, and practices in C # by Martin (Uncle Bob.

1. Single Responsibility Principle (SRP)
The SRP principle is highly consistent with the SOC principle. It requires that each object be changed for only one element and has only one responsibility focus. By following this principle, we can avoid the design problems of a single category (like the Swiss army knife in the software field. By keeping each class concise, you can improve the readability and maintainability of the system.

2. Open and closed principle (OCP)
The OCP principle requires that the class should be open for extension, and the modification should be closed, so that new functions can be added and the class can be extended without changing the internal behavior of the class. This principle tries to avoid damaging existing classes and other classes dependent on it, because it will cause bugs and errors in the application.

3. Lean replacement principle (LSP)
The LSP principle indicates that any inheritance class should be used to replace the parent class and keep its behavior unchanged. This principle is consistent with the OCP principle: it ensures that the inherited class does not affect the behavior of the parent class. In other words, the inherited class must replace their base class.

4. Interface separation principle (ISP)
The ISP principle focuses on dividing the contract method into several responsibility groups and assigning different interfaces to these groups, in this way, the client does not need to implement a large interface and a bunch of methods they are not used. The purpose behind this principle is: to use a class with the same interface, you only need to implement a specific set of methods, rather than implementing a large single method interface.

5. Dependency inversion principle (DIP)
The objective of the dip principle is to isolate the classes you write from the specific implementations so that these classes depend on abstract classes or interfaces. It advocates interface-oriented (rather than implementation) programming, which ensures that the Code is not closely coupled with a certain implementation, thus improving the flexibility of the system.

6. Dependency injection (DI) and control reversal (IOC) Principles
The DI and IOC principles are closely related to dip. Di provides underlying classes or subordinate classes by constructors, methods, or attributes. With the di principle, these subordinate classes can be reversed to interfaces or abstract classes, thus forming a loose coupling system with high testability and ease of modification.

In the IOC principle, the control flow of the system is reversed compared with the procedural programming method. An example of this principle is the IOC container, which is used to inject services into the client code without having to let the client code specify the specific implementation. In this instance, the inversion of control refers to the client's behavior of obtaining services.

Fowler's enterprise design model

Martin Fowler's book patterns of enterprise application architecture is a reference book on best practices and patterns for building enterprise-level applications.

Domain logic model

Three common methods for organizing business logic: Transaction script, active record, and domain model ).

1. Transaction script
The transaction Script Mode organizes the business logic according to the linear and procedural methods. It maps fine-grained business use cases to fine-grained methods.

2. Active record
The active record mode organizes the business logic in a way that closely matches the underlying data structure, that is, the object that represents the data row in the table.

3. Domain Model
The domain model mode is an abstraction of objects in the real world. Data and behavior modeling at the same time. Objects can have a complex relationship that matches the real domain.

Object link ing

Enterprise models required to support persistent infrastructure code.

1. unit of work
The unit of work mode is used to maintain a list of business objects that have been modified (added, deleted, or updated) by business transactions. Then, the unit of work mode coordinates the persistence work of these changed objects into an atomic action. If a problem occurs, the entire transaction will be rolled back.

2. Repository
The Repository (resource library) mode is generally used for the logical set of objects. More people know that they are called aggregate (aggregate ). It acts as a memory set or warehouse of business entities, and completely abstracts the underlying data infrastructure.

3. Data mapper
The data Er (Data ER) mode is used to extract information from raw data to generate objects, and convert information from business objects to databases. Business Objects and databases do not know each other.

4. Identity Map
The identity map mode monitors every object loaded from the database and ensures that all objects are loaded only once. When this object is requested later, the flag ing is checked before retrieval from the database.

5. Lazy Loading
The lazy loading mode delays the process of obtaining resources until the resource is actually needed.

6. query object
The query object mode is an implementation of the gof interpreter design mode. The query object acts as an Object-Oriented Query abstracted from the underlying database. It references attributes and classes rather than real tables and columns. Generally, you also need to use a translator object to generate native SQL statements used to query databases.

Web representation Mode
Separating domain and representation logic allows the presentation layer to be effectively tested. Tasks in these modes are to separate the logical focus expressed from the business logic focus. ASP. NET indicates the modes to be covered:

● Model-View-Presenter (Model-View-Express ).
● Model-View-controller (Model-View-Controller ).
● Front Controller (front-end Controller ).
● Page controller (page Controller ).

Other enterprise models in Fowler's books
1. Null Object Mode
The Null Object Mode is also called the special case mode. It acts as the return value rather than returning null to the calling code. Empty objects share the same interface as expected results or inherit from the same base class, which reduces the need to check the null condition everywhere in the code base.

2. Separated interface mode
The separated interface (independent interface) mode requires that the interface be placed in an assembly or namespace independent of the specific implementation. This ensures that the client is completely unaware of the specific implementation, and can follow the principle of object-oriented abstract programming (rather than implementation) and dependency inversion.

3. Gateway Mode
The gateway mode allows the client to access complex resources through a simplified interface. The gateway object basically wraps the resource API into a single method call that can be used everywhere in the application. In addition, it hides all API complexity.

TDD (test-driven development, test-driven design) is more of a design methodology than a test strategy, as it is not mentioned in its name. The main idea behind this design methodology is to use testing to shape the design of the system. When creating a software solution, first write a test program that causes the test to fail to assert a certain business logic. Then write the code to pass the test. Finally, all code is cleared through refactoring. These three steps are known as red-green-refactor ). Red and green refer to the color of the test framework used to show test pass and test failure respectively.

Through the TDD process, a loosely coupled system with a set of tests that can confirm all behaviors will be obtained. One of the by-products of TDD is that these tests provide a document describing what the system can do and what it cannot do. Because testing is part of the system, it will never be out of date, which is different from writing documents and code comments.

● Test driven development: by example, by Kent Beck
● The Art of unit testing: with examples in. net, Roy osherove
● Professional enterprise. net, Jon arking and Scott Millett (wrox publishing)

Domain-driven design (DDD) is a set of patterns and principles that help you build applications that reflect your understanding of your business and meet your business needs. In addition, it is a brand new way of thinking about development methodology. The modeling method of DDD is as follows: Firstly, the real domain is modeled by fully understanding the real domain, then all the terms, rules, and logic are put into an abstract representation of the Code (usually in the form of a domain model ). Although DDD is not a framework, it does have a set of building blocks or concepts that can be integrated into the solution.

● Domain-driven design: Tackling complexity in the heart of software, Eric Evans
● Applying domain-driven design and patterns: with examples in C # And. net, by Jimmy Nilsson
●. Net domain-driven design with C #: Problem-design-

BDD (behavior-driven design, behavior-driven design) is considered as the result of the merger of TDD and DDD. BDD focuses on the behavior of the system, not just testing it. The specifications created when using BDD can use languages that are everywhere in the real world, which can benefit both technical users and business users.

The document generated when the BDD specification is used allows the reader to understand the behavior of the system in various situations, rather than simply verifying that each method is performing what they should do. By organically integrating several aspects of DDD with core TDD concepts, BDD will meet the needs of both business users and technical users. The standard unit test framework can be used to execute BDD, but a dedicated BDD framework has emerged, and BDD is about to become the next major event.

How to Learn

The name and intent reflect the purpose, usage, benefits, and motivation of the mode or principle.

UML diagram
Graphical representation of the structure of the presentation mode or principle. Graphical representation is used to display common solution templates and their implementations.
Code Implementation

How to select and use design patterns
● You cannot use modes without understanding them. First, expand your knowledge and use abstract to concrete methods to learn models and principles.
● Whether it is necessary to introduce the complexity of the design model. It is necessary to measure the time required to implement a certain model and the benefits it can bring. Keep in mind the KISS Principle.
● The problem is generalized, and the problem is being identified in a more abstract way. Understand how each mode and principle is written. And check whether the problem to be solved meets the specific mode or principle. The design pattern is a high-level radical solution. Try to abstract the problem and do not focus too much on the details of the specific problem.
● Understand similar models and other models in the same group.
● Encapsulate the changed part. Understand what may change in the application.
● After selecting the design mode, make sure that the participants in the naming solution use the language and domain language of the mode. Fedexshippingcoststrategy uses the Strategy Mode to price different express delivery companies

Related Article

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.