How should I organize my C# code by using .cs files and classes?
I build websites with c# and put the code of each single functionality into one .cs file. However, the code of some functionalitie开发者_如何学运维s can grow really large and become very hard to manage and debgug. To better manage the code in this kind of .cs files, should I sub-devide this .cs file into multiple .cs files (will this cause problem of the solution tree structure not as clear as before. Multi cs files only fulfill one same functionality) or should I create some inline classes (i.e., still in the same one single .cs file, but contains seveal classes)?
It seems that I don't have enough knowledge about how to well organize the code in my project by using .cs files and classes. Is there any standards or guidelines that I should follow? Is there any book or ducuments can be recommended?
Many thanks for your input!
Wei
You should follow the convention of one class per file. There are some variations; a partial class may be split among multiple files. Nested classes will be contained within the containing classes file.
If your following this and your files are still becoming large, it's likely that you're putting too much functionality within a class. A class should exist to satisfy a (single) behavior (known as Single Responsibility principal), and have the necessary state to carry out that behavior. Modifying behavior can be done in a variety of ways; inheritance, composition, etc.
There is not general rule which applies to all use cases, but a good starting point is to have one file per class. Obviously you should give class and file the same name. Regarding your problem, I would propose to focus on your code first. If a single class/file grows to large, there is probably a problem with your code. Have a look at these links:
http://en.wikipedia.org/wiki/Single_responsibility_principle
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
Is better to create one .cs file for each class into your business logic, it does give no problem to you for the development, only if you have them in the same namespace, if you place them in different namespaces you only have to use using wordkey to take the information of different classes into the namespace you imported.
As an example, you can have the different files:
Car.cs
Person.cs
Main.cs
and, inside Main.cs, you can use the classes and public members inside the other two files.
Is a best practice to write classes implementation in separate files from others and this gives no problems.
I hope this could be usefull for you.
See you.
For a general convention which works well and will give you some ideas, have a look at the C# coding standards docuement on the iDesign website.
Direct link to Coding Standards
Follow some simple conventions like
- One Page One Class
- Create Some your own conventions (or you may use some existing ) for Declaring Functions and Properties.
- Always Make Use of Regions to separate out logic.
- Always Isolate EventHandlers, Functions, Properties from each other. For example put all properties at the top and surround them with Region then Functions and then Event handlers like that..
- Always avoid duplication, or we can say Redundancy, means code written once should not be repeated again. (For that make use of concepts like polymorphism, Functions, Inheritance)
- Make things in classes private unless you require them somewhere else.
- Whatever functions you make make them generic.
精彩评论