A set of Roslyn Analyzer aimed to enforce some design good practices and code quality (QA) rules.
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.
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. | ⚠ | ✅ | ❌ |
- Category: Design
- Severity: Warning ⚠
- Description: WebApi controller methods should not contain data-manipulation business logics through a LINQ query expression.
- Motivation and fix: A WebApi controller method is using Entity Framework DbContext to directly manipulate data through a LINQ query expression. WebApi controllers should not contain data-manipulation business logics. Move the data-manipulation business logics into a more appropriate class, or even better, an injected service.
- Knowledge base: this is a typical violation of the "Single Responsibility" rule of the "SOLID" principles. In order to fix the problem, the code could be modified in order to rely on the "Indirection pattern" and maximize the "Low coupling evalutative pattern" of the "GRASP" principles.
- See also: DSA002
public class MyEntitiesController : ControllerBase
{
protected MyDbContext DbContext { get; }
public MyEntitiesController(MyDbContext dbContext)
{
DbContext = dbContext;
}
[HttpGet]
public IEnumerable<MyEntity> GetAll0()
{
// this WILL trigger an error
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 an error
var query = DbContext.MyEntities.Where(entities => entities.Id > 0).Select(entities=>entities.Id);
return query.ToList();
}
}
- Category: Design
- Severity: Warning ⚠
- Description: WebApi controller methods should not contain data-manipulation business logics through a LINQ fluent query.
- Motivation and fix: A WebApi controller method is using Entity Framework DbSet to directly manipulate data through a LINQ fluent query. WebApi controllers should not contain data-manipulation business logics. Move the data-manipulation business logics into a more appropriate class, or even better, an injected service.
- Knowledge base: this is a typical violation of the "Single Responsibility" rule of the "SOLID" principles. In order to fix the problem, the code could be modified in order to rely on the "Indirection pattern" and maximize the "Low coupling evalutative pattern" of the "GRASP" principles.
- See also: DSA001
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 an error
var query = from entities in DbContext.MyEntities where entities.Id > 0 select entities;
return query.ToList();
}
[HttpPost]
public IEnumerable<long> GetAll1()
{
// this WILL trigger an error
var query = DbContext.MyEntities.Where(entities => entities.Id > 0).Select(entities=>entities.Id);
return query.ToList();
}
}
- NuGet package (recommended) → https://www.nuget.org/packages/DogmaSolutions.Analyzers