views:

663

answers:

5

I'm creating a library for use with an application that I am building. I am building a name space structure similar to below.

MyNamespace.Validation
MyNamespace.Reports
MyNamespace.Transactions
MyNamespace.DataImport
etc...

Would it be best practice to create a solution with multiple projects for each sub namespace or one project with multiple class files for each sub namespace? Thanks.

+2  A: 

I would go for the one solution with multiple projects.

Advantages:
- Each project can be a separate dll
- All projects in one solution for easy navigating between files

Mvt
+1  A: 

I have usually followed the pattern with one assembly is one namespace and the DLL name is in the namespace. Easier to find what DLLs to reference

Arve
+2  A: 

I would say it depends.

  • First, it's best practice to put each class in its own file.
  • If you go with one project, I would create folders for each namespace inside that project, and put the code files in the appropriate folder.
  • Doing the above, Visual Studio will automatically create new class files within the correct namespace

I think the real question here is this though:

If this is only ever going to be used once, putting everything in one project would make sense. However, if this code is going to be reusable, you should think if you would ever reuse just a part (or one sub-namespace) of this library. If the answer is yes, I would break apart the namespaces into separate projects, so in the future, you could only include the projects you needed.

TJMonk15
+3  A: 

Deciding exactly how to break up your solution is subjective - and it really depends on the specifics of your code.

However, one thing is certain: maintaining multiple assemblies has drawbacks! This article is particularly good at describing those drawbacks, observing how they add costs at development time, compile time, deployment time, and runtime.

I use as few assemblies as possible, aiming for a single assembly while isolating volatile areas of the domain. When multiple assemblies are clearly appropriate or required (and they often are, particularly to enforce decoupling), I do my best to group interfaces that will change at the same time into the same assemblies.

Jeff Sternal
+3  A: 

There are pros and cons to both approaches, which you need to personally decide between for your own circumstance.

Pro to multiple projects:

  • Separate assemblies allow the compiler to provide more strict guidance, potentially preventing coupling from creeping through. This allows you to maintain the dependencies better.
  • Separate assemblies can be loaded as needed in other projects, potentially easing reuse.
  • Separate assemblies can prevent unnecessary code from being loaded into a process, since they're loaded on demand.

Cons to multiple projects:

  • More complex deployment, as more files need deployment (minor)
  • Slower build/compile, and even potentially load times from loading multiple assemblies (minor)

Personally, I think the pros far outweigh the cons in most cases. I typically will split my namespaces into separate assemblies, provided they are not related. In your case, you're working on 4 very different concepts, so my gut feeling is that splitting makes the most sense.

Reed Copsey
+1 - though I lean the other way, that first 'Pro' is a big deal. You don't want to combine so many classes that you destroy the significance of the `internal` visibility modifier!
Jeff Sternal
Yes. That first Pro (and the third) is the main reason I do this typically.
Reed Copsey