当顶级命名空间包含基类且内部命名空间包含子类时,C# 命名空间和类/子类命名约定
我正在尝试为特定的工程应用程序设计一个类库,并且我正在尝试确保我的类和类库命名空间命名约定是有意义的。
我遇到以下情况:
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
...
}
...
}
代码的使用方式需要在同一范围内引用所有这些类。可能会出现以下情况:
...
Vehicle.Wheel.DoSomething();
Vehicle.Truck.Wheel.DoSomething();
Vehicle.Car.Wheel.DoSomething();
...
在这些情况下,我是否最好为类提供更具体的名称
namespace Car{
class CarWheel: Vehicle.Wheel{...} //Car specific Wheel object
...
}
,还是保留第一个示例中所示的命名并依赖命名空间中编码的信息来清楚起见?在后一种方法下,我想我想在使用这个库的代码中为了清晰起见而使用 alaising,对吗?
似乎是多余的
Vehicle.Car.CarWheel
拥有:或
Vehicle.Truck.TruckEngine
,但我也希望有非常具有描述性和具体的类名。
从哲学上讲,我要问的是,在考虑类名是否具有足够的描述性时,是否将命名空间作为类名的一部分。
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.
I have the following situation:
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
...
}
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?
It seems redundent to have:
Vehicle.Car.CarWheel
or
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常命名空间是复数的,以免与类名冲突(例如,您可能需要名为
Vehicle
和Car
的类),因此我倾向于使用命名空间如下所示:至于类的名称,通常会在类名称前加上专门化前缀,特别是当它们可能一起使用时,因此您最终会得到类似的结果:
您可以看到这种类型的 ' .NET Framework 中的任何地方都存在“冗余”,例如在 System.Xml 命名空间中,几乎所有类都以
Xml
为前缀,或者在 System.Data.SqlClient 中 命名空间大多数类都以Sql
为前缀。这意味着您可以使用using
指令导入命名空间,然后不必在整个代码中完全限定类名,例如,以下哪一个更具可读性?或者
很明显两者都在做什么,但第二个要短得多。
请注意,如果您确实在名称中包含专业化,那么您可能会发现不需要所有嵌套命名空间(
.Cars
、.Trucks
等)如果它们通常一起使用,这会变得很痛苦,因此使用它们的每个文件都必须导入所有名称空间,例如,如果您发现相同的
using
指令堆栈位于每个文件的顶部,然后将类折叠到一个命名空间中。通常,您将预期在单个命名空间中一起使用的所有相关功能包含在内,并且仅将嵌套功能用于扩展基本命名空间但使用频率较低的功能。Typically namespaces are pluralized, so as not to collide with class names (e.g. it is likely you would want classes named
Vehicle
andCar
) so I'd be inclined to use namespaces as follows: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:
You can see this type of 'redundancy' everywhere in the .NET Framework, for example in the
System.Xml
namespace virtually all classes are prefixed withXml
, or in theSystem.Data.SqlClient
namespace most classes are prefixed withSql
. It means that you can import namespaces with theusing
directive and then not have to fully-qualify class names throughout your code, e.g. which of the following is more readable?or
It's obvious what both are doing, but the second is considerably shorter.
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.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.我会尝试避免在不同的命名空间中重复使用名称,特别是当客户端可能想在同一个程序中使用这两个名称时。
您真的需要
Car
、Truck
等的命名空间吗?所有这些命名空间听起来更像是类,而不是命名空间。也许在你的实际情况下它更有意义......I would try to avoid reusing names across different namespaces, particularly if a client may want to use both in the same program.
Do you really need a namespace for
Car
,Truck
etc? All these namespaces sound more like they ought to be classes than namespacese. Perhaps in your real situation it makes more sense though...