Class organization

 

Currently, all the classes in the “Engine” project are in the same location – the top level of the project.

I want to group the different types of classes (models, utilities, factories, viewmodels, etc.) into folders – which are also the namespaces, by default in Visual Studio.

This is the current class organization.

Engine project - before

 

My typical way to organize simple projects is to have a folder/namespace for Models, ViewModels, Utilities, CustomEventArgs, and Factories. So, I added these folders to the project and moved the classes where they belong.

I don’t have ViewModels yet, but I’ve added a folder for them, because I am 100% sure I will be creating a ViewModel as part of this refactoring.

 

Correcting the namespaces

Originally, all the classes are defined as being in the “Engine” namespace. Since I’ve moved them into folders, I should change their namespaces to include the folder names.

Because I have ReSharper, I can right-click on the Engine project (in Solution Explorer) and select Refactor -> Adjust Namespaces… This will change all the classes’ namespaces to include the folder name.

ReSharper - Refactor -> Adjust Namespaces

 

If you do this manually, you need to also manually update the “using” lines in the classes that use the Engine classes. This includes classes in other projects that use any classes from the Engine project – such as the UI classes. ReSharper automatically updates the “using” statements everywhere in the solution.

 

Test the refactoring change

After each refactoring, rebuild the solution, run the unit tests (if there are any), and run the program (especially if you don’t have any unit tests).

Then, check this set of successful refactoring changes into your version control repository.

Leave a Reply

Your email address will not be published. Required fields are marked *