Freddessson Freddessson - 1 year ago 135
C# Question

MVC - folders vs namespaces structure (C#)

What is best practice when using MVC pattern in C#? Am I supposed to use folders or new namespaces? When using namespaces, I have to add references, which namespace who can talk to another… and that is good!

But when using namespaces and releasing my program and checking the .exe file, it creates three (Model, View, Controller) .exe files which have to be in the same folder to be able to run.

When using folders, they are not dependent on each other which means that I can move the "main" .exe to a folder instead of moving all three exe files and it works.

I’d say that splitting namespaces is better because you can set up which classes that can talk to each other’s using References. But I don’t like that I have to have three exe files in the same folder to be able to run it (namespace-style).

So, what is best practice for this + pros and cons?

See screengrabs to see the difference in Visual Studio 2015:
Different namespaces
Folder structure


Answer Source

Answer is - it depends. It depends on what is easier to understand for you or, in case you are working with someone else, what is easier to understand for all programmers working on this code.

In C# you can do absolutely everything combining folder structure and namespaces. For example you could have class Car in SuperModels\ExtraModels\Models\Car.cs and this class could be in namespace MyProject.Controllers. It would be super weird and probably everyone who would like to change your code would say nasty things about you in their minds (or even out loud!), but it would work.

I'm using MVC pattern in ASP.NET and there the default project structure is like on your second screenshot, so I (and probably everyone who used ASP.NET at least once) feel more comfortable with that. However I saw projects where there was separate class library project only for models and was referenced in second project with controllers and views. Every approach which you are okay with, is good approach - there is no such thing as correct project structure.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download