Preface
Yes, we have learned how to use various paradigms (functional, imperative, and object-oriented) in F # for programming. However, it is still limited to writing in a single module. You must know that no matter which language or paradigm programming is used, if the project is large, it is not suitable to put all the code in a single module.
In conventional. Net projects (such as C # + ASP. NET), we often choose the concept of solution as the solution of the entire (independent) problem domain.
Project and file. These concepts are physically manifested as assembly (class library or executable program), class files, etc. If there are too many projects and files
Consider how to organize them. The following three layers are discussed respectively.
Solution level
The main consideration here is the relationship between projects. At this time, we can basically ignore the differences in languages. It can also be said that at this level, the language has little impact. So we put those in use
C # The Code organization principles used during development are moved for use. For example, Martin Fowler mentioned in enterprise application architecture model, such as Robert
The package design principles mentioned by Martin in Agile Software development also include discussions about the petshop architecture in the. NET community. The content in this regard has been
There are a lot of discussions, so I will not repeat them here.
Here is only a specific question: how to add references to other assembly. In F # CTP
Before 1.9.6.0, the # I and # R commands are required to add references to the Assembly. # I is used to specify the directory of the assembly to be referenced, # R is used to specify the path (including the file name, which can be
Relative or absolute path ). These two commands can be placed in both the code file and compilation options. Here is a tip: The. netframework node in the registry contains
Information about each. Net version. The assemblyfoldersex contains several directories. If the directory where the Assembly is located appears in assemblyfoldersex
# R and file name can be directly used to add references.
In the CTP version, you can add references to other assemblies (including references to other projects in the same solution) for the project as in the general C #/VB. NET project ):
# R can only be used for FSX script files or in compilation options.
Project level
Now, assuming that you have a sufficient understanding of the above design principles and applied them to complete the design, the next step is how to use F # To implement these designs. Now we enter the project
At this level, you need to consider the relationship between various code entities in the project. These entities can be physical source code files, or logical modules, types, and configurations. F # basic organization
Namespaces and modules are constructed. The concept of namespaces is the same as that in C. With reflector, we can see that the module is a static class after compilation. We need to know when adding members to the module.
Add a member to a static class. About namespaces and modules, we strongly recommend how lvxuwen organizes programs (up and down ).
File level
Now we need to consider the basic internal issues of the source code file. When using the functional programming paradigm, in addition to modules, you can also use the User-Defined type of F #. The types in F # are divided into two categories. One is tuple)
Or record type, which is similar to the class in C #; the second is the Union type, which is also called the sum type. You can see from reflector that the value of the tuples is a tuple class.
Tuple implements
Microsoft. fsharp. Core. istructuralhash and system. icomparable interfaces; records and union are directly implemented.
These two interfaces. For more information about the istructualhash interface, see jome Fisher's article.
When using the object-oriented programming paradigm, we can define. Net types like in C #, such as interfaces, classes, structures, enumeration, and delegation. Of course, there are a lot of programming details (we suggest you take a look at the articles I have written earlier), and you can adopt different solutions for the same problem. This requires us to learn more and make practical choices based on different needs.
Here is another specific question: how to use the signature file (Signature) in F #
File ). When I learned the C language, I came into contact with the concept of the function prototype. It gave the function name, parameter type, and return type. The function signature has the same meaning as the function prototype. If we put the letter in the module
The digital signature is extracted and placed in a separate file. This is the origin of the signature file. It can control the access modifier of functions in the module. If you want to use a signature file, you must control it.
Module files appear in pairs, and the file names are the same. For example:
F # code-mymodule. FSI
# Light
Module fslib. mymodule
/// Obtain the square value of a floating point number
Val square: Float-> float
/// Obtain the cube value of a floating point number
Val Cube: Float-> float
F # code-mymodule. FS
# Light
Module fslib. mymodule
Open System
Let pow x y = math. Pow (x, y)
Let square x = POW x 2.0
Let cube x = POW x 3.0
*. FSI is the signature file. The signature of two functions is defined here: Square and cube. *. FS is the implementation file. It must provide the implementation of all functions of the corresponding signature file. Its
The module of its Assembly can only access functions with signatures in *. FSI. You can see from reflector that for the three functions in mymodule. FS, square and
The modifier of the cube is public, and the POW is internal.
From this point of view, the signature file works much like the interface in C. But in fact, after compilation, there is no actually generated interface. Note that to add XML document comments to the Code, add them to the signature file (if any) instead of the module. Next let's take a look at how to add comments to the code.
Regular comment
In F #, the single-line comment uses //, while the multi-line comment uses (*... *).
XML document comment
If a document comment is added to the code, you can generate an XML document during compilation, and then use some tools (such as sandcastle) to generate easy-to-use help documents. Previous generation
As you can see in the code, you can directly use /// to add a document comment for the module or its members, which is easier than in C. At the same time, you can use the complete document comment format in C # (for example
Summary, Param, and other nodes ).
Finally, if you want to use the code in the C # class library in F #, you can refer to the article I wrote earlier: F # imperative programming to learn about this.
Project of F # can be compiled into a class library or executable application (console application or Windows application ). I plan to discuss these two aspects in the next article and try some practical small projects. I believe that the Code Organization will be more accurate at that time.
Summary
When learning F #, we can easily put the code in the same module to try or test it. But our programmers should not be casual people. As the project grows, the Code Organization problems change.
And we should pay more and more attention to it. During Development in,
The entire project organization is naturally divided into three levels: solution, project, and file. This article discusses the basic issues of code organization at these three levels. It is easy to write. Thank you!
You can leave a message for discussion.