Skip to content

Commit 0db3475

Browse files
committed
Use file scoped namespace
1 parent deeba66 commit 0db3475

21 files changed

+371
-396
lines changed
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System.Security.Principal;
22

3-
namespace Devpro.Common.AspNetCore.WebApi.Authentication
3+
namespace Devpro.Common.AspNetCore.WebApi.Authentication;
4+
5+
public class BasicAuthenticationClient : IIdentity
46
{
5-
public class BasicAuthenticationClient : IIdentity
6-
{
7-
public string? AuthenticationType { get; set; }
7+
public string? AuthenticationType { get; set; }
88

9-
public bool IsAuthenticated { get; set; }
9+
public bool IsAuthenticated { get; set; }
1010

11-
public string? Name { get; set; }
12-
}
11+
public string? Name { get; set; }
1312
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
namespace Devpro.Common.AspNetCore.WebApi.Authentication
1+
namespace Devpro.Common.AspNetCore.WebApi.Authentication;
2+
3+
public class BasicAuthenticationDefaults
24
{
3-
public class BasicAuthenticationDefaults
4-
{
5-
public const string AuthenticationScheme = "Basic";
6-
}
5+
public const string AuthenticationScheme = "Basic";
76
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using Microsoft.AspNetCore.Authorization;
22

3-
namespace Devpro.Common.AspNetCore.WebApi.Authentication
3+
namespace Devpro.Common.AspNetCore.WebApi.Authentication;
4+
5+
public class BasicAuthorizationAttribute : AuthorizeAttribute
46
{
5-
public class BasicAuthorizationAttribute : AuthorizeAttribute
7+
public BasicAuthorizationAttribute()
68
{
7-
public BasicAuthorizationAttribute()
8-
{
9-
AuthenticationSchemes = BasicAuthenticationDefaults.AuthenticationScheme;
10-
}
9+
AuthenticationSchemes = BasicAuthenticationDefaults.AuthenticationScheme;
1110
}
1211
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
using Devpro.Common.AspNetCore.WebApi.Configuration;
22
using Microsoft.AspNetCore.Builder;
33

4-
namespace Devpro.Common.AspNetCore.WebApi.Builder
4+
namespace Devpro.Common.AspNetCore.WebApi.Builder;
5+
6+
public static class PolicyBuilderExtensions
57
{
6-
public static class PolicyBuilderExtensions
8+
public static IApplicationBuilder UseHttpsRedirection(this IApplicationBuilder app, WebApiConfiguration configuration)
79
{
8-
public static IApplicationBuilder UseHttpsRedirection(this IApplicationBuilder app, WebApiConfiguration configuration)
10+
if (configuration.IsHttpsRedirectionEnabled)
911
{
10-
if (configuration.IsHttpsRedirectionEnabled)
11-
{
12-
app.UseHttpsRedirection();
13-
}
14-
15-
return app;
12+
app.UseHttpsRedirection();
1613
}
14+
15+
return app;
1716
}
1817
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
using Devpro.Common.AspNetCore.WebApi.Configuration;
22
using Microsoft.AspNetCore.Builder;
33

4-
namespace Devpro.Common.AspNetCore.WebApi.Builder
4+
namespace Devpro.Common.AspNetCore.WebApi.Builder;
5+
6+
public static class SwaggerBuilderExtensions
57
{
6-
public static class SwaggerBuilderExtensions
8+
public static IApplicationBuilder UseSwagger(this IApplicationBuilder app, WebApiConfiguration configuration)
79
{
8-
public static IApplicationBuilder UseSwagger(this IApplicationBuilder app, WebApiConfiguration configuration)
10+
if (configuration.IsSwaggerEnabled)
911
{
10-
if (configuration.IsSwaggerEnabled)
11-
{
12-
var openApi = configuration.OpenApi;
13-
14-
app.UseSwagger();
15-
app.UseSwaggerUI(c => c.SwaggerEndpoint($"/swagger/{openApi.Version}/swagger.json", $"{openApi.Title} {openApi.Version}"));
16-
}
12+
var openApi = configuration.OpenApi;
1713

18-
return app;
14+
app.UseSwagger();
15+
app.UseSwaggerUI(c => c.SwaggerEndpoint($"/swagger/{openApi.Version}/swagger.json", $"{openApi.Title} {openApi.Version}"));
1916
}
17+
18+
return app;
2019
}
2120
}
Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
11
using Microsoft.Extensions.Configuration;
22
using Microsoft.OpenApi.Models;
33

4-
namespace Devpro.Common.AspNetCore.WebApi.Configuration
5-
{
6-
public class WebApiConfiguration
7-
{
8-
protected IConfigurationRoot ConfigurationRoot { get; }
4+
namespace Devpro.Common.AspNetCore.WebApi.Configuration;
95

10-
public WebApiConfiguration(IConfigurationRoot configurationRoot)
11-
{
12-
ConfigurationRoot = configurationRoot;
13-
}
6+
public class WebApiConfiguration(IConfigurationRoot configurationRoot)
7+
{
8+
protected IConfigurationRoot ConfigurationRoot { get; } = configurationRoot;
149

15-
// flags
10+
// flags
1611

17-
public bool IsOpenTelemetryEnabled => TryGetSection("Application:IsOpenTelemetryEnabled").Get<bool>();
12+
public bool IsOpenTelemetryEnabled => TryGetSection("Application:IsOpenTelemetryEnabled").Get<bool>();
1813

19-
public bool IsHttpsRedirectionEnabled => TryGetSection("Application:IsHttpsRedirectionEnabled").Get<bool>();
14+
public bool IsHttpsRedirectionEnabled => TryGetSection("Application:IsHttpsRedirectionEnabled").Get<bool>();
2015

21-
public bool IsSwaggerEnabled => TryGetSection("Application:IsSwaggerEnabled").Get<bool>();
16+
public bool IsSwaggerEnabled => TryGetSection("Application:IsSwaggerEnabled").Get<bool>();
2217

2318

24-
// definitions
19+
// definitions
2520

26-
public static string HealthCheckEndpoint => "/health";
21+
public static string HealthCheckEndpoint => "/health";
2722

28-
public OpenApiInfo OpenApi => TryGetSection("OpenApi").Get<OpenApiInfo>() ?? throw new Exception("");
23+
public OpenApiInfo OpenApi => TryGetSection("OpenApi").Get<OpenApiInfo>() ?? throw new Exception("");
2924

30-
public string OpenTelemetryService => TryGetSection("OpenTelemetry:ServiceName").Get<string>() ?? "";
25+
public string OpenTelemetryService => TryGetSection("OpenTelemetry:ServiceName").Get<string>() ?? "";
3126

32-
// infrastructure
27+
// infrastructure
3328

34-
public string OpenTelemetryCollectorEndpoint => TryGetSection("OpenTelemetry:CollectorEndpoint").Get<string>() ?? "";
29+
public string OpenTelemetryCollectorEndpoint => TryGetSection("OpenTelemetry:CollectorEndpoint").Get<string>() ?? "";
3530

36-
// protected methods
31+
// protected methods
3732

38-
protected IConfigurationSection TryGetSection(string sectionKey)
39-
{
40-
return ConfigurationRoot.GetSection(sectionKey)
41-
?? throw new ArgumentException("Missing section \"" + sectionKey + "\" in configuration", nameof(sectionKey));
42-
}
33+
protected IConfigurationSection TryGetSection(string sectionKey)
34+
{
35+
return ConfigurationRoot.GetSection(sectionKey)
36+
?? throw new ArgumentException("Missing section \"" + sectionKey + "\" in configuration", nameof(sectionKey));
4337
}
4438
}

src/Common.AspNetCore/RawRequestBodyFormatter.cs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,56 @@
44
using Microsoft.AspNetCore.Mvc.Formatters;
55
using Microsoft.Net.Http.Headers;
66

7-
namespace Devpro.Common.AspNetCore
7+
namespace Devpro.Common.AspNetCore;
8+
9+
/// <summary>
10+
/// ASP.NET Core input formatter to manage raw request bodies.
11+
/// </summary>
12+
/// <remarks>
13+
/// Solution found at https://weblog.west-wind.com/posts/2017/Sep/14/Accepting-Raw-Request-Body-Content-in-ASPNET-Core-API-Controllers
14+
/// </remarks>
15+
public class RawRequestBodyFormatter : InputFormatter
816
{
9-
/// <summary>
10-
/// ASP.NET Core input formatter to manage raw request bodies.
11-
/// </summary>
12-
/// <remarks>
13-
/// Solution found at https://weblog.west-wind.com/posts/2017/Sep/14/Accepting-Raw-Request-Body-Content-in-ASPNET-Core-API-Controllers
14-
/// </remarks>
15-
public class RawRequestBodyFormatter : InputFormatter
17+
private const string PlainTextContentType = "text/plain";
18+
19+
private const string OctetStreamApplicationContentType = "application/octet-stream";
20+
21+
public RawRequestBodyFormatter()
1622
{
17-
private const string PlainTextContentType = "text/plain";
23+
SupportedMediaTypes.Add(new MediaTypeHeaderValue(PlainTextContentType));
24+
SupportedMediaTypes.Add(new MediaTypeHeaderValue(OctetStreamApplicationContentType));
25+
}
1826

19-
private const string OctetStreamApplicationContentType = "application/octet-stream";
27+
public override bool CanRead(InputFormatterContext context)
28+
{
29+
ArgumentNullException.ThrowIfNull(context);
2030

21-
public RawRequestBodyFormatter()
22-
{
23-
SupportedMediaTypes.Add(new MediaTypeHeaderValue(PlainTextContentType));
24-
SupportedMediaTypes.Add(new MediaTypeHeaderValue(OctetStreamApplicationContentType));
25-
}
31+
var contentType = context.HttpContext.Request.ContentType;
32+
return string.IsNullOrEmpty(contentType)
33+
|| contentType == PlainTextContentType
34+
|| contentType == OctetStreamApplicationContentType;
35+
}
2636

27-
public override bool CanRead(InputFormatterContext context)
28-
{
29-
ArgumentNullException.ThrowIfNull(context);
37+
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
38+
{
39+
var request = context.HttpContext.Request;
3040

31-
var contentType = context.HttpContext.Request.ContentType;
32-
return string.IsNullOrEmpty(contentType)
33-
|| contentType == PlainTextContentType
34-
|| contentType == OctetStreamApplicationContentType;
41+
if (string.IsNullOrEmpty(request.ContentType)
42+
|| request.ContentType == PlainTextContentType)
43+
{
44+
using var reader = new StreamReader(request.Body);
45+
var content = await reader.ReadToEndAsync();
46+
return await InputFormatterResult.SuccessAsync(content);
3547
}
3648

37-
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
49+
if (request.ContentType == OctetStreamApplicationContentType)
3850
{
39-
var request = context.HttpContext.Request;
40-
41-
if (string.IsNullOrEmpty(request.ContentType)
42-
|| request.ContentType == PlainTextContentType)
43-
{
44-
using var reader = new StreamReader(request.Body);
45-
var content = await reader.ReadToEndAsync();
46-
return await InputFormatterResult.SuccessAsync(content);
47-
}
48-
49-
if (request.ContentType == OctetStreamApplicationContentType)
50-
{
51-
using var ms = new MemoryStream(2048);
52-
await request.Body.CopyToAsync(ms);
53-
var content = ms.ToArray();
54-
return await InputFormatterResult.SuccessAsync(content);
55-
}
56-
57-
return await InputFormatterResult.FailureAsync();
51+
using var ms = new MemoryStream(2048);
52+
await request.Body.CopyToAsync(ms);
53+
var content = ms.ToArray();
54+
return await InputFormatterResult.SuccessAsync(content);
5855
}
56+
57+
return await InputFormatterResult.FailureAsync();
5958
}
6059
}

src/Common.MongoDb/DefaultMongoClientFactory.cs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,35 @@
22
using MongoDB.Bson.Serialization.Conventions;
33
using MongoDB.Driver;
44

5-
namespace Devpro.Common.MongoDb
5+
namespace Devpro.Common.MongoDb;
6+
7+
public class DefaultMongoClientFactory : IMongoClientFactory
68
{
7-
public class DefaultMongoClientFactory : IMongoClientFactory
9+
static DefaultMongoClientFactory()
810
{
9-
static DefaultMongoClientFactory()
10-
{
11-
RegisterConventions();
12-
}
11+
RegisterConventions();
12+
}
1313

14-
public virtual MongoClient CreateClient(string connectionString)
15-
{
16-
return new MongoClient(connectionString);
17-
}
14+
public virtual MongoClient CreateClient(string connectionString)
15+
{
16+
return new MongoClient(connectionString);
17+
}
1818

19-
/// <summary>
20-
/// Register usual conventions.
21-
/// </summary>
22-
/// <remarks>
23-
/// See https://github.com/mongodb/mongo-csharp-driver/tree/master/src/MongoDB.Bson/Serialization/Conventions
24-
/// </remarks>
25-
protected static void RegisterConventions()
19+
/// <summary>
20+
/// Register usual conventions.
21+
/// </summary>
22+
/// <remarks>
23+
/// See https://github.com/mongodb/mongo-csharp-driver/tree/master/src/MongoDB.Bson/Serialization/Conventions
24+
/// </remarks>
25+
protected static void RegisterConventions()
26+
{
27+
var pack = new ConventionPack
2628
{
27-
var pack = new ConventionPack
28-
{
29-
new CamelCaseElementNameConvention(),
30-
new EnumRepresentationConvention(BsonType.String),
31-
new IgnoreExtraElementsConvention(true),
32-
new IgnoreIfNullConvention(true)
33-
};
34-
ConventionRegistry.Register("Conventions", pack, t => true);
35-
}
29+
new CamelCaseElementNameConvention(),
30+
new EnumRepresentationConvention(BsonType.String),
31+
new IgnoreExtraElementsConvention(true),
32+
new IgnoreIfNullConvention(true)
33+
};
34+
ConventionRegistry.Register("Conventions", pack, t => true);
3635
}
3736
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
using MongoDB.Driver;
22

3-
namespace Devpro.Common.MongoDb
3+
namespace Devpro.Common.MongoDb;
4+
5+
/// <summary>
6+
/// Avoids calling "new" in application code.
7+
/// </summary>
8+
public interface IMongoClientFactory
49
{
510
/// <summary>
6-
/// Avoids calling "new" in application code.
11+
/// Creates MongoDB client from a given connection string.
712
/// </summary>
8-
public interface IMongoClientFactory
9-
{
10-
/// <summary>
11-
/// Creates MongoDB client from a given connection string.
12-
/// </summary>
13-
/// <param name="connectionString"></param>
14-
/// <returns></returns>
15-
MongoClient CreateClient(string connectionString);
16-
}
13+
/// <param name="connectionString"></param>
14+
/// <returns></returns>
15+
MongoClient CreateClient(string connectionString);
1716
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
namespace Devpro.Common.MongoDb
1+
namespace Devpro.Common.MongoDb;
2+
3+
public class MongoDbConfiguration
24
{
3-
public class MongoDbConfiguration
4-
{
5-
public string ConnectionString { get; set; } = string.Empty;
5+
public string ConnectionString { get; set; } = string.Empty;
66

7-
public string DatabaseName { get; set; } = string.Empty;
8-
}
7+
public string DatabaseName { get; set; } = string.Empty;
98
}

0 commit comments

Comments
 (0)