当顶部命名空间包含基类而内部命名空间包含子类时,C#名称空间和类/子类的命名约定 [英] The C# namespace and class/sub-class naming conventions when the top namespace contains the base class and inner namespaces contain sub-classes

查看:105
本文介绍了当顶部命名空间包含基类而内部命名空间包含子类时,C#名称空间和类/子类的命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为特定的工程应用程序设计类库,并且正在尝试确保我的类&名称空间命名约定很有意义.

I'm trying to design a class library for a particular engineering application and I'm trying to ensure that my class & namespace naming conventions make sense.

我有以下情况:

namespace Vehicle{

    class Wheel{...} //base class for Wheel objects
    class Engine{...} //base class for Engine objects
    ...
    namespace Truck{ 
        class Wheel: Vehicle.Wheel{...} //Truck specific Wheel object
        class Engine: Vehicle.Engine{...} //Truck specific Engine object
        ...
    }

    namespace Car{ 
        class Wheel: Vehicle.Wheel{...} //Car specific Wheel object
        class Engine: Vehicle.Engine{...} //Car specific Engine object
        ...
    }
    ...
}

使用这些代码的方式是需要在同一范围内引用所有这些类.可能出现以下情况:

The code gets used in ways that all of these classes will need to be referenced from within the same scope. The following situation would be likely:

...
Vehicle.Wheel.DoSomething();
Vehicle.Truck.Wheel.DoSomething();
Vehicle.Car.Wheel.DoSomething();
...

在这种情况下,我最好给班级指定更具体的名称

Under these circumstances, am I better off giving the classes more specific names

namespace Car{
    class CarWheel: Vehicle.Wheel{...} //Car specific Wheel object
    ...
}

还是保留第一个示例中所示的命名,并依靠命名空间中编码的信息来保持清晰?在后一种方法下,我假设我想在使用该库的代码(Corret)中使用透明化来使代码更清晰?

or leave the naming as shown in the first example and rely on the information that is encoded in the namespace for clarity? Under the latter approach, I assume I would want to utilize alaising for clarity in the code that makes use of this library, corret?

似乎多余:

Vehicle.Car.CarWheel

Vehicle.Truck.TruckEngine

但是我也想拥有非常具有描述性和特定性的类名.

but I also want to have very descriptive and specific class names.

从哲学上讲,我要问的是,在考虑类名是否具有足够的描述性时,是否将名称空间作为类名的一部分.

Philosophically, what I'm asking is whether or not to include the namespace as a part of the class name when considering if a class name is descriptive enough.

推荐答案

通常,名称空间是复数形式,以免与类名冲突(例如,您可能希望使用名为VehicleCar的类),所以我倾向于按如下方式使用名称空间:

Typically namespaces are pluralized, so as not to collide with class names (e.g. it is likely you would want classes named Vehicle and Car) so I'd be inclined to use namespaces as follows:

namespace Vehicles;
namespace Vehicles.Cars;
namespace Vehicles.Trucks;

关于类的名称,通常会在类名称的前面加上专业名称,特别是如果它们可能一起使用时,那么最终会得到以下内容:

As for the names of classes, it would be typical to prefix the class name with the specialization, especially if they are likely to be used together, so you'd end up with something like:

class CarWheel : Wheel
class TruckWheel : Wheel

您可以在.NET Framework的任何地方看到这种类型的冗余",例如,在System.Xml名称空间中,实际上所有类都以Xml为前缀,或者在System.Data.SqlClient名称空间中,大多数类都以Sql.这意味着您可以使用using指令导入名称空间,然后不必在整个代码中完全限定类名,例如以下哪个更易读?

You can see this type of 'redundancy' everywhere in the .NET Framework, for example in the System.Xml namespace virtually all classes are prefixed with Xml, or in the System.Data.SqlClient namespace most classes are prefixed with Sql. It means that you can import namespaces with the using directive and then not have to fully-qualify class names throughout your code, e.g. which of the following is more readable?

Vehicles.Cars.Wheel wheel = new Vehicles.Cars.Wheel();

CarWheel wheel = new CarWheel();

两个人都在做什么很明显,但是第二个要短得多.

It's obvious what both are doing, but the second is considerably shorter.

请注意,如果您确实在名称中包含了特殊化名称,那么您可能会发现不需要所有嵌套的命名空间(.Cars.Trucks等),如果这些命名空间通常被使用,则会很痛苦在一起,因此使用它们的每个文件都必须导入所有名称空间,例如

Note that if you do include the specialization in the name, then you may find that you don't need all the nested namespaces (.Cars, .Trucks, etc.) which can become painful if they are usually used together, and so every file using them would have to import all the namespaces, e.g.

using Vehicles;
using Vehicles.Cars;
using Vehicles.Trucks;
using Vehicles.SomethingElse;
using Vehicles.YetAnotherThing;

如果在每个文件的顶部都找到相同的using指令堆栈,则将这些类折叠到单个命名空间中.通常,您会包括所有预期在单个名称空间中一起使用的相关功能,并且仅将嵌套功能用于扩展基本名称空间但使用频率较低的功能.

If you find this same stack of using directives is at the top of each file, then collapse the classes down into a single namespace. You typically include all related functionality that is expected to be used together in a single namespace, and only use nested ones for functionality that extends the base namespace but is less frequently used.

这篇关于当顶部命名空间包含基类而内部命名空间包含子类时,C#名称空间和类/子类的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