简体   繁体   English

命名约定和命名空间

[英]Naming Conventions and Namespaces

If I have objects on one layer with the same name as objects on another layer, is it best to change the object names with some prefix or have new namespace and refer to them with fully qualified names? 如果我在一个图层上有与另一个图层上的对象同名的对象,最好是用一些前缀更改对象名称还是使用新的命名空间并用完全限定的名称引用它们? For example: 例如:

namespace Project1.Data
Object Person;

namespace Project1.Model
Object Person;

Data.Person.Name=Person.Name;

OR

dbPerson.Name= Person.Name;

I'd use namespaces and namespace aliases, eg: 我使用命名空间和命名空间别名,例如:

Define your classes in appropriate namespaces: 在适当的命名空间中定义类:

namespace Project1.Data
{
    public class Person {...}
}
namespace Project1.Model
{
    public class Person {...}
}

And where you use the classes, either use fully qualified names or define an alias for the namespaces (especially usefule if the full namespace is long): 在使用类的地方,要么使用完全限定名称,要么为命名空间定义别名(如果完整命名空间很长,则使用usefule):

using data = Project1.Data;
using model = Project1.Model;

data.Person p1 = new data.Person();
model.Person p2 = new model.Person();
//...
p1.Name = p2.Name;

It depends on how often you're referring to the overloaded name. 这取决于您引用重载名称的频率。

If you use it several times in one file, then use the first way. 如果您在一个文件中多次使用它,请使用第一种方式。

If you use it only once or twice, then write out the fully qualified name, so that other people won't have to go hunting around at the top of your file to figure out what object you're referring to. 如果您只使用它一次或两次,那么请写出完全限定的名称,这样其他人就不必在文件顶部狩猎来找出您所指的对象。

It really depends on the frequency you're requesting each of them. 这实际上取决于您要求每个人的频率。 Generally, I use the shortened version for the type I'm referring to most frequently, and use the longer name for the type which is less frequently used. 一般来说,我使用的缩写版本最常用于我所指的类型,并且对于不常使用的类型使用较长的名称。 I'd say eventually, if you end up having a lot of usages of both in the same file, that you should use namespace aliasing, but for me, that's a last resort only after the code has bloated to a point where it's hard to follow what's going on. 我最后会说,如果你最终在同一个文件中有很多用法,你应该使用命名空间别名,但对我而言,只有在代码膨胀到难以达到的程度之后,这才是最后的手段。跟着发生了什么。

Had the same thought myself. 有同样的想法自己。 I think chaging the name of the classes is a bad Idea. 我认为查找类的名称是一个坏主意。 For instance I have a data access layer and a business layer. 例如,我有一个数据访问层和一个业务层。 Both deal with users. 两者都与用户打交道。 So I have... 所以我有...

Project1.Business.User Project1.DataAccess.User Project1.Business.User Project1.DataAccess.User

trying to think of inventive new names for the classes is a waste of time and will probably mean odd names for classes with little meaning. 试图想出这些类的创造性新名称是浪费时间,并且可能意味着对于没有意义的类的奇怪名称。 Naming classes can be enough of a headache already. 命名类已经足够令人头痛。

I agree with McWafflestix "I use the shortened version for the type I'm referring to most frequently, and use the longer name for the type which is less frequently used". 我同意McWafflestix“我使用的缩写版本最常用于我所指的类型,并且使用较长名称的类型较少使用”。

It's simple. 这很简单。 Listening to the .NET framework guidelines for once actually helps (although plenty of material in the book is just plain Elements of Java style Yet Again in Redmond Wonderland).. 一次听.NET框架指南实际上有所帮助(尽管本书中的大量内容只是Java风格的简单元素,但在Redmond Wonderland中再次出现)

You should avoid similar type names in cross or intra-project/library mixing namespaces ie. 您应该避免在交叉或项目内/库混合命名空间中使用类似的类型名称,即。 mixing across domains and models in generial ( even in C++ one that is extremellly strict and powerful, it also has an incarnation in compiler, resolution and enum-style compiler crashes and problems). 跨域和模型混合(即使在C ++中极其严格和强大,它也有编译器,解决方案和枚举式编译器崩溃和问题的化身)。

Therefore even fully qualifying all the time is no fool-proof (and btw aliases and 'using' are extremely limited and cause mild duplication at best, as well as prove C# weakness in generic programming etc ). 因此,即使完全符合条件也不是万无一失(btw别名和'使用'是极其有限的,并且最好导致轻微的重复,以及证明通用编程中的C#弱点等)。

In my experience, Data domain types are a primary target for a more appropriate name, and thus for name refactoring which is: 根据我的经验,数据域类型是更合适的名称的主要目标,因此对于名称重构,它是:

a) cheap (as a process in rich ASTs but simple adt-s support like in C#, right-click in IDE and feel powerful according to type-challenged dynamic Ruby fans/backers ) a)便宜(作为丰富的AST中的过程,但在C#中提供简单的adt-s支持,在IDE中右键单击并根据类型挑战的动态Ruby粉丝/支持者感觉强大)

[can also be read as: 4.0 dynamic features sheep will blame everyone but not think about namespaces or functional JS, C-with templates(not C-with-classes), or similar ] [也可以理解为:4.0动态功能绵羊会责怪每个人但不考虑名称空间或功能JS,C模板(不是C-with-classes)或类似的]

b) communicates the semantics better ie. b)更好地传达语义,即。 the intent (ie. plumbing + support to build your own processing ) 意图(即管道+支持构建自己的处理)

c) usually of primitive but typed nature or message ( typed not OO; ie. OO-style critique as in aforementioned book which itself breaks straight out of intro lifts all 'Models' to reference land) c)通常具有原始但有类型的性质或信息(不是OO类型;即OO风格的批评,如前面提到的那本书直接突破介绍所有“模型”引用土地)

d) and 'aliasing' becomes a useful artifact in cross-domain usage (which is actually possible and quite 2020-like.. ie. value-type programming ) d)和'别名'在跨域使用中成为一个有用的工件(这实际上是可能的,并且非常像2020年......即价值型编程)

There really are no rules but beware that you will see mixing of namespaces in development when least expected.. which means only one thing for a managed-minded dev: confusion. 确实没有规则,但要注意你会在最不期望的时候看到开发中名称空间的混合......这意味着对于一个有管理意识的开发人员来说只有一件事:混乱。 Plus somewhat less serious, more compile-time and IntelliNonsense errors of course.. 加上稍微不那么严重,更多编译时和IntelliNonsense错误当然..

Tough problem in all languages, so it is your design/naming issue.. Even tool vendors can mess up for machines to parse.. say output of enhanced popular IDEs based on outdated browse information; 所有语言都存在严峻的问题,因此这是您的设计/命名问题。甚至工具供应商也可以搞砸机器来解析...说基于过时浏览信息的增强型流行IDE的输出; then again, others do it real well for managed languages. 然后,其他人对托管语言做得很好。

Not that I am against duplicating names, there are cases (they are tough but necessary) when mixing dual + interop + representation etc other models where same name makes it more readable; 并不是说我反对复制名称,有些情况(它们很难但很必要)混合双+互操作+表示等其他模型,其中相同的名称使其更具可读性; ie. 即。 where duplication is a necessity of dual-usage.. but that is lower level idioms that C# is not friendly or encouraging of (re: in favour of overhead). 复制是双重用法的必要性..但这是C#不友好或不鼓励的低级习语(re:赞成开销)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM