类是否应该具有与名称空间相同的名称?

时间:2022-05-14 23:53:19

I'm designing a namespace to hold a set of classes that will handle user related tasks for a several different applications. (Log-in, authenticate etc)

我正在设计一个名称空间来保存一组类,这些类将处理几个不同应用程序的用户相关任务。(登录、身份验证等)

The problem is the namespace will be called Fusion.User but then it requires a class in that namespace that makes sense to call User.

问题是命名空间将被称为Fusion。但是,它需要一个名称空间中的类,这对调用用户是有意义的。

Should you have a class with the same name as the namespace? Or am I taking the wrong approach here?

是否应该有一个与名称空间同名的类?还是我在这里采用了错误的方法?

5 个解决方案

#1


1  

Having class named in the same way as the name space (package) may lead to a thought that class is central to the package. But if I get it correctly User is just a data object in your case.

以与名称空间(package)相同的方式命名类,可能会导致认为类是包的中心。但如果我做对了,用户只是数据对象。

As far as I see you have 2 options:

在我看来,你有两个选择:

  1. Name your name space differently e.g. Fusion.Security
  2. 将你的名字空间命名为不同的名称,例如:fusion.securityofz
  3. Use suffix for class name indicating its purpose e.g. UserDTO, UserAction etc.
  4. 类名使用后缀表示其目的,如UserDTO、UserAction等。

#2


1  

The namespace is Fusion.User
Class Full Name would be Fusion.User.User

名称空间是融合。用户类的全名应该是Fusion.User.User

It is a good practise to keep them different because

让他们与众不同是一种很好的练习。

  1. It avoids confusion to the developer
  2. 它避免了开发人员的困惑
  3. It also looks ugly in some cases like here we are using two user.

    在某些情况下,它看起来也很难看,比如这里我们使用两个用户。

    using Fusion;
    namespace xyz {
    public class test
    {
    User.User userObject {get;set;}
    }
    }
    So the better option would be to use different names

    使用融合;命名空间xyz {public class test {User。用户userObject {get;set;}}所以更好的选择是使用不同的名称

#3


1  

There are instances where using the same name will cause problems. One that leaps immediately to mind is when consuming a WCF service. When I did this recently in a class called "someBehaviour" in the namespace "companyName.someBehaviour" to consume "MyService", the compiler barfed on me saying that MyService didn't exist within the someBehaviour namespace. Changing the class name to something different (and vastly more useful) solved the issue and allowed me to compile the assembly.

在某些情况下,使用相同的名称会导致问题。当使用WCF服务时,会立即想到一个问题。最近,我在一个名为“companyName”的类中做了这一点。为了使用“MyService”,编译器对我进行了咆哮,说MyService不存在于someBehaviour名称空间中。将类名更改为不同的(更有用的)解决了这个问题,并允许我编译程序集。

#4


1  

See Do not name a class the same as its namespace post of Eric Lippert(MSFT)

请参见不要将类命名为与Eric Lippert(MSFT)的命名空间帖子相同的名称。

#5


0  

I'd probably call the name space 'usertasks' to avoid any confusion. You are going to have to qualify the inner class using the namespace regularily to avoid confusing the compiler.

我可能会将名称空间命名为“usertasks”,以避免混淆。您将不得不使用名称空间定期限定内部类,以避免混淆编译器。

#1


1  

Having class named in the same way as the name space (package) may lead to a thought that class is central to the package. But if I get it correctly User is just a data object in your case.

以与名称空间(package)相同的方式命名类,可能会导致认为类是包的中心。但如果我做对了,用户只是数据对象。

As far as I see you have 2 options:

在我看来,你有两个选择:

  1. Name your name space differently e.g. Fusion.Security
  2. 将你的名字空间命名为不同的名称,例如:fusion.securityofz
  3. Use suffix for class name indicating its purpose e.g. UserDTO, UserAction etc.
  4. 类名使用后缀表示其目的,如UserDTO、UserAction等。

#2


1  

The namespace is Fusion.User
Class Full Name would be Fusion.User.User

名称空间是融合。用户类的全名应该是Fusion.User.User

It is a good practise to keep them different because

让他们与众不同是一种很好的练习。

  1. It avoids confusion to the developer
  2. 它避免了开发人员的困惑
  3. It also looks ugly in some cases like here we are using two user.

    在某些情况下,它看起来也很难看,比如这里我们使用两个用户。

    using Fusion;
    namespace xyz {
    public class test
    {
    User.User userObject {get;set;}
    }
    }
    So the better option would be to use different names

    使用融合;命名空间xyz {public class test {User。用户userObject {get;set;}}所以更好的选择是使用不同的名称

#3


1  

There are instances where using the same name will cause problems. One that leaps immediately to mind is when consuming a WCF service. When I did this recently in a class called "someBehaviour" in the namespace "companyName.someBehaviour" to consume "MyService", the compiler barfed on me saying that MyService didn't exist within the someBehaviour namespace. Changing the class name to something different (and vastly more useful) solved the issue and allowed me to compile the assembly.

在某些情况下,使用相同的名称会导致问题。当使用WCF服务时,会立即想到一个问题。最近,我在一个名为“companyName”的类中做了这一点。为了使用“MyService”,编译器对我进行了咆哮,说MyService不存在于someBehaviour名称空间中。将类名更改为不同的(更有用的)解决了这个问题,并允许我编译程序集。

#4


1  

See Do not name a class the same as its namespace post of Eric Lippert(MSFT)

请参见不要将类命名为与Eric Lippert(MSFT)的命名空间帖子相同的名称。

#5


0  

I'd probably call the name space 'usertasks' to avoid any confusion. You are going to have to qualify the inner class using the namespace regularily to avoid confusing the compiler.

我可能会将名称空间命名为“usertasks”,以避免混淆。您将不得不使用名称空间定期限定内部类,以避免混淆编译器。