Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ on:
branches: [ "main" ]

jobs:
test-net8:
runs-on: ubuntu-latest
container: mcr.microsoft.com/dotnet/sdk:8.0

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Build
run: ./build.sh net8.0
- name: Test
run: ./test.sh net8.0

test-net9:
runs-on: ubuntu-latest
container: mcr.microsoft.com/dotnet/sdk:9.0
Expand All @@ -27,7 +15,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Build
run: ./build.sh net9.0
run: dotnet build
- name: Test
run: ./test.sh net9.0
run: dotnet test

2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Authors>Cnblogs</Authors>
Expand Down
4 changes: 0 additions & 4 deletions build.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@

<ItemGroup>
<PackageReference Include="Mapster" Version="7.4.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.8" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.8" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public record CommandResponse : IValidationResponse, ILockableResponse
public string ErrorMessage { get; init; } = string.Empty;

/// <inheritdoc />
public ValidationErrors ValidationErrors { get; init; } = new();
public ValidationErrors ValidationErrors { get; init; } = [];

/// <inheritdoc />
public bool LockAcquired { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Cnblogs.Architecture.Ddd.Cqrs.Abstractions;
/// </summary>
public class ValidationErrors : ICollection<ValidationError>
{
private readonly List<ValidationError> _validationErrors = new();
private readonly List<ValidationError> _validationErrors = [];

/// <summary>
/// Add a new validation error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.19" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.19" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
</ItemGroup>

<ItemGroup>
Expand Down
24 changes: 12 additions & 12 deletions src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsRouteMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore;
/// </summary>
public static class CqrsRouteMapper
{
private static readonly List<Type> QueryTypes = new() { typeof(IQuery<>), typeof(IListQuery<>) };
private static readonly List<Type> QueryTypes = [typeof(IQuery<>), typeof(IListQuery<>)];

private static readonly List<Type> CommandTypes = new() { typeof(ICommand<>), typeof(ICommand<,>) };
private static readonly List<Type> CommandTypes = [typeof(ICommand<>), typeof(ICommand<,>)];

private static readonly string[] GetAndHeadMethods = { "GET", "HEAD" };
private static readonly string[] GetAndHeadMethods = ["GET", "HEAD"];

private static readonly List<string> PostCommandPrefixes = new()
{
private static readonly List<string> PostCommandPrefixes =
[
"Create",
"Add",
"New"
};
];

private static readonly List<string> PutCommandPrefixes = new()
{
private static readonly List<string> PutCommandPrefixes =
[
"Update",
"Modify",
"Replace",
"Alter"
};
];

private static readonly List<string> DeleteCommandPrefixes = new()
{
private static readonly List<string> DeleteCommandPrefixes =
[
"Delete",
"Remove",
"Clean",
"Clear",
"Purge"
};
];

/// <summary>
/// Map a query API, using GET method. <typeparamref name="T"/> would been constructed from route and query string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Cnblogs.Architecture.Ddd.Cqrs.Dapper.Clickhouse;
/// </summary>
public class ClickhouseContextCollection
{
internal List<Type> ContextTypes { get; } = new();
internal List<Type> ContextTypes { get; } = [];

internal void Add<TContext>()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Cnblogs.Architecture.Ddd.Cqrs.MongoDb;
/// </summary>
public class MongoContextCollection
{
private readonly List<Type> _contexts = new();
private readonly List<Type> _contexts = [];

/// <summary>
/// 添加一个 MongoContext。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ public async Task<CommandResponse<TResponse, TError>> PutCommandAsync<TResponse,
return response.StatusCode switch
{
HttpStatusCode.OK => await response.Content.ReadFromJsonAsync<T>(),
HttpStatusCode.NotFound => default,
_ => default
};
}
Expand Down Expand Up @@ -202,7 +201,7 @@ public async Task<List<TResponse>> BatchGetItemsAsync<TResponse, TId>(
'&',
ids.Select(i => $"{WebUtility.UrlEncode(paramName)}={WebUtility.UrlEncode(i.ToString())}"));
url = $"{url}{(url.Contains('?') ? '&' : '?')}{query}";
return await HttpClient.GetFromJsonAsync<List<TResponse>>(url) ?? new List<TResponse>();
return await HttpClient.GetFromJsonAsync<List<TResponse>>(url) ?? [];
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public async Task<List<TResponse>> BatchGetItemsAsync<TResponse, TId>(
'&',
ids.Select(i => $"{WebUtility.UrlEncode(paramName)}={WebUtility.UrlEncode(i.ToString())}"));
url = $"{url}{(url.Contains('?') ? '&' : '?')}{query}";
return await HttpClient.GetFromJsonAsync<List<TResponse>>(url) ?? new List<TResponse>();
return await HttpClient.GetFromJsonAsync<List<TResponse>>(url) ?? [];
}
catch (HttpRequestException e)
{
Expand All @@ -306,7 +306,7 @@ public async Task<List<TResponse>> BatchGetItemsAsync<TResponse, TId>(
ThrowApiException(HttpMethod.Get, url, e);
}

return new List<TResponse>();
return [];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace Cnblogs.Architecture.Ddd.Domain.Abstractions;

/// <summary>
/// Handy calculator for DateTime
/// </summary>
public static class DateTimeOffsetCalculator
{
/// <summary>
/// Get a new <see cref="DateTimeOffset"/> with time set to 0:00:00
/// </summary>
/// <param name="dateTime">Input <see cref="DateTimeOffset"/>.</param>
/// <returns></returns>
public static DateTimeOffset StartOfTheDay(this DateTimeOffset dateTime)
{
return new DateTimeOffset(dateTime.Date, dateTime.Offset);
}

/// <summary>
/// Get a new <see cref="DateTimeOffset"/> with time set to 23:59:59.999.
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTimeOffset EndOfTheDay(this DateTimeOffset dateTime)
{
return StartOfTheDay(dateTime).AddDays(1).AddMilliseconds(-1);
}

/// <summary>
/// Get a new <see cref="DateTimeOffset"/> that set the start of a week for the given date.
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTimeOffset StartOfTheWeek(this DateTimeOffset dateTime)
{
var monday = dateTime.DayOfWeek switch
{
DayOfWeek.Monday => dateTime,
DayOfWeek.Sunday => dateTime.AddDays(-6),
_ => dateTime.AddDays(-(int)(dateTime.DayOfWeek - 1))
};

return monday.StartOfTheDay();
}

/// <summary>
/// Get a new <see cref="DateTimeOffset"/> that set to the end of a week for the given date.
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTimeOffset EndOfTheWeek(this DateTimeOffset dateTime)
{
var sunday = dateTime.DayOfWeek switch
{
DayOfWeek.Sunday => dateTime,
DayOfWeek.Monday => dateTime.AddDays(6),
_ => dateTime.AddDays((int)(7 - dateTime.DayOfWeek))
};

return sunday.EndOfTheDay();
}

/// <summary>
/// Check whether two <see cref="DateTimeOffset"/> is in same date.
/// </summary>
/// <param name="left">First <see cref="DateTimeOffset"/> to check.</param>
/// <param name="right">Second <see cref="DateTimeOffset"/> to check.</param>
/// <returns></returns>
public static bool IsInSameDate(this DateTimeOffset left, DateTimeOffset right)
{
return left.Year == right.Year && left.DayOfYear == right.DayOfYear;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,52 @@ public DateTimeOffset Now()
return DateTimeOffset.Now;
}

/// <inheritdoc />
public DateTimeOffset Yesterday()
{
return Today().AddDays(-1);
}

/// <inheritdoc />
public DateTimeOffset EndOfYesterday()
{
return Today().AddDays(-1).EndOfTheDay();
}

/// <inheritdoc />
public DateTimeOffset Today()
{
var now = Now();
return new DateTimeOffset(now.Year, now.Month, now.Day, 0, 0, 0, now.Offset);
}

/// <inheritdoc />
public DateTimeOffset EndOfToday()
{
return Today().EndOfTheDay();
}

/// <inheritdoc />
public DateTimeOffset Tomorrow()
{
return Today().AddDays(1);
}

/// <inheritdoc />
public DateTimeOffset EndOfTomorrow()
{
return Today().AddDays(1).EndOfTheDay();
}

/// <inheritdoc />
public long UnixSeconds()
{
return Now().ToUnixTimeSeconds();
}

/// <inheritdoc />
public long UnixMilliseconds()
{
return Now().ToUnixTimeMilliseconds();
}
}
2 changes: 1 addition & 1 deletion src/Cnblogs.Architecture.Ddd.Domain.Abstractions/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public abstract class Entity<TKey> : Entity, IEntity<TKey>
/// <param name="generator">领域事件生成器。</param>
public void AddDomainEvent(Func<TKey, IDomainEvent> generator)
{
_domainEventGenerator ??= new List<Func<TKey, IDomainEvent>>();
_domainEventGenerator ??= [];
_domainEventGenerator.Add(generator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class EntityBase : IDomainEventSource
/// <param name="eventItem">领域事件。</param>
public virtual void AddDomainEvent(IDomainEvent eventItem)
{
_events ??= new List<IDomainEvent>();
_events ??= [];
_events.Add(eventItem);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,50 @@ public interface IDateTimeProvider
DateTimeOffset Now();

/// <summary>
/// Get <see cref="DateTimeOffset"/> today's date.
/// Get <see cref="DateTimeOffset"/> of yesterday's date.
/// </summary>
/// <returns></returns>
DateTimeOffset Yesterday();

/// <summary>
/// Get <see cref="DateTimeOffset"/> of yesterday's date with time set to 23:59:59.999.
/// </summary>
/// <returns></returns>
DateTimeOffset EndOfYesterday();

/// <summary>
/// Get <see cref="DateTimeOffset"/> of today's date.
/// </summary>
/// <returns>Today's date.</returns>
DateTimeOffset Today();

/// <summary>
/// Get <see cref="DateTimeOffset"/> of today's date with time set to 23:59:59.999.
/// </summary>
/// <returns></returns>
DateTimeOffset EndOfToday();

/// <summary>
/// Get <see cref="DateTimeOffset"/> of tomorrow's date with time set to 0:00:00.000.
/// </summary>
/// <returns></returns>
DateTimeOffset Tomorrow();

/// <summary>
/// Get <see cref="DateTimeOffset"/> of tomorrow's date with time set to 23:59:59.000.
/// </summary>
/// <returns></returns>
DateTimeOffset EndOfTomorrow();

/// <summary>
/// Get number of seconds that have elapsed since 1970-01-01T00:00:00Z.
/// </summary>
/// <returns>The number of seconds that have elapsed since 1970-01-01T00:00:00Z.</returns>
long UnixSeconds();

/// <summary>
/// Get number of milliseconds that have elapsed since 1970-01-01T00:00:00Z.
/// </summary>
/// <returns></returns>
long UnixMilliseconds();
}
Loading