tags:

views:

95

answers:

4

I come from a Java background, and I see a lot of people say namespaces = packages, but looking around at available code it doesn't seem to me that people use namespaces like they used packages.

Currently I'm working on a DLL to manage all my data access to a database to be shared between two Windows Applications. So far I have been creating packages like I would for Java so I have a Domain, Services, DAO (I call it Repositories) sub namespaces off my top level. Is this correct? Has anyone written a best practice for namespaces? I assume this is probably a very minor point, but I don't want to go off going against the grain so to speak.

A: 

Have a look at this one http://msdn.microsoft.com/en-us/library/ms973231.aspx

Raymund
This article manly covers what a namespace is, and how to use them. What I'm looking for is how should namespaces be used in the context above. Also this article is for VB.NET
Patrick
A: 

One common usage is CompanyName.System.Module e.g. LogicStudio.ERP.GeneralLedger

Edgar Sánchez
I am currently doing this already. So I have Company.Product.Module but my question is about what to do after that
Patrick
+2  A: 

Slightly subjective, there is no "definitive, correct" answer to this.

Here's how i do it, with the intention that assemblies can be shared amongst projects.

Consider i have a company name called FooBar (+1 for originality anyone? :)

All your assemblies start from this root namespace. We have many shared assemblies.

Things like:

  • MVC (UI) HTML Helpers. These go in one assembly.
  • Generic Repository. Repository pattern implemented with generics, for re-use.
  • LINQ Extension methods (paging, general syntactic sugar factory). Again, these go in one assembly.

So, our FooBar Namespace Universe might look like this:

FooBar
|
 ---- FooBar.Common.Mvc
|
 ---- FooBar.Common.DataAccess
|
 ---- FooBar.Common.Linq
|
 ---- FooBar.ProjectOne (ASP.NET MVC Web Application)
|     |
|      --- FooBar.ProjectOne.Repository (makes use of FooBar.Common.DataAccess)
|     |
|      --- FooBar.ProjectOne.WebMvc (makes use of FooBar.Common.Mvc)
|
 ---- FooBar.ProjectTwo (WPF Application)
      |
       --- FooBar.ProjectTwo.Repository (makes use of FooBar.Common.DataAccess)
      |
       --- FooBar.ProjectTwo.BindingServices (makes use of FooBar.Common.Linq)

Know what i mean?

Setup your namespaces in a way that it 'feels right' putting common logic into common areas, based on the heterogeneous namespace.

You'll find a lot of companies with multiple shared projects follow this trend.

Your thinking of 'sub-namespaces' is correct (in my opinion).

RPM1984
Agreed. Do what feel right. But don't be afraid to refactor your namespaces as you adapt and change your language. While common practice has been do separate layers into assemblies, it's probably better to keep closely tied things together to reduce connections. Eric Evans covers this (generically, and mostly with Java) in Domain Drive Design (http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215). I think the same lessons apply to .NET.
jwadsack
@jwadsack - it's a double edged sword. Some things should be tied together. Other's not. Repository/DAL for example. Should be de-coupled. Im a big fan of Dependency Injection, that's why i like abstracting as much as i can away.
RPM1984
Great post. Thanks for the information!
Patrick
+1  A: 

The best guideline is probably the Namespace Naming Guidelines within the Design Guidelines for Developing Class Libraries on MSDN.

It basically says that you should use:

<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]

Which would translate as:

FooCompany.BarProduct.Baz
Reed Copsey