-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from cnblogs/fix-string-result-deserialization
feat: support cqrs-v2 responses
- Loading branch information
Showing
19 changed files
with
312 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Cnblogs.Architecture.IntegrationTests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsHeaderNames.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore; | ||
|
||
internal static class CqrsHeaderNames | ||
{ | ||
public const string CqrsVersion = "X-Cqrs-Version"; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsObjectResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore; | ||
|
||
/// <summary> | ||
/// Send command response as json and report current cqrs version. | ||
/// </summary> | ||
/// <param name="value"></param> | ||
public class CqrsObjectResult(object? value) : ObjectResult(value) | ||
{ | ||
/// <inheritdoc /> | ||
public override Task ExecuteResultAsync(ActionContext context) | ||
{ | ||
context.HttpContext.Response.Headers.AppendCurrentCqrsVersion(); | ||
return base.ExecuteResultAsync(context); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore; | ||
|
||
/// <summary> | ||
/// Send object as json and append X-Cqrs-Version header | ||
/// </summary> | ||
/// <param name="commandResponse"></param> | ||
public class CqrsResult(object commandResponse) : IResult | ||
{ | ||
/// <inheritdoc /> | ||
public Task ExecuteAsync(HttpContext httpContext) | ||
{ | ||
httpContext.Response.Headers.Append("X-Cqrs-Version", "2"); | ||
return httpContext.Response.WriteAsJsonAsync(commandResponse); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsResultExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore; | ||
|
||
/// <summary> | ||
/// Extension methods for creating cqrs result. | ||
/// </summary> | ||
public static class CqrsResultExtensions | ||
{ | ||
/// <summary> | ||
/// Write result as json and append cqrs response header. | ||
/// </summary> | ||
/// <param name="extensions"><see cref="IResultExtensions"/></param> | ||
/// <param name="result">The command response.</param> | ||
/// <returns></returns> | ||
public static IResult Cqrs(this IResultExtensions extensions, object result) | ||
{ | ||
ArgumentNullException.ThrowIfNull(extensions); | ||
return new CqrsResult(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Cnblogs.Architecture.Ddd.Cqrs.AspNetCore/CqrsVersionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Net.Http.Headers; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Cnblogs.Architecture.Ddd.Cqrs.AspNetCore; | ||
|
||
internal static class CqrsVersionExtensions | ||
{ | ||
private const int CurrentCqrsVersion = 2; | ||
|
||
public static int CqrsVersion(this IHeaderDictionary headers) | ||
{ | ||
return int.TryParse(headers[CqrsHeaderNames.CqrsVersion].ToString(), out var version) ? version : 1; | ||
} | ||
|
||
public static int CqrsVersion(this HttpHeaders headers) | ||
{ | ||
if (headers.Contains(CqrsHeaderNames.CqrsVersion) == false) | ||
{ | ||
return 1; | ||
} | ||
|
||
return headers.GetValues(CqrsHeaderNames.CqrsVersion).Select(x => int.TryParse(x, out var y) ? y : 1).Max(); | ||
} | ||
|
||
public static void CqrsVersion(this IHeaderDictionary headers, int version) | ||
{ | ||
headers.Append(CqrsHeaderNames.CqrsVersion, version.ToString()); | ||
} | ||
|
||
public static void AppendCurrentCqrsVersion(this IHeaderDictionary headers) | ||
{ | ||
headers.CqrsVersion(CurrentCqrsVersion); | ||
} | ||
|
||
public static void AppendCurrentCqrsVersion(this HttpHeaders headers) | ||
{ | ||
headers.Add(CqrsHeaderNames.CqrsVersion, CurrentCqrsVersion.ToString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.