DogmaSolutions.Analyzers 1.0.48

There is a newer version of this package available.
See the version list below for details.
dotnet add package DogmaSolutions.Analyzers --version 1.0.48                
NuGet\Install-Package DogmaSolutions.Analyzers -Version 1.0.48                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DogmaSolutions.Analyzers" Version="1.0.48">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DogmaSolutions.Analyzers --version 1.0.48                
#r "nuget: DogmaSolutions.Analyzers, 1.0.48"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DogmaSolutions.Analyzers as a Cake Addin
#addin nuget:?package=DogmaSolutions.Analyzers&version=1.0.48

// Install DogmaSolutions.Analyzers as a Cake Tool
#tool nuget:?package=DogmaSolutions.Analyzers&version=1.0.48                

Dogma Solutions Roslyn Analyzers

DogmaSolutions.Analyzers on NuGet

A set of Roslyn Analyzer aimed to enforce some design good practices and code quality (QA) rules.

Rules structure

This section describes the rules included into this package.

Every rule is accompanied by the following information and clues:

  • Category → identify the area of interest of the rule, and can have one of the following values: Design / Naming / Style / Usage / Performance / Security
  • Severity → state the default severity level of the rule. The severity level can be changed by editing the .editorconfig file used by the project/solution. Possible values are enumerated by the DiagnosticSeverity enum
  • Description → a short description about the rule aim.
  • Motivation and fix → a detailed explanation of the detected issue, and a brief description on how to change your code in order to solve it.
  • See also → a list of similar/related rules.

Rules list

Id Category Description Severity Is enabled Code fix
DSA001 Design WebApi controller methods should not contain data-manipulation business logics through a LINQ query expression.
DSA002 Design WebApi controller methods should not contain data-manipulation business logics through a LINQ fluent query.
DSA003 Code Smells Don't use string.IsNullOrEmpty. Use string.IsNullOrWhiteSpace instead.<br/>Usually, business logics distinguish between "string with meaningful content", and "string NULL or without meaningfull content". Thus, statistically speaking, almost every call to string.IsNullOrEmpty could or should be replaced by a call to string.IsNullOrWhiteSpace, because in the large majority of cases, a string composed by only spaces, tabs, and return chars is not considered valid because it doesn't have "meaningfull content". In most cases, string.IsNullOrEmpty is used by mistake, or has been written when string.IsNullOrWhiteSpace was not available.
DSA004 Code Smells Don't use DateTime.Now. Use DateTime.UtcNow instead.<br/>Using DateTime.Now into business logics potentially leads to many different problems, like:<br/> - Incoherence between nodes or processes running in different timezones (even in the same country, i.e. USA, Soviet Union, China, etc)<br/> - Unexpected behaviours in-between legal time changes<br/> - Code conversion problems and loss of timezone info when saving/loading data to/from a datastore

DSA001 - Don't use Entity Framework to launch LINQ queries in a WebApi controller

Code sample

public class MyEntitiesController : ControllerBase
{
 protected MyDbContext DbContext { get; }

 public MyEntitiesController(MyDbContext dbContext)
 {
     DbContext = dbContext;
 }

 [HttpGet]
 public IEnumerable<MyEntity> GetAll0()
 {
     // this WILL trigger the rule
     var query = from entities in DbContext.MyEntities where entities.Id > 0 select entities;
     return query.ToList(); 
 }

 [HttpPost]
 public IEnumerable<long> GetAll1()
 {
     // this WILL NOT trigger the rule
     var query = DbContext.MyEntities.Where(entities => entities.Id > 0).Select(entities=>entities.Id);
     return query.ToList(); 
 }
}

DSA002 - Don't use an Entity Framework DbSet to launch queries in a WebApi controller

Code sample

public class MyEntitiesController : Microsoft.AspNetCore.Mvc.ControllerBase
{
 protected MyDbContext DbContext { get; }

 public MyEntitiesController(MyDbContext dbContext)
 {
     this.DbContext = dbContext;
 }

 [HttpGet]
 public IEnumerable<MyEntity> GetAll0()
 {
     // this WILL NOT trigger the rule
     var query = from entities in DbContext.MyEntities where entities.Id > 0 select entities;
     return query.ToList(); 
 }

