I have a problem how to go about naming my namespaces and classes. I already figured they shouldn’t both have the same name, as it causes all kinds of problems. Most notable problem for me is that I need to fully qualify the class or make an alias. Here is what I have:
Model.DataSource.DataSource Model.DataSource.FileSystemDataSource Model.DataSource.JsonDataSource Model.DataSource.XmlDataSource Model.DataSource.DataSourceConstraints Model.DataSource.DataSourceStorage Model.DataSource.DataTable ... // more others Model.QueryStorage.QueryStorage Model.QueryStorage.FileSystemQueryStorage ... // more others Model.Project.Project Model.Project.ProjectFactory
As you can see, I have 3 classes that are named same as packages they are in. Two of them are interfaces so I could consider prefixing them with
I but I really don’t like this convention. I also heared the proposition to name package like
ProjectUtils or something. But I think these are not utils, they are legit domain objects. I could alias, with
using but what is a better name for
I can’t figure out a way to have sensible names for my classes without duplicates.
You’re right that you shouldn’t name the namespace the same as a type it contains. I think there are several approaches you can use:
This works especially well if the primary purpose of the namespace is to contain types that inherit from the same base type or implement the same interface.
If a namespace contains only a small number of types, maybe you don’t need that namespace at all.
This can work especially for features that are important part of your product, so they deserve their own name.
Two of them are interfaces so I could consider prefixing them with
Ibut I really don’t like this convention.
You really should, if your code is used by others (i.e. it’s a library) or if you don’t want to confuse newcomers to your code.
.NET design guidelines requires
I prefix for interfaces. It recommends singular form for classes (
Collection suffix for everything implementing
IEnumerable) and plural form for namespaces.
You need to prefix namespace names with a company name to prevent namespaces from different companies from having the same name.
eventually your namespace must have this structure: