简体   繁体   English

使用StyleCop的C#中的命名空间问题

[英]Namespace Problems in C# with StyleCop

I'm fairly new to C# and I'm trying to structure my simple web API project. 我对C#相当陌生,并且正在尝试构建我的简单Web API项目。 One of the things I want to do is group all of my stuff under the namespace [Company].[Project] , and then further slice that into individual .[Feature] slices. 我想做的一件事情是将我所有的东西归类在[Company].[Project]命名空间下,然后进一步将其切成单独的.[Feature]片。 For example, I'd like to do MyCompany.MyProject.MusicHandling for a part of it that handles serialization, deserialization, and talking to the music part of the service. 例如,我想为MyCompany.MyProject.MusicHandling做一部分处理序列化,反序列化以及与服务的音乐部分通信的事情。

This seems natural: 这看起来很自然:

namespace MyCompany.MyProject {
  namespace MusicHandling {
       // Classes
  }
}

For every class file I want in this namespace. 对于我要在此命名空间中使用的每个类文件。 However, when I do this StyleCop complains about SA1403: AC# Document Can Only Contain a Single Namespace . 但是,当我这样做时,StyleCop会抱怨SA1403: AC# Document Can Only Contain a Single Namespace

Okay, I think, no problem: 好吧,我认为,没问题:

namespace MyCompany.MyProject.MusicHandling {
  // classe
}

Now it's complaining again! 现在又在抱怨! This time it's Namespace does not correspond to file location, should be 'MyCompany.MyProject' . 这次它的Namespace does not correspond to file location, should be 'MyCompany.MyProject' SIGH. 叹。

I try to follow a linter with a new language and set it extra strict so I learn best practices as I code. 我尝试用一​​种新语言跟随短毛猫,并将其设置得更加严格,以便在编写代码时学习最佳实践。 How should I structure these submodules of code so it makes logical sense for the user of my library? 我应该如何构造这些代码子模块,以使其对我的库用户具有逻辑意义?

The new complaint is that Style Cop expects that files in a namespace are in a folder structure matching the namespace. 新的抱怨是,Style Cop期望名称空间中的文件位于与名称空间匹配的文件夹结构中。 Basically it takes your project's root namespace and assumes all files in that namespace are at the root of the project. 基本上,它使用项目的根名称空间,并假定该名称空间中的所有文件都位于项目的根目录。 Any namespaces under the root should have a matching folder in which files that contains classes in that namespace live. 根目录下的任何名称空间都应有一个匹配的文件夹,其中包含该名称空间中的类的文件将存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM