Skip to content

Commit

Permalink
Introduce static code analysis in chapter two
Browse files Browse the repository at this point in the history
Introduce static code analysis in chapter two
  • Loading branch information
meaboutsoftware authored Sep 5, 2023
2 parents 1c8860e + 1d7f897 commit 79ebe05
Show file tree
Hide file tree
Showing 111 changed files with 710 additions and 572 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
= 11. Turn on static code analysis

Date: 2023-09-04

== Problem

We have been experiencing an increasing number of bugs and code quality issues in our project. Manual code reviews are time-consuming and error-prone, and we need a more systematic approach to catch these issues early in the development process.

== Decision

After careful consideration, we have decided to turn on static code analysis for all our software projects. We will leverage automated tools and integrate them into our continuous integration (CI) pipeline to analyze code for potential issues, adherence to coding standards, and security vulnerabilities.

== Consequences

- Static code analysis will help us catch code quality issues, bugs, and security vulnerabilities at an early stage of development, reducing the likelihood of these issues reaching production
- The tools will enforce coding standards and best practices across all projects, leading to more consistent and maintainable code
- Developers can focus more on writing code, as the analysis tools will automatically identify issues, reducing the need for manual code reviews
- Over time, the quality of our codebase is expected to improve, leading to fewer defects and maintenance challenges
- Identifying and addressing security vulnerabilities early in the development process will enhance the security of our software
- There may be a learning curve for some team members as they become accustomed to using static code analysis tools. We will provide training and resources to help them adapt
- Configuring and integrating static code analysis tools into our CI pipeline may require some initial effort, but this investment is expected to pay off in the long run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= 11: Applying Clean Architecture approach to the Contracts module
= 12. Applying Clean Architecture approach to the Contracts module

Date: 2023-04-15

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= 12: Applying layered approach to the Passes module
= 13. Applying layered approach to the Passes module

Date: 2023-05-24

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= 13: Use shared integration events
= 14. Use shared integration events

Date: 2023-05-24

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= 14: Use command query separation to contracts module
= 15. Use command query separation to contracts module

Date: 2023-05-28

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= 15: Leveraging Feature Flags to Isolate Integration Tests
= 16. Leveraging Feature Flags to Isolate Integration Tests

Date: 2023-06-20

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= 16: Selection of Microsoft's Feature Management Tool
= 17. Selection of Microsoft's Feature Management Tool

Date: 2023-06-20

Expand Down
137 changes: 137 additions & 0 deletions Chapter-2-modules-separation/Src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
###############################
# Core EditorConfig Options #
###############################
root = true
# All files
[*]
indent_style = space

# XML project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2

# XML config files
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
indent_size = 2

# Code files
[*.{cs,csx,vb,vbx}]
indent_size = 4
insert_final_newline = true
charset = utf-8-bom

dotnet_analyzer_diagnostic.severity = error
dotnet_diagnostic.CS1591.severity = none

###############################
# .NET Coding Conventions #
###############################
[*.{cs,vb}]
# Organize usings
dotnet_sort_system_directives_first = true
# this. preferences
dotnet_style_qualification_for_field = false
dotnet_style_qualification_for_property = false
dotnet_style_qualification_for_method = false
dotnet_style_qualification_for_event = false
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true
dotnet_style_predefined_type_for_member_access = true
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
dotnet_style_parentheses_in_other_operators = always_for_clarity
# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members
dotnet_style_readonly_field = true
# Expression-level preferences
dotnet_style_object_initializer = true
dotnet_style_collection_initializer = true
dotnet_style_explicit_tuple_names = true
dotnet_style_null_propagation = true
dotnet_style_coalesce_expression = true
dotnet_style_prefer_is_null_check_over_reference_equality_method = true
dotnet_style_prefer_inferred_tuple_names = true
dotnet_style_prefer_inferred_anonymous_type_member_names = true
dotnet_style_prefer_auto_properties = true
dotnet_style_prefer_conditional_expression_over_assignment = true
dotnet_style_prefer_conditional_expression_over_return = true
###############################
# Naming Conventions #
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = error
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const
###############################
# C# Coding Conventions #
###############################
[*.cs]
# var preferences
csharp_style_var_for_built_in_types = true
csharp_style_var_when_type_is_apparent = true
csharp_style_var_elsewhere = true
# Expression-bodied members
csharp_style_expression_bodied_methods = true
csharp_style_expression_bodied_constructors = true
csharp_style_expression_bodied_operators = true
csharp_style_expression_bodied_properties = true
csharp_style_expression_bodied_indexers = true
csharp_style_expression_bodied_accessors = true
# Pattern matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true
csharp_style_pattern_matching_over_as_with_null_check = true
# Null-checking preferences
csharp_style_throw_expression = true
csharp_style_conditional_delegate_call = true
# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async
# Expression-level preferences
csharp_prefer_braces = true
csharp_style_deconstructed_variable_declaration = true
csharp_prefer_simple_default_expression = true
csharp_style_prefer_local_over_anonymous_function = true
csharp_style_inlined_variable_declaration = true
# Other
csharp_style_unused_value_expression_statement_preference = discard_variable:none
csharp_style_namespace_declarations = file_scoped
csharp_using_directive_placement = inside_namespace
dotnet_style_namespace_match_folder = false
dotnet_code_quality.ca1711.allowed_suffixes = EventHandler
dotnet_code_quality.CA1716.analyzed_symbol_kinds = Event
###############################
# C# Formatting Rules #
###############################
# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left
# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
# Wrapping preferences
csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed class ExceptionMiddleware
private readonly RequestDelegate _next;

public ExceptionMiddleware(RequestDelegate next) => _next = next;

public async Task InvokeAsync(HttpContext context)
{
try
Expand All @@ -23,7 +23,7 @@ public async Task InvokeAsync(HttpContext context)
await HandleExceptionAsync(context, ex);
}
}

private static async Task HandleExceptionAsync(HttpContext context, Exception exception)
{
context.Response.ContentType = ContentType;
Expand All @@ -50,9 +50,9 @@ private static async Task HandleExceptionAsync(HttpContext context, Exception ex
context.Response.StatusCode = statusCode;

var result = JsonSerializer.Serialize(new ExceptionResponseMessage(statusCode, message));

await context.Response.WriteAsync(result);
}
}

public record ExceptionResponseMessage(int StatusCode, string Message);
public record ExceptionResponseMessage(int StatusCode, string Message);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace EvolutionaryArchitecture.Fitnet.Common.Api.Validation.Requests;

public static class EndpointBuilderExtensions
{
public static RouteHandlerBuilder ValidateRequest<TRequest>(this RouteHandlerBuilder builder) where TRequest : class =>
public static RouteHandlerBuilder ValidateRequest<TRequest>(this RouteHandlerBuilder builder) where TRequest : class =>
builder.AddEndpointFilter<RequestValidationApiFilter<TRequest>>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace EvolutionaryArchitecture.Fitnet.Common.Api.Validation.Requests;

public static class RequestValidationsExtensions
{
public static IServiceCollection AddRequestsValidations(this IServiceCollection services, Assembly assembly) =>
public static IServiceCollection AddRequestsValidations(this IServiceCollection services, Assembly assembly) =>
services.AddValidatorsFromAssembly(assembly, includeInternalTypes: true);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using EvolutionaryArchitecture.Fitnet.Common.Infrastructure.Events.EventBus;
namespace EvolutionaryArchitecture.Fitnet.Common.Infrastructure;

using Events.EventBus;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;

namespace EvolutionaryArchitecture.Fitnet.Common.Infrastructure;

public static class CommonInfrastructureModule
{
public static IServiceCollection AddCommonInfrastructure(this IServiceCollection services)
Expand All @@ -13,4 +13,4 @@ public static IServiceCollection AddCommonInfrastructure(this IServiceCollection

return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace EvolutionaryArchitecture.Fitnet.Common.Infrastructure.Events.EventBus;

internal static class EventBusModule
{
internal static IServiceCollection AddEventBus(this IServiceCollection services) =>
internal static IServiceCollection AddEventBus(this IServiceCollection services) =>
services.AddInMemoryEventBus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace EvolutionaryArchitecture.Fitnet.Common.Infrastructure.Events.EventBus.
internal sealed class InMemoryEventBus : IEventBus
{
private readonly IMediator _mediator;

public InMemoryEventBus(IMediator mediator) => _mediator = mediator;

public async Task PublishAsync<TEvent>(TEvent @event, CancellationToken cancellationToken = default) where TEvent : IIntegrationEvent =>
public async Task PublishAsync<TEvent>(TEvent @event, CancellationToken cancellationToken = default) where TEvent : IIntegrationEvent =>
await _mediator.Publish(@event, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class InMemoryEventBusModule
public static IServiceCollection AddInMemoryEventBus(this IServiceCollection services)
{
services.AddSingleton<IEventBus, InMemoryEventBus>();

return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class MediatorModule
public static IServiceCollection AddMediator(this IServiceCollection services, Assembly assembly)
{
services.AddMediatR(configuration => configuration.RegisterServicesFromAssembly(assembly));

return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public static bool IsModuleEnabled(this IServiceCollection services, string modu
{
var buildServiceProvider = services.BuildServiceProvider();
var featureManager = buildServiceProvider.GetRequiredService<IFeatureManager>();

return featureManager.IsEnabledAsync(module).Result;
}

public static bool IsModuleEnabled(this IApplicationBuilder applicationBuilder, string module)
{
var buildServiceProvider = applicationBuilder.ApplicationServices;
var featureManager = buildServiceProvider.GetRequiredService<IFeatureManager>();

return featureManager.IsEnabledAsync(module).Result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public static class ConfigurationExtensions
{
public static WebApplicationFactory<T> WithContainerDatabaseConfigured<T>(
this WebApplicationFactory<T> webApplicationFactory, IDatabaseConfiguration databaseConfiguration)
where T : class
{
return webApplicationFactory.UseSettings(databaseConfiguration.Get());
}
where T : class => webApplicationFactory.UseSettings(databaseConfiguration.Get());

private static WebApplicationFactory<T> UseSettings<T>(
this WebApplicationFactory<T> webApplicationFactory,
Expand All @@ -24,7 +21,9 @@ private static WebApplicationFactory<T> UseSettings<T>(
webApplicationFactory.WithWebHostBuilder(webHostBuilder =>
{
foreach (var setting in settings)
{
webHostBuilder.UseSetting(setting.Key, setting.Value);
}
});

public static WebApplicationFactory<T> SetFakeSystemClock<T>(
Expand All @@ -50,4 +49,4 @@ public static WebApplicationFactory<T> WithFakeConsumers<T>(
webApplicationFactory.WithWebHostBuilder(webHostBuilder =>
webHostBuilder.ConfigureTestServices(services =>
services.AddMediator(executingAssembly)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public sealed class DatabaseContainer : IAsyncLifetime
private const string Password = "$3cureP@ssw0rd";
private const string Database = "fitnet";
private PostgreSqlContainer? _container;
public string? ConnectionString;
public string? ConnectionString { get; private set; }

public async Task InitializeAsync()
{
Expand All @@ -20,9 +20,9 @@ public async Task InitializeAsync()
.Build();

await _container!.StartAsync();

ConnectionString = _container.GetConnectionString();
}

public async Task DisposeAsync() => await _container!.StopAsync();
}
}
Loading

0 comments on commit 79ebe05

Please sign in to comment.