Professional Asp. Net Design Pattens Reading Notes (chapter 2)

Source: Internet
Author: User

I. Introduction

These two chapters mainly introduce some popular object-oriented models and some OOAD principles. The book only mentions the tip of the iceberg of these concepts, it is a very long process to grasp these concepts and principles. It may be difficult to fully understand these concepts and principles over the past two or two years. Therefore, I will not explain them too much here, I need to think about it myself.
These concepts mainly include:

Common design modes
Recommended learning materials: Getting Started: big talk design patterns; Head First design patterns; DoFactory
Improvement: there is a series of video tutorials in the blog site "C #3.0 Design Patterns" and the MSDN WebCast.

2: OOAD principle: KISS/SOLID
These principles are not difficult to understand, but it takes a long time to grasp and practice them. You can see a Chinese translation version in the "How I Explained OOD To My Wife" blog on CodeProject.

3: Mode
Repository, Unit of Work, Data Mapper, Active Record, Transaction Script, etc.

Must be the enterprise application architecture model

4: TDD, DDD, and BDD
Test-driven development (TDD) has never been concerned about it. Recently, I have put it into practice. Simply put, I want to write unit tests and business code first. I 'd better google it myself. Domain-driven design (DDD) currently has two good books on the market, "Domain-driven design: the way to cope with software core complexity and the field-driven design and pattern practice have never been touched by behavior-driven design BDD;

 

Ii. How to select and apply the Model

1. you cannot use this mode without understanding it (it seems a bit nonsense). The most important thing is to learn and practice various modes from two aspects: Abstraction and concrete as much as possible, you can create multiple implementations for a mode. The more you practice, the more you will understand the intention of the mode. In addition, you can use different methods to solve a problem.

2. Do I need to introduce complicated design patterns? It is very common for developers to use a Learning Mode to solve all problems. You must determine whether the benefits of this model are worthwhile. Compliance with KISS principles

3. look at the problem with a more abstract perspective, and write down the intention of each related model to see if it is suitable for solving your problem. Remember that the design mode is a high-level solution, try to make the problem more abstract and do not focus too much on details

4. When you use a mode to solve a problem, you need to consider other modes similar to this mode. Maybe you will find it appropriate.

5. encapsulation changes. Find the place where the application will change. For example, if a discount scheme changes over time, find a pattern to solve this change, so that his changes will not lead to other changes in the application.

6. When using the mode, you can mark this mode on the name. For example, if the policy mode is used, the related classes can be named XXXStrategy, which can be better understood by others.

3. A simple example

1: Negative textbooks

Problems:

(1) Since ProductService, ProductRepository and Repository have to be changed to Service.

(2) The GetAllProductsIn method in Service is almost impossible to test. The first two classes are coupled, and the second is coupled with HttpContext.

(3) Http Context is used for caching, but if the system needs to use a cache system like Memcached for caching, the Service code needs to be modified. (I don't know why Memcached belongs to google)

2 reconstruction:

(1): Let the Service and Repository be decoupled here the first thing we should consider here is to use an IReository interface to make the Service dependent on the interface, instead of a specific class. In fact, if you think about it carefully, it is our "Dependency inversion" principle (dependency and abstraction, not specific ). So we added an IProductRepository interface to the project. With IDE, I can handle it more conveniently,

Then VS automatically generates the corresponding code for us.

Then we only need to modify the code in our ProductService to solve this coupling problem.

In fact, we need to call the specific ProductRepository in the PorductService constructor.

In this case, there is coupling. If we need to modify the Repository and the code in the Service still need to be modified, how can we prevent the new ProductRepository from appearing here? Otherwise, pass in the parameter.

Good. So the code is changed to this

In this way, the repository can be specified by the client. Here, it is only responsible for calling and the coupling problem is solved. In fact, this is the simplest "dependency injection ". The concept of dependency injection is not mentioned here. This concept is simple and complicated. For example, the IoC container allows us to instantiate objects outside the Code through configuration.

(2): The GetAllProductsIn method coupling problem in Service. because we do not have the source code of HttpContext, it is impossible to implement dependency Injection Using interface separation. Here we introduce a mode called the Adapter mode, which will not be explained here, simply put, our socket converter can convert two common sockets into three. See the following two figures.

Adapter mode class diagram

From the logic that the GetAllProductsIn method mainly needs to implement, We can abstract these methods, query the cache, insert the cache, and remove the three main functions from the cache. As shown in.

Class diagram in the example

According to the class diagram above. We will change the code to the following:

First, inject ICacheStorage into the Service

Second, modify the GetAllProductsIn method. The green comment part is before the modification.

Finally, the ICacheStorage method is implemented.

In this step, the reconstruction is complete. This simple example can be better expanded.

3. Reconstruction and Testing

(1) Although the above steps have basically made the program more object-oriented. However, we still find some dissatisfaction. For example, if the client does not want to use the cache policy when calling the Service, this code must be modified. How can we comply with the OCP principles? So we will introduce another mode. The Null Object Mode is very simple. It is to pass a "null" Object when passing cache parameters to the Service constructor ., We implement a null class.

(2) test ProductService

Unit Testing is very important, but it is not discussed here. (Because I do not have good practice. J), you can read the following series of articles.

Http://www.codeproject.com/KB/cs/autp1.aspx

Http://www.codeproject.com/KB/cs/autp2.aspx

Http://www.codeproject.com/KB/cs/autp3.aspx

Http://www.codeproject.com/KB/cs/autp4.aspx

Http://www.codeproject.com/KB/architecture/autp5.aspx

Http://www.cnblogs.com/huyh/archive/2010/06/14/1758143.html

Iv. References

Professional Asp. Net Design Pattens

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.