Namespaces and folder structures in c# solutions: how should folders on disk be organised?

前端 未结 8 742
野趣味
野趣味 2021-02-04 06:26

First off, let’s agree that namespace should match folder structure and that each language artefact should be in its own file.

(see Should the folders in a solution ma

8条回答
  •  孤独总比滥情好
    2021-02-04 06:31

    12 Years on, is there any definitive or best practice answer on this question?

    I would like to use the Nested folders structure, however given that you:

    "agree that namespace should match folder structure and that each language artefact should be in its own file."

    How would project A.B.C.csproj folder structure look if it extended code from other namespaces, for example:

    • grand-parent: A.csproj
    • parent: A.B.csproj
    • child A.B.C.D.csproj
    • external namespaces System.Linq or Microsoft.Extensions.Logging

    Perhaps something like:

    /A                                  // namespace: A
      /B                                // namespace: A.B
        /C                              // namespace: A.B.C
          A.B.C.csproj 
          ClassC.cs                  
          /_                            // External Namespace root folder (underscore with no text)
            /System                     // namespace: System 
              /Linq                     // namespace: System.Linq
                IQueryableExtensions.cs 
            /Microsoft                  // namespace: Microsoft
              /Extensions               // namespace: Microsoft.Extensions
                /Logging                // namespace: Microsoft.Extensions.Logging
                  ILoggerExtensions.cs                                  
          /__A                          // namespace: A (Grand Parent folder (2 underscores for 2 levels up)
            ClassAExtensions.cs
            /B                          // namespace: A.B (Nested Parent folder)
              ClassBExtensionsNested.cs
                                        //Parent Namespace folder (1 underscore for 1 level up)
          /_B                           // namespace: A.B
            ClassBExtensions.cs
          /D                            // namespace: A.B.C.D (Child folder)
            ClassDExtensions.cs
    

    Above is trying to demonstrate:

    • A folder with a lone Underscore (_) with no text following would substitute as an empty namespace & new root for all external namespaces.
    • Folders with Underscore(s) (__A) & (_B) followed by text substitutes as a level up in the tree/hierarchy of the current project. 1 level per underscore

提交回复
热议问题