开发者

C#: namespaces and classes structure

开发者 https://www.devze.com 2023-01-13 05:07 出处:网络
Coming from Java ,开发者_StackOverflow I\'m used to the package structure (com.domain.appname.tier)

Coming from Java ,开发者_StackOverflow I'm used to the package structure (com.domain.appname.tier) Now I've started working on a C# project , where all the projects have depth of 1:

i.e

ProjectA

- Utilities.cs

- Validation.cs

- ....

- Extraction.cs

and all the cs files are around 2,500 lines long ...

How do you order your classes and namespaces in C# so it will make sense , and keep the source file in logical size ?


The same way as I'd imagine you do in Java:

  • A few (< 10?) classes in each namespace, with namespaces arranged in a hierarchy
  • One class per source file
  • One or two screenfuls of text per source file

The project you've joined doesn't sound very structured and isn't a good example of good source code organisation.


In a similar way in Java, you just need to make some effort :) Some C# developers, especially with VB background, tend to write looooong classes and put them at the top level.


I would suggest reading Microsoft guidelines on the subject:

Design Guidelines for Developing Class Libraries

In particular you should look at the following section:

Guidelines for Names

Even if you are not writing a class library you may still benefit a lot from these guidelines. FxCop (or Code Analysis as it is named now) will flag many constructs that are not in accordance with these guidelines.


I would first start grouping the classes together into areas of functionality, areas around authorisation for example would go under a folder within a project.

Then update the namespaces of the classes in the folder to reflect the change, Resharper does this for you and newer versions of VS will probably do too.

Lastly (if you are able) I would start to break the classes to smaller more manageable size.


Here's an example of how I organize my solutions, which mirrors the namespace structure.

C#: namespaces and classes structure

The project has a default namespace which, in this case, is CompanyName.ProjectName Source files are organized logically into a directory structure. In the example, my WF4 activity designers are organized under Activities in a folder called Designers.

The way VS works is that, as you create directories in a project, you are also creating namespaces. So, if I were to add a new activity designer called "Foo" in the shown directory, its namespace would be

"CompanyName.ProjectName.Activities.Designers"

Visual studio takes the default namespace, then uses the folder structure to determine the namespace for a particular file. Of course, once the file is created, and you move a file, it isn't automatically refactored. But the system works very well for not only controlling namespaces for classes, but also for keeping files organized.


The same way as you would in Java.

In Java, packages organize classes in physical directories. I'm not sure about this, but the compiler even encourages this convention IIRC. In C# you're not obliged to organize your classes into separate directories that match your namespaces, but it's a very common convention though.

Speaking of namespaces in C#, they do not follow the com.domain.appname.tier convention, but use the Company.Product.Tier format.

How to reorganize large classes depends on the application. This is an exercise in applying OOP guidelines and applies to both Java and C#.


if you are deeply engaged in the project ,i recommend investing some time in redesinging the stucture the way you used to in java ,considering that packages are equivalent to namespaces in c#.

0

精彩评论

暂无评论...
验证码 换一张
取 消