 [HttpPost]
 public IEnumerable<long> GetAll1()
 {
     // this WILL trigger the rule
     var query = DbContext.MyEntities.Where(entities => entities.Id > 0).Select(entities=>entities.Id);
     return query.ToList(); 
 }
}

DSA003 - Don't use String.IsNullOrEmpty. Use IsNullOrWhiteSpace instead

  • Category → Code smells
  • Severity → Warning ⚠
  • Description → Usually, business logics distinguish between "string with content", and "string NULL or without meaningfull content". Thus, statistically speaking, almost every call to string.IsNullOrEmpty could or should be replaced by a call to string.IsNullOrWhiteSpace, because in the large majority of cases, a string composed by only spaces, tabs, and return chars is not considered valid because it doesn't have "meaningfull content". In most cases, string.IsNullOrEmpty is used by mistake, or has been written when string.IsNullOrWhiteSpace was not available. Don't use string.IsNullOrEmpty. Use string.IsNullOrWhiteSpace instead.
  • Fix → Don't use string.IsNullOrEmpty. Use string.IsNullOrWhiteSpace instead.

Code sample

public class MyClass
{

 public bool IsOk(string s)
 {
     // this WILL NOT trigger the rule
     return string.IsNullOrWhiteSpace(s);
 }

 public bool IsNotOk(string s)
 {
     // this WILL trigger the rule
     return string.IsNullOrEmpty(s);
 }

}

  • DSA004 - Don't use DateTime.Now. Use DateTime.UtcNow instead

  • Category → Code smells

  • Severity → Warning ⚠

  • Description → Using DateTime.Now into business logics potentially leads to many different problems:

    • Incoherence between nodes or processes running in different timezones (even in the same country, i.e. USA, Soviet Union, China, etc)
    • Unexpected behaviours in-between legal time changes
    • Code conversion problems and loss of timezone info when saving/loading data to/from a datastore
  • See also
    Security-wise, this is correlated to the CWE category “7PK” (CWE-361)
    Cit: "This category represents one of the phyla in the Seven Pernicious Kingdoms vulnerability classification. It includes weaknesses related to the improper management of time and state in an environment that supports simultaneous or near-simultaneous computation by multiple systems, processes, or threads. According to the authors of the Seven Pernicious Kingdoms, "Distributed computation is about time and state. That is, in order for more than one component to communicate, state must be shared, and all that takes time. Most programmers anthropomorphize their work. They think about one thread of control carrying out the entire program in the same way they would if they had to do the job themselves. Modern computers, however, switch between tasks very quickly, and in multi-core, multi-CPU, or distributed systems, two events may take place at exactly the same time. Defects rush to fill the gap between the programmer's model of how a program executes and what happens in reality. These defects are related to unexpected interactions between threads, processes, time, and information. These interactions happen through shared state: semaphores, variables, the file system, and, basically, anything that can store information."

  • Fix → Don't use DateTime.Now. Use DateTime.UtcNow instead

Code sample

public class MyClass
{

 public bool IsOk(string s)
 {
     // this WILL NOT trigger the rule
     return string.IsNullOrWhiteSpace(s);
 }

 public bool IsNotOk(string s)
 {
     // this WILL trigger the rule
     return string.IsNullOrEmpty(s);
 }

}

Installation

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.57 71 1/21/2025
1.0.56 76 1/21/2025
1.0.55 74 1/20/2025
1.0.54 73 1/20/2025
1.0.53 73 1/20/2025
1.0.52 85 1/20/2025
1.0.51 76 1/19/2025
1.0.50 64 1/19/2025
1.0.49 62 1/19/2025
1.0.48 78 1/19/2025
1.0.40 523 3/14/2022
1.0.39 447 3/14/2022
1.0.38-rc 285 3/14/2022
1.0.37-rc 286 3/13/2022
1.0.35-rc 196 3/13/2022
1.0.34-rc 272 3/13/2022
1.0.33-rc 282 3/13/2022
1.0.32-rc 210 3/13/2022
1.0.31-rc 260 3/13/2022

Added rules: DSA003, DSA004