0

I have a .Net project in which some classes (e.g. constants, enums, etc.) are generated by a tool developed in the company. Developers would not participate in changing them. In addition the team using this tool may make mistakes due to the large size of the project.

Is there any way I can enforce some rules like folder structure, naming, proper namespaces, and such things upon inserting those files in the solution? Or is there a way to test these factors?

burnsi
  • 6,194
  • 13
  • 17
  • 27
Reza Shafie
  • 81
  • 1
  • 7
  • 1
    Sure, you can test automatically generated code using StyleCop/Roslyn rules as you would normal code. – PMF Jul 17 '22 at 14:39
  • @PMF Thank you for your guidance. But as I know StyleCop doesn't let me validate folder structure and namespace in accordance with the structure. How do I solve that issue? – Reza Shafie Jul 17 '22 at 15:28
  • Yea, true. That is not checked by default. However, you can write your own custom StyleCop rules. There's an API for that. – PMF Jul 17 '22 at 16:59
  • Resharper has a "Namespace does not correspond to file location" code inspection that I believe can be automated. – Jonathan Dodds Jul 17 '22 at 18:25

2 Answers2

1

To enforce a folder structure, you could add custom logic in MSBuild. The logic in MSBuild would run as part of a build. If you know that certain folders must exist as part of a project and/or that certain files must be in certain folders, you can add verification steps in MSBuild and either issue a warning or stop the build with an error.

To enforce name and namespace rules/conventions you can use a static code analyzer. You can use the Microsoft Code Analyzer and/or a third party analyzer. If the 'rules' you need are not available out of box, you can write custom rules.

Both the MSBuild and code analyzer can be used with and without the Visual Studio IDE and can be used locally and in automated builds.

Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14
  • I could not find any proper tutorial for implementing what I have in mind for MSBuild. Could you please introduce some clue to begin with? – Reza Shafie Jul 20 '22 at 13:49
  • Can you a share some details of what you have in mind for MSBuild for enforcing a folder structure? – Jonathan Dodds Jul 20 '22 at 19:51
  • I want to ensure files containing a certain word should only exist in one folder e.g. files containing 'constant' should only be in the 'constants' folder not anywhere else. Additionally, I want to ensure the naming of the files and folders are the ones upon which we agreed. – Reza Shafie Jul 24 '22 at 13:22
0

What I seek is achievable with ArchUnitNet. It can be reached here It helps with testing the folder structure of the project as well as namespace testing and relative naming and even correct inheritance if I'm not mistaken.

Reza Shafie
  • 81
  • 1
  • 7