Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to have a namespace name which exists at two points in the tree?

Tags:

c#

.net

Is this OK:

namespace Simple.OData
{
    // Common OData functionality
}

namespace Simple.Data.OData
{
    // The Simple.Data adapter for OData
}

It feels like it might be wrong, but I'm not sure.

like image 805
Mark Rendle Avatar asked Sep 05 '11 12:09

Mark Rendle


People also ask

Can namespace and class have same name?

Inside a namespace, no two classes can have the same name.

What is the difference between name and namespace?

Names are what can be traditionally thought of as "variables". Namespaces are the places where names live.

Why do we use namespace in C sharp?

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

Is namespace mandatory in C#?

There is no need to have a namespace. However developer studio expects you to be using a name space. For example, when you choose to add a class to a project developer studio will: Create a file for the class.


2 Answers

It's certainly valid - consider System.Xml.Linq and System.Linq. I can't immediately foresee any problems... but that's not to say it's necessarily a good idea.

Personally I prefer Simple.Data.OData over Simple.OData.Data, as I suspect this is primarily aimed at people who are using Simple.Data, but happen to be using OData - not people who are focused on OData. Again, this is like LINQ: System.Xml.Linq is an XML API which plays will with LINQ; it's not a LINQ "provider" as such.

Basically it's the same sort of problem as "I have a converter to convert from type A to type B; do I put it near type A or type B?" - but with namespaces. My experience is that usually more head-scratching goes into thinking of the best thing to do than the problems that would be caused by taking either approach...

like image 59
Jon Skeet Avatar answered Oct 17 '22 04:10

Jon Skeet


More correct would be, namespace Simple.OData.Data.

This is because the Data namespace should be grouped with the other classes relating to OData.

If you're thinking along with lines of System.Data, System.Data.SqlClient then it's pretty much because they are a part of the System.Data.dll assembly, and are an integrated part of it. My own implementation of the IDbCommand etc classes live in MyNamespace.SubNamespace.AdoWrapper namespace, if that gives you some context.

In your case, Simple.Data presumably doesn't exist or have much in it, unlike System.Data ..

like image 45
Kieren Johnstone Avatar answered Oct 17 '22 04:10

Kieren Johnstone