Getting Started
- How to install?
- How to configure?
- How to add dependency config file to a C# project?
- How to define dependency rules?
- How to run the analysis?
- How to change the severity of the NsDepCop issues?
- How to turn NsDepCop on and off?
How to install?
There are two types of installers.- MSI package
- Installs MSBuild integration and/or Visual Studio integration.
- The Visual Studio integration feature is installed for all users.
- Requires admin privilege.
- Requires Visual Studio restart if it was running during install.
- VSIX package
- Installs only the Visual Studio integration feature and only for the current user.
- No admin privilege required.
- Also available from Visual Studio Gallery.
How to configure?
You can describe the namespace dependency rules in XML config files.- One config file per one C# project.
- The file must be called: config.nsdepcop
- The file must be next to the csproj file (that is, in the same folder).
- Edit the XML config file and describe your dependency rules.
How to add dependency config file to a C# project?
- In the Solution Explorer window right-click on a C# project.
- Choose Add / New Item... from the context menu.
- Choose NsDepCop Config File (in the "Visual C# Items" group).
Don't care about the file name. It will be config.nsdepcop regardless of what you type.
How to define dependency rules?
You can specify the allowed/disallowed namespace dependencies in the config.nsdepcop file.- You can specify any number of "Allowed" and "Disallowed" rules.
- Only those dependencies are allowed that has a matching "Allowed" rule and no match with any of the "Disallowed" rules.
- If both an "Allowed" and a "Disallowed" rule is matched then "Disallowed" is the "stronger".
<NsDepCopConfig IsEnabled="True" CodeIssueKind="Warning"> <Allowed From="*" To="System.*" /> <Allowed From="*" To="." /> </NsDepCopConfig>
Special symbols
. (a single dot) = the global namespace * (a single star) = any namespace MyNamespace.* = MyNamespace and any sub-namespaces
Examples
<Allowed From="MyNamespace" To="System" /> | MyNamespace can depend on System |
<Allowed From="MyNamespace" To="System.*" /> | MyNamespace can depend on System and any sub-namespace |
<Allowed From="MyNamespace" To="*" /> | MyNamespace can depend on any namespace |
<Allowed From="MyNamespace" To="." /> | MyNamespace can depend on the global namespace |
If a dependency is not defined as allowed then it's treated as disallowed. If you want to change this behavior and allow everything that is not explicitly disallowed then define an "allow all" rule and then the desired disallowed rules.
Example:
<NsDepCopConfig IsEnabled="True" CodeIssueKind="Warning"> <Allowed From="*" To="*" /> <Disallowed From="MyFrontEnd.*" To="MyDataAccess.*" /> </NsDepCopConfig>
How to run the analysis?
- With MSBuild integration
- Start a build. Issues will be reported in the build output.
- With Visual Studio integration
- As you type into the code editor Visual Studio will automatically parse the relevant part of the source file and invoke the analyzer for each relevant symbol.
- Double clicking an item in the Error List will take you to the source code that caused the problem.
- (1) Only if you have a config.nsdepcop file in your project ...
- (2) ... issues will be reported in the Error List ...
- (3) ... and underlined in the code editor.
How to change the severity of the NsDepCop issues?
In the config.nsdepcop file set the value of the CodeIssueKind attribute to one of the following values.- Error
- Warning (default)
- Info
How to turn NsDepCop on and off?
- If there's no "config.nsdepcop" file next to the project file (*.csproj) then NsDepCop is turned off for that project.
- If there is a "config.nsdepcop" file, you can still turn analysis on and off by setting the IsEnabled attribute in the config file to one of the following values.
- True (default, NsDepCop is turned on)
- False (NsDepCop is turned off)