Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate projects or multiple class files ... namespace best practice in C#

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.

like image 618
Davin Studer Avatar asked Feb 09 '10 18:02

Davin Studer


People also ask

Can a namespace can hold more than one class?

Two classes with the same name can be created inside 2 different namespaces in a single program. Inside a namespace, no two classes can have the same name.

How many classes per file c#?

One class per file discussion. : r/csharp.

Which of the following is used to organize the class files in C#?

We use namespaces to organize classes into a logically related hierarchy. Namespaces function as both an internal system for organizing our application and an external way to avoid name collision between source code and application.

What is a namespace in c#?

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. C# Copy.


Video Answer


3 Answers

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.

like image 133
Reed Copsey Avatar answered Oct 28 '22 11:10

Reed Copsey


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.

like image 27
FallenAvatar Avatar answered Oct 28 '22 13:10

FallenAvatar


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

like image 39
Mvt Avatar answered Oct 28 '22 11:10

Mvt