How to divide an assembly in programming practice?

Source: Internet
Author: User

The program design language discussed in this article is. NET, at least for C. This article does not detail the differences between namespaces and assembly, but discusses how to apply these differences in programming practices. This article focuses on the Assembly and the division of namespaces will be discussed later.


In the. NET language, at least C #, namespace and assembly (dll extension, represented as a project on Visual Studio) are independent of each other. An assembly can contain multiple namespaces, and a namespace can be scattered across multiple datasets. This is almost a well-known textbook answer. However, when programming, some people still don't know when to create a project, and what is the relationship between the namespace of the new project and the old project.

Assembly division is a high-level application of object-oriented design concepts and design patterns.
Basic Principles

The basic purpose of an assembly is to increase the number of files to reduce the volume of a single file-a trade-off. It is enough to distribute the functions of a program in multiple datasets and allow only the updated assembly to be updated when the program is updated.

Principle 1 encapsulate changes: Put the program logic that tends to change into a program set.

Further, if there are different approaches to accomplish the same thing, and these practices are complex enough to meet the Assembly level, create an interface and each practice is built into an independent assembly, this interface is implemented.

Principle 2 Separation of interfaces and implementations: Each logic that can replace each other is made into an assembly.

Example

I used to organize the user dictionary of the input method. It reads the dictionary, deletes and saves low-frequency words. The logic for deleting low-frequency words is fixed, while the read/write logic changes with different file formats. (I believe that Google's input method, sogou's input method, and Ziguang pinyin all have different user dictionary formats .)

Therefore, create a project and define only one Interface, which is the Assembly Interface.

Create a new project and implement the interface to read and write the Google input dictionary. This is googleReader.

Create a project and implement the interface to read and write the sogou input dictionary. This is dogReader.

After the dictionary sorting software is released, if the user says that he wants to organize the dictionary of the QQ input method, you only need to write another dll and publish it online.


If a logic needs to be used by multiple solutions (whether or not they are related), this logic is complex enough to meet the Assembly level, then it is written as an assembly.

At the beginning, it was impossible to say that A, B, and C all needed something and asked what to do. It must have been written as A first, and all of them have been completed. After A while, I wrote B. "Oh, it seems like I have written these codes before." I want to find the original code in program A. "Copy it !" After A while, I wrote C, "Hey, I 've encountered this problem before." I used to write code for A and B to solve this problem, simply extract it into an Assembly. A, B, and C reference it!"

Or if you want to share code on the internet, instead of sharing the entire application, you can extract the code into a project and package it for download. Others can directly import their own projects.

Principle 3 Independence Principle: extract the logic of multiple dependencies without relying on others into an assembly.

If you are writing a program with a lot of code, remember to divide the assembly. If an exe is larger than kb, I will try to extract some of its logic into the new Assembly. Otherwise, if you modify the code of this exe, You need to compile all the code and write kb.

If the Assembly is divided and the code of this exe is modified, you may only need to compile kb for recompilation. Similarly, if you modify the code of the new assembly (if it does not affect its appearance) and uninstall the exe project when compiling it, it will only compile kb. Save time and space.

Principle 4 streamlining principle: Too large exe or dll should be subdivided.

Principle 5 unchanged principle: the logic separation that is not easy to change is an assembly. This is the correspondence of principle 1.

Author: gqqnb

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.