Usually a occupation workforce of builders may have guidelines for coding together with code opinions to make sure code is writen to the foundations and pointers of the workforce.
Even with code opinions guidelines can nonetheless be damaged which brings us to a Roslyn Analyzer Microsoft.CodeAnalysis.BannedApiAnalyzers MCABAA NuGet package deal which gives a solution to carry out a pre-check on a code evaluate and/or whereas writing code.
The best way to use MCABAA
- Add the package to a mission
- Create a textual content file named BannedSymbols.txt within the root folder of the mission.
Examine the mission file by double clicking the mission file in Resolution Explorer and observe how the banned file was positioned in.
<ItemGroup>
<None Take away="BannedSymbols.txt" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Embody="BannedSymbols.txt" />
</ItemGroup>
Including guidelines
Instance 1
Suppose for working with SQL-Server database with a knowledge supplier the package deal Microsoft.Information.SqlClient shouldn’t be used however as a substitute Entity Framework Core. Add the next rule (as the primary name is often creating an occasion of SqlConnection.
T:Microsoft.Information.SqlClient.SqlConnection; Use EF Core as a substitute
One other rule is required within the occasion a developer makes an attempt utilizing System.Information.SqlClient
T:System.Information.SqlClient.SqlConnection; Use EF Core as a substitute
On the identical line, stop a neighborhood technique from getting used.
M:Library1.Lessons.DataOperations.ReadData; use EF Core as a substitute
Instance 2
Disallow utilizing Newtonsoft.Json.JsonConvert, use System.Textual content.Json.JsonSerializer as a substitute.
T:Newtonsoft.Json.JsonConvert;Use System.Textual content.Json.JsonSerializer as a substitute
Instance 3
Disallow System.DateTime.Now in favor of Use System.DateTime.UtcNow
P:System.DateTime.Now;Use System.DateTime.UtcNow as a substitute
Instance 4
The workforce has two variations of a category, on this case Individual in two separate class initiatives.
Library1 class mission Individual class
public class Individual
{
public int Id { get; set; }
public string Kind { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? BirthDate { get; set; }
}
Library2 class mission Individual class
public class Individual
{
public int Id { get; set; }
public string Kind { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateOnly? BirthDate { get; set; }
}
Library1 is for pre-existing mission so it want to remain however for brand spanking new initiatives, use Library2. We add the next.
T:Library1.Fashions.Individual;Use Library2.Fashions.Individual which makes use of DateOnly somewhat than DateTime.
Discovering damaged guidelines.
Construct the mission(s) and verify the Error Record in Visible Studio.
What the code seems like with damaged guidelines
Guidelines cheat sheet
See the next page
Caveats
- The file BannedSymbols.txt could be positioned in a folder exterior the mission however might not all the time work, its hit or miss.
- Guidelines cannot be utilized to language extension strategies.
Supply code
Reasonably than taking time to setup a mission, clone the next GitHub repository.