views:

896

answers:

9
+5  Q: 

.NET namespaces

My background is primarily as a Java Developer, but lately I have been doing some work in .NET. So I have been trying to do some simple projects at home to get better at working with .NET. I have been able to transfer much of my Java experience into working with .NET (specifically C#), but the only thing that has really perplexed me is namespaces.

I know namespaces are similar to Java packages, but as from what I can tell the main difference is that with Java packages they use actual file folders to show the seperation, while in .NET it does not and all the files reside in a single folder and the namespace is simply declared in each class.

I find this odd, because I always saw packages as a way to organize and group related code, making it easier to navigate and comprehend. Since in .NET it does not work this work this way, overtime, the project appears more overcrowded and not as easy to navigate.

Am I missing something here? I have to be. Should I be breaking things into separate projects within the solution? Or is there a better way to keep the classes and files organized within a project?

Edit: As Blair pointed out this is pretty much the same question asked here.

+10  A: 

I can't claim that it's a best practice, but I often see files organized in a directory hierarchy that mirrors the namespace. If it fits your mental model of the code better, then do so - I can't think of any harm. Just because the .NET model doesn't enforce relationships between namespaces, projects, and directory structure doesn't mean you can't have such relationships if you want to.

I'd be a little leery of breaking up the code into more projects than you need, as this can slow compilation and add a little bit of overhead when you have to manage multiple assemblies.

EDIT: Note that this question is nearly a duplicate of should the folders in a solution match the namespace?

Blair Conrad
Sorry about that, but I promise I did search before I asked, just apparently not for the right thing.
jschoen
+2  A: 

You can add folders to your solution for each namespace. While it'll still compile to a single executable, it organizes your source files and gives (what I think is) the desired effect?

I typically add a folder for each namespace in my project, and nest them according to the same hierarchy (MyApp.View.Dialogs for example)

AlexCuse
+3  A: 

A VS solution normally contains one or more projects. Thse projects have default namespaces (usually the namespace is just the name of the project). Normally, if you add a folder within the project, all the classes in it will be named as follows:

DefaultNamespace.FolderName.ClassName

Of course, you can change the default namespace of the project, and have your classes be named in whatever manner you wish.

As far as when/how to break stuff into projects, that's a matter of experience and/or preference. However, you should absolutely break stuff into projects within a solution, in order to keep your project organized. If managing too many assemblies becomes cumbersome (as Blair suggested), you can always ILMerge your assemblies into a single assembly. What's great about ILMerge is that even though you end up with just one assembly, all your classes keep their original fully qualified names.

It's also important to remember that a VS solution has no bearing on code - ie. they do not get built. VS Solutions are nothing but a way to group projects; it's the projects that are built and turned into DLLs.

Finally, VS let's you add "virtual" folders anywhere in the solution. These folders do not map to a folder in the filesystem, and are just used as another mean to help you organize your projects and other artifacts within the solution.

Esteban Araya
+6  A: 

Yep, in .NET namespace doesn't depend on file system or anything else. It's a great advantage in my opinion. For example you can split your code across different assemblies which allows flexible distribution.

When working in Visual Studio, IDE tends to introduce new namespace when you add new folder to project tree.

Here is a useful link from MSDN:

Namespace Naming Guidelines

The general rule for naming namespaces is to use the company name followed by the technology name and optionally the feature and design as follows.
CompanyName.TechnologyName[.Feature][.Design]

Of course you can use namespaces in the way you find more suitable. However if you going to share your code, I would recommend to go along with accepted standards.

EDIT:

I highly recommend to any .net developer to get a copy of Framework design guidelines This book will help you to understand how and why .NET is designed.

aku
What does the CompnayName.Technology buy you? I don't see any benefit into naming things this way. Any insights?
Esteban Araya
Did you read MSDN article? In fact I had situation when 2 3rd party assembly used the same root namespace.
aku
I still don't see how that solves the problem. What if both third parties are called ACME? What if they both use the same technology?
Esteban Araya
Also, I did read the article, I'm just asking for YOUR opinion.
Esteban Araya
In my opinion it's a matter of probability. It's least probable that there will be several ACMEs, but FooBars are all around the place. I find Microsoft scheme to be very reasonable. I'm using *techonolgies* from *vendors* not an abstract Suber.Duper.Utility classes.
aku
+2  A: 

Namespaces are purely semantic. Whilst they usually do reflect a folder structure, at least when using Visual Studio IDE, they do not need to.

You can have the same namespace referenced in multiple libraries, ugly but true.

johnc
+3  A: 

Indeed, the .Net environment allows you to throw your code at the IDE/filesystem like spaghetti against a wall.

That doesn't mean that mean this approach is sane, however. Its generally a good idea to stick with the project.foldername.Class approach that was mentioned earlier. Its also a really good idea to keep all of the classes from one namespace into the same class.

In Java, you can do screwy things like this as well getting all of the "flexibility" that you want, but the tools tend to strongly discourage it. Honestly, one of the most confusing things for me in being introduced to the .Net world was just how sloppy/inconsistent this can be thanks to the relatively poor guidance. Its easy to organize things sanely with a little thought, though. :)

jsight
+1  A: 

I've always considered source file organization and assigning identifiers to classes and objects to be two separate problems. I tend to keep related classes in groups, but not every group should be a namespace. Namespaces exist (more or less) to solve the problem of name conflicts—in flat-namespace languages like C, you can't walk two feet without tripping over identifiers like mycompany_getcurrentdate or MYCGetCurrentDate, because the risk of a conflict with another function in a third-party (or system) library is that much smaller. If you created a package or namespace for every logical separation, you would get (Java example) class names like java.lang.primitivewrapper.numeric.Integer, which is pretty much overkill.

John Calsbeek
+3  A: 

Namespaces are a logical grouping, while projects are a physical grouping.

Why is this important? Think about .NET 2.0, 3.0, and 3.5. .NET 3.0 is basically .NET 2.0 with some extra assemblies, and 3.5 adds a few more assemblies. So for instance, .NET 3.5 adds the DataPager control, which is a web control and should be grouped in System.Web.UI.WebControls. If namespaces and physical locations were identical, it couldn't be because it's in a different assembly.

So having namespaces as independent logical entities means you can have members of several different assemblies which are all logically grouped together because they're meant to be used in conjunction with each other.

(Also, there's nothing wrong with having your physical and logical layouts pretty similar.)

Kyralessa
Thanks, that actually makes a lot of since.
jschoen
+1  A: 

the difference is that .net namespaces have nothing much to do with java packages.

.net namespaces are purely for managing declarative scope, and have nothing to do with files, projects or their locations.

it's very simple everything declared in a particular namespace is accessible when you include a 'using' to that namespace.

very easy.

the choice of name and whether or not/how many '.' seperators you use is entirely up to you.

VS defaults to adding .foldernames to your namespaces just to try and be helpful.

this article explains namespaces quite well: http://www.blackwasp.co.uk/Namespaces.aspx

It also has an example naming convention toward the end, although your name convention is your call! ;)

that said, most places i've worked at and people i've worked with start with company name which is sensible, as it makes typenames for that company distinct, (separate from other, libraries, vendors, opensource projcts etc.)

Jerome