Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to do a bulk namespace rename on a large c# application?

First, a little background.

Currently namespaces and assemblies in our codebase (~60 assemblies, thousands of classes) looks like

WidgetCompany.Department.Something 

We have now been spun off such that we are selling the software that drives a WidgetCompany, so we 'd like to rename the namespaces & assemblies

NewCompany.Something 

Under normal circumstances I'd probably just stick with the old namespace, but the problem is our customers don't want to see the name of one of their competitors anywhere in the application. In the off chance that they see a stack trace, assembly properties etc, it shouldn't show up. It's not meant to hide our associates or do anything sinister, we just need to make sure that people know we are a separate entity, separate management, and they don't need to worry about data being shared etc.

Now the question. What is the best way to perform this type of all encompassing rename? The following would need to change:

  • Namespace for (almost) every class in the application
  • Every using statement in the application which references the old names
  • Folder structure for each project
  • References between projects which rely on changed folder structure
  • .Sln files which reference the changed folder structure
  • Any references to those classes which are fully qualified (should be few and far between)
  • Any references to those classes in xml config files (config sections etc)
  • AssemblyInfo.cs files for every assembly
  • AssemblyName in every .csproj file

Am I stuck with the find-replace-pray strategy or is there something better?

like image 689
Brook Avatar asked Jan 26 '11 19:01

Brook


People also ask

Can we rename namespace?

You can't, and that's probably a good thing. If there are any dependencies on a namespace (e.g. someone tries to call a service in the namespace via service. namespace.

How do I refactor namespace in Visual Studio?

Place your cursor in the namespace name. Press Ctrl+. to trigger the Quick Actions and Refactorings menu.

How do I rename a namespace in Visual Studio?

Just right click on the name you want to change (this could be namespace or whatever else) and select Refactor->Rename... Enter new name, leave location as [Global Namespace], check preview if you want and you're done!


1 Answers

Right click on your current namespace and select Refactor -> Rename and change the name in the pop up that comes up after a while. Enter your new name and click ok.

If you have multiple depths to your namespace, then Visual Studio won't let you type a dot. However, if you copy and paste a dot, despite a warning, it will do the business.

To completely change to the new name, you will likely need to make additional changes manually. You can find where by performing a project search (ctrl+shift+f) for other references to the name in the Project, Solution, or other supporting files from a separate text editor like VS Code. Finally, folders may need to be changed manually as well.

like image 170
franklins Avatar answered Sep 24 '22 14:09

franklins