Inconsistent namespaces
(Solved) I'm currently working on a solution with a lot of subfolders and I noticed that the namespaces of .cs files are not reflecting the folder structure.
On top of that, in some projects, i.e. the files inside "Commands" and "Queries" have the same namespace and in other projects they have wrong namespace like ending in "Messages" while the folder name is Queries.
I would be able to fix this automatically in Rider by naming all namespaces according to folder structure but I'm afraid I will end up having hundreds of 10 levels long namespaces and usings.
Other solution is to go in every file and understand the pattern that was used in naming the namespaces and correct the wrong ones (this will take a lot of time).
30 Replies
I see no question attached to this post... but I'm guessing you want ideas on how to solve this.
Yes 🙂
Unfortunately, you've already nailed it - its either via a tool, or manual.
They both have ups and downs.
Perhaps refactor the folder structure before doing the rider fix?
That's a nice idea.
Hm? There's a code cleanup thing for this
editorconfig
Whichever
I can do it via editconfig and apply to the whole solution and name the namespaces as folder structure.
Do you have in mind another editorconfig rule for this?
If you have set such a rule, perhaps running
dotnet format
will apply it?that is going well, I tried it already, but it creates long namespaces as there are 10 level subfolders.
Maybe this is better than having inconsistency
Two different projects i.e.
Company.App.Access.User
and Company.App.Access.Implementation
have the same namespace Company.App.Access
for files in the root folder but different for the ones in subfolders.
that's why I want to fix the namespaces, is a total disaster and I'm pretty fixed on consistency :))Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
thanks
Do you recommend having the namespaces the same as folder structure?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I want the same. I feel better now :))
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Thank you all, I will be starting now to organize them all.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
😄
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
893 .cs files
that's why I like rules, so that in the future I don't have to figure it out. I took the project from someone else and I have to continue development.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Thank you. That is very very useful.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
In my previous project the TL said not to go too deep :)) now I understand why
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Do you recommend ERP in project name or Erp?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
ok
Microsoft.AspNetCore.Hosting.csproj
Even though it's ASP they go with Asp
I've seen now that instead of API they also use Api. I go with the convention
But your company is free to create their own naming conventions 🤷♂️
I created it now :))