Name collision between class and namespace

Asked

Viewed 653 times

8

It’s wrong, bad practice or I might have problems with class like namespace? Example:

namespace Cielo {
    public class Cielo {
        ...
    }
}

It’s been working, in some places it gets a little strange to call Cielo.Cielo.etc.

You should change the class for example to CieloService or something like?

  • 5

    Unfortunately, the convention to use Uppercamelcase also in namespace names can cause technical problems when using the same name in the class and in the namespace (in java the convention is to name namespaces with lowercase letters, avoiding the possibility of collision). In his article, Eric Lippert says that using the same name would be a design flaw in the hierarchy, but he cites a good example Pobrinho (a namespace List classy List), only that in Enterprise applications it is common to want namespace and class with equal names; and in C# we ended up having to forge pro namespace an unnatural name :-/

  • This is a valid hierarchy pattern in Enterprise systems: company.domain.context.entity.Rootentity, company.domain.context.entity.Aggregateentity1, company.domain.context.entity.Valueobject1. For example: dorathoto.erp.Stock.product.Product, dorathoto.erp.stock.producto.Category, dorathoto.erp.stock.product.Location. In C# I have to forge another name for the aggregation namespace of the entity Product. Which name would be more natural than simply "Product"?

2 answers

9


Power, can, but is not recommended. Gets confused. There is an official recommendation.

There’s an article by Eric Lippert about this.

The ideal is to name things as they are. The class is a Cielo? Or she’s a Service specific to the Cielo?

Already the namespace is from Cielo, right? Everything that’s inside is theirs. If you’re not Cielo you shouldn’t use this name.

It gave to understand how to name?

  • Yes, he made it clear that it is necessary to rename.. but still about the names I do not know. The System is a webservice of Cielo, has several services, Webservice, Windowform, but I think I will follow in namespace Cielo and Cielowebservice class

4

None of the options.

The namespace serves to give context to the components you are using. It is only useful to the developer.

Name a class with the same name as namespace will generate some confusion, because sometimes it will not be possible to use the using normally, you will always have to be explicit: var cielo = new Cielo.Cielo();.

Anyway, matter of taste. I recommend that, if you give, avoid doing this, as the example you gave CieloService or CieloClient, I don’t know.

But it’s not wrong, and I’ve never read anything and I don’t see how malpractice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.