Should the class have the same name as the namespace?

I am creating a namespace to store a set of classes that will handle user related tasks for several different applications. (Login, authentication, etc.)

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

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

+5
namespaces class-design naming-conventions
source share
5 answers

Having a class name in the same way as a namespace (package) may lead to the idea that the class is central to the package. But if I get it right, the User will be just a data object in your case.

As far as I can see, you have 2 options:

  • Name your namespace differently, for example. Fusion.Security
  • Use a suffix for the class name with an indication of its purpose, for example. UserDTO, UserAction, etc.
+1
source share

Fusion.User Namespace
The full class name will be Fusion.User.User

It’s good practice to keep them different because

  • This avoids confusion with the developer.
  • It also looks ugly in some cases, for example, here we use two users.

    using fusion;
    xyz namespace {
    open class test
    {
    User.User userObject {get; set;}
    }
    } So, the best option would be to use different names

+1
source share

There are times when the same name causes problems. The one that immediately catches your eye is about consuming a WCF service. When I did this recently in a class called "someBehaviour" in the namespace "companyName.someBehaviour" to consume "MyService", the compiler told me that MyService does not exist in the namespace someBehaviour. Changing the class name to something else (and much more useful) solved the problem and allowed me to compile the assembly.

+1
source share
+1
source share

I would call the namespace "usertasks" to avoid confusion. You will need to qualify the inner class using the namespace to avoid confusion with the compiler.

0
source share

All Articles