Skip to content

Commit 896b3d8

Browse files
committed
Refactor constructors to use C# primary constructor syntax
Updated multiple classes to use C# primary constructor syntax for improved readability and conciseness. Replaced manual null checks with ArgumentNullException.ThrowIfNull where appropriate. Also simplified collection initializations and minor code style improvements throughout the codebase.
1 parent 39fe03c commit 896b3d8

13 files changed

Lines changed: 82 additions & 207 deletions

src/Zetian/Authentication/IAuthenticator.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,25 @@ Task<AuthenticationResult> AuthenticateAsync(
2929
/// <summary>
3030
/// Authentication result
3131
/// </summary>
32-
public class AuthenticationResult
32+
/// <remarks>
33+
/// Initializes a new authentication result
34+
/// </remarks>
35+
public class AuthenticationResult(bool success, string? identity = null, string? errorMessage = null)
3336
{
34-
/// <summary>
35-
/// Initializes a new authentication result
36-
/// </summary>
37-
public AuthenticationResult(bool success, string? identity = null, string? errorMessage = null)
38-
{
39-
Success = success;
40-
Identity = identity;
41-
ErrorMessage = errorMessage;
42-
}
43-
4437
/// <summary>
4538
/// Gets whether authentication succeeded
4639
/// </summary>
47-
public bool Success { get; }
40+
public bool Success { get; } = success;
4841

4942
/// <summary>
5043
/// Gets the authenticated identity
5144
/// </summary>
52-
public string? Identity { get; }
45+
public string? Identity { get; } = identity;
5346

5447
/// <summary>
5548
/// Gets the error message if failed
5649
/// </summary>
57-
public string? ErrorMessage { get; }
50+
public string? ErrorMessage { get; } = errorMessage;
5851

5952
/// <summary>
6053
/// Creates a successful result

src/Zetian/Authentication/LoginAuthenticator.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,8 @@ namespace Zetian.Authentication
1010
/// <summary>
1111
/// Implements LOGIN authentication mechanism
1212
/// </summary>
13-
public class LoginAuthenticator : IAuthenticator
13+
public class LoginAuthenticator(AuthenticationHandler? handler = null) : IAuthenticator
1414
{
15-
private readonly AuthenticationHandler? _handler;
16-
17-
public LoginAuthenticator(AuthenticationHandler? handler = null)
18-
{
19-
_handler = handler;
20-
}
21-
2215
public string Mechanism => "LOGIN";
2316

2417
public async Task<AuthenticationResult> AuthenticateAsync(
@@ -84,9 +77,9 @@ public async Task<AuthenticationResult> AuthenticateAsync(
8477
return AuthenticationResult.Fail("Invalid password encoding");
8578
}
8679

87-
if (_handler != null)
80+
if (handler != null)
8881
{
89-
return await _handler(username, password).ConfigureAwait(false);
82+
return await handler(username, password).ConfigureAwait(false);
9083
}
9184

9285
// Default: accept any non-empty credentials

src/Zetian/Authentication/PlainAuthenticator.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,8 @@ namespace Zetian.Authentication
1010
/// <summary>
1111
/// Implements PLAIN authentication mechanism
1212
/// </summary>
13-
public class PlainAuthenticator : IAuthenticator
13+
public class PlainAuthenticator(AuthenticationHandler? handler = null) : IAuthenticator
1414
{
15-
private readonly AuthenticationHandler? _handler;
16-
17-
public PlainAuthenticator(AuthenticationHandler? handler = null)
18-
{
19-
_handler = handler;
20-
}
21-
2215
public string Mechanism => "PLAIN";
2316

2417
public async Task<AuthenticationResult> AuthenticateAsync(
@@ -79,9 +72,9 @@ public async Task<AuthenticationResult> AuthenticateAsync(
7972
password = parts[2];
8073
}
8174

82-
if (_handler != null)
75+
if (handler != null)
8376
{
84-
return await _handler(username, password).ConfigureAwait(false);
77+
return await handler(username, password).ConfigureAwait(false);
8578
}
8679

8780
// Default: accept any non-empty credentials

src/Zetian/Core/EventArgs.cs

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,113 +6,85 @@ namespace Zetian.Core
66
/// <summary>
77
/// Event arguments for session events
88
/// </summary>
9-
public class SessionEventArgs : EventArgs
9+
public class SessionEventArgs(ISmtpSession session) : EventArgs
1010
{
11-
public SessionEventArgs(ISmtpSession session)
12-
{
13-
Session = session ?? throw new ArgumentNullException(nameof(session));
14-
}
15-
1611
/// <summary>
1712
/// Gets the SMTP session
1813
/// </summary>
19-
public ISmtpSession Session { get; }
14+
public ISmtpSession Session { get; } = session ?? throw new ArgumentNullException(nameof(session));
2015
}
2116

2217
/// <summary>
2318
/// Event arguments for message events
2419
/// </summary>
25-
public class MessageEventArgs : EventArgs
20+
public class MessageEventArgs(ISmtpMessage message, ISmtpSession session) : EventArgs
2621
{
27-
public MessageEventArgs(ISmtpMessage message, ISmtpSession session)
28-
{
29-
Message = message ?? throw new ArgumentNullException(nameof(message));
30-
Session = session ?? throw new ArgumentNullException(nameof(session));
31-
Cancel = false;
32-
Response = SmtpResponse.Ok;
33-
}
34-
3522
/// <summary>
3623
/// Gets the received message
3724
/// </summary>
38-
public ISmtpMessage Message { get; }
25+
public ISmtpMessage Message { get; } = message ?? throw new ArgumentNullException(nameof(message));
3926

4027
/// <summary>
4128
/// Gets the session that received the message
4229
/// </summary>
43-
public ISmtpSession Session { get; }
30+
public ISmtpSession Session { get; } = session ?? throw new ArgumentNullException(nameof(session));
4431

4532
/// <summary>
4633
/// Gets or sets whether to cancel/reject the message
4734
/// </summary>
48-
public bool Cancel { get; set; }
35+
public bool Cancel { get; set; } = false;
4936

5037
/// <summary>
5138
/// Gets or sets the response to send to the client
5239
/// </summary>
53-
public SmtpResponse Response { get; set; }
40+
public SmtpResponse Response { get; set; } = SmtpResponse.Ok;
5441
}
5542

5643
/// <summary>
5744
/// Event arguments for error events
5845
/// </summary>
59-
public class ErrorEventArgs : EventArgs
46+
public class ErrorEventArgs(Exception exception, ISmtpSession? session = null) : EventArgs
6047
{
61-
public ErrorEventArgs(Exception exception, ISmtpSession? session = null)
62-
{
63-
Exception = exception ?? throw new ArgumentNullException(nameof(exception));
64-
Session = session;
65-
}
66-
6748
/// <summary>
6849
/// Gets the exception that occurred
6950
/// </summary>
70-
public Exception Exception { get; }
51+
public Exception Exception { get; } = exception ?? throw new ArgumentNullException(nameof(exception));
7152

7253
/// <summary>
7354
/// Gets the session where the error occurred (if any)
7455
/// </summary>
75-
public ISmtpSession? Session { get; }
56+
public ISmtpSession? Session { get; } = session;
7657
}
7758

7859
/// <summary>
7960
/// Event arguments for authentication events
8061
/// </summary>
81-
public class AuthenticationEventArgs : EventArgs
62+
public class AuthenticationEventArgs(string mechanism, string? username, string? password, ISmtpSession session) : EventArgs
8263
{
83-
public AuthenticationEventArgs(string mechanism, string? username, string? password, ISmtpSession session)
84-
{
85-
Mechanism = mechanism ?? throw new ArgumentNullException(nameof(mechanism));
86-
Username = username;
87-
Password = password;
88-
Session = session ?? throw new ArgumentNullException(nameof(session));
89-
IsAuthenticated = false;
90-
}
91-
9264
/// <summary>
9365
/// Gets the authentication mechanism
9466
/// </summary>
95-
public string Mechanism { get; }
67+
public string Mechanism { get; } = mechanism ?? throw new ArgumentNullException(nameof(mechanism));
9668

9769
/// <summary>
9870
/// Gets the username
9971
/// </summary>
100-
public string? Username { get; }
72+
public string? Username { get; } = username;
10173

10274
/// <summary>
10375
/// Gets the password
10476
/// </summary>
105-
public string? Password { get; }
77+
public string? Password { get; } = password;
10678

10779
/// <summary>
10880
/// Gets the session
10981
/// </summary>
110-
public ISmtpSession Session { get; }
82+
public ISmtpSession Session { get; } = session ?? throw new ArgumentNullException(nameof(session));
11183

11284
/// <summary>
11385
/// Gets or sets whether the authentication succeeded
11486
/// </summary>
115-
public bool IsAuthenticated { get; set; }
87+
public bool IsAuthenticated { get; set; } = false;
11688

11789
/// <summary>
11890
/// Gets or sets the authenticated identity

src/Zetian/Extensions/RateLimiting/InMemoryRateLimiter.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ public Task<bool> IsAllowedAsync(string key)
4141

4242
public Task<bool> IsAllowedAsync(IPAddress ipAddress)
4343
{
44-
if (ipAddress == null)
45-
{
46-
throw new ArgumentNullException(nameof(ipAddress));
47-
}
44+
ArgumentNullException.ThrowIfNull(ipAddress);
4845

4946
return IsAllowedAsync(ipAddress.ToString());
5047
}
@@ -63,10 +60,7 @@ public Task RecordRequestAsync(string key)
6360

6461
public Task RecordRequestAsync(IPAddress ipAddress)
6562
{
66-
if (ipAddress == null)
67-
{
68-
throw new ArgumentNullException(nameof(ipAddress));
69-
}
63+
ArgumentNullException.ThrowIfNull(ipAddress);
7064

7165
return RecordRequestAsync(ipAddress.ToString());
7266
}

src/Zetian/Extensions/SmtpServerBuilderExtensions.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Security.Cryptography.X509Certificates;
23
using Zetian.Authentication;
34
using Zetian.Storage;
45

@@ -14,10 +15,7 @@ public static class SmtpServerBuilderExtensions
1415
/// </summary>
1516
public static SmtpServerBuilder AddSpamFilter(this SmtpServerBuilder builder, params string[] spamDomains)
1617
{
17-
if (builder == null)
18-
{
19-
throw new ArgumentNullException(nameof(builder));
20-
}
18+
ArgumentNullException.ThrowIfNull(builder);
2119

2220
return builder.WithSenderDomainBlacklist(spamDomains);
2321
}
@@ -27,10 +25,7 @@ public static SmtpServerBuilder AddSpamFilter(this SmtpServerBuilder builder, pa
2725
/// </summary>
2826
public static SmtpServerBuilder AddAllowedDomains(this SmtpServerBuilder builder, params string[] domains)
2927
{
30-
if (builder == null)
31-
{
32-
throw new ArgumentNullException(nameof(builder));
33-
}
28+
ArgumentNullException.ThrowIfNull(builder);
3429

3530
return builder.WithRecipientDomainWhitelist(domains);
3631
}
@@ -40,10 +35,7 @@ public static SmtpServerBuilder AddAllowedDomains(this SmtpServerBuilder builder
4035
/// </summary>
4136
public static SmtpServerBuilder AddSizeFilter(this SmtpServerBuilder builder, long maxSizeInBytes)
4237
{
43-
if (builder == null)
44-
{
45-
throw new ArgumentNullException(nameof(builder));
46-
}
38+
ArgumentNullException.ThrowIfNull(builder);
4739

4840
return builder.MaxMessageSize(maxSizeInBytes);
4941
}
@@ -75,7 +67,7 @@ public static SmtpServer CreateDevelopment(this SmtpServerBuilder _, int port =
7567
public static SmtpServer CreateProduction(
7668
this SmtpServerBuilder _,
7769
int port,
78-
System.Security.Cryptography.X509Certificates.X509Certificate2 certificate,
70+
X509Certificate2 certificate,
7971
AuthenticationHandler authHandler,
8072
IMessageStore? messageStore = null,
8173
IMailboxFilter? mailboxFilter = null)

0 commit comments

Comments
 (0)