CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
AsynchronousBusinessRuleInheritingFromBusinessRuleAnalyzer.cs
Go to the documentation of this file.
2using Microsoft.CodeAnalysis;
3using Microsoft.CodeAnalysis.CSharp;
4using Microsoft.CodeAnalysis.CSharp.Syntax;
5using Microsoft.CodeAnalysis.Diagnostics;
6using System.Collections.Immutable;
7
8namespace Csla.Analyzers
9{
10 [DiagnosticAnalyzer(LanguageNames.CSharp)]
12 : DiagnosticAnalyzer
13 {
14 private static readonly DiagnosticDescriptor inheritsFromBusinessRuleRule =
15 new DiagnosticDescriptor(
16 Constants.AnalyzerIdentifiers.AsynchronousBusinessRuleInheritance,
17 AsynchronousBusinessRuleInheritingFromBusinessRuleAnalyzerConstants.Title,
18 AsynchronousBusinessRuleInheritingFromBusinessRuleAnalyzerConstants.Message,
19 Constants.Categories.Usage, DiagnosticSeverity.Error, true,
20 helpLinkUri: HelpUrlBuilder.Build(
21 Constants.AnalyzerIdentifiers.AsynchronousBusinessRuleInheritance, nameof(AsynchronousBusinessRuleInheritingFromBusinessRuleAnalyzer)));
22
23 public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
24 ImmutableArray.Create(inheritsFromBusinessRuleRule);
25
26 public override void Initialize(AnalysisContext context)
27 {
28 context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
29 context.EnableConcurrentExecution();
30 context.RegisterSyntaxNodeAction(AnalyzeMethodDeclaration, SyntaxKind.MethodDeclaration);
31 }
32
33 private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
34 {
35 var methodNode = (MethodDeclarationSyntax)context.Node;
36
37 if (!methodNode.ContainsDiagnostics)
38 {
39 var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
40 var typeSymbol = methodSymbol.ContainingType;
41
42 if(typeSymbol.IsBusinessRule() && methodSymbol.Name == "Execute" && methodSymbol.IsAsync)
43 {
44 context.ReportDiagnostic(Diagnostic.Create(
45 inheritsFromBusinessRuleRule, methodSymbol.Locations[0]));
46 }
47 }
48 }
49 }
50}