From a30ef66afc785382d62b19d3e94e47f1f88a7fd0 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 20 Dec 2020 21:05:04 +0200 Subject: [PATCH] NDB.Application.DataContracts --- NDB.Application.DataContracts/Command.cs | 24 +++++++++++++ NDB.Application.DataContracts/Metadata.cs | 25 +++++++++++++ .../NDB.Application.DataContracts.csproj | 11 ++++++ NDB.Application.DataContracts/Query.cs | 6 ++++ .../Filters/DtoSchemaFilter.cs | 35 +++++++++++++++++++ NDB.sln | 12 ++++++- 6 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 NDB.Application.DataContracts/Command.cs create mode 100644 NDB.Application.DataContracts/Metadata.cs create mode 100644 NDB.Application.DataContracts/NDB.Application.DataContracts.csproj create mode 100644 NDB.Application.DataContracts/Query.cs create mode 100644 NDB.Extensions.Swagger/Filters/DtoSchemaFilter.cs diff --git a/NDB.Application.DataContracts/Command.cs b/NDB.Application.DataContracts/Command.cs new file mode 100644 index 0000000..3a199c0 --- /dev/null +++ b/NDB.Application.DataContracts/Command.cs @@ -0,0 +1,24 @@ +using MediatR; +using System; + +namespace NDB.Application.DataContracts +{ + public interface ICommand + { + } + + public abstract class Command : ICommand, IRequest + { + public Metadata Metadata { get; } + + protected Command() + { + Metadata = new Metadata() { CorrelationId = Guid.NewGuid() }; + } + + protected Command(Metadata metadata) + { + Metadata = metadata; + } + } +} diff --git a/NDB.Application.DataContracts/Metadata.cs b/NDB.Application.DataContracts/Metadata.cs new file mode 100644 index 0000000..a8c261d --- /dev/null +++ b/NDB.Application.DataContracts/Metadata.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; + +namespace NDB.Application.DataContracts +{ + public class Metadata : Dictionary + { + public const string CorrelationIdKey = "CorrelationId"; + + public Guid CorrelationId + { + get + { + return Guid.Parse(this[CorrelationIdKey]); + } + set + { + if (ContainsKey(CorrelationIdKey)) + this[CorrelationIdKey] = value.ToString(); + else + Add(CorrelationIdKey, value.ToString()); + } + } + } +} diff --git a/NDB.Application.DataContracts/NDB.Application.DataContracts.csproj b/NDB.Application.DataContracts/NDB.Application.DataContracts.csproj new file mode 100644 index 0000000..c944418 --- /dev/null +++ b/NDB.Application.DataContracts/NDB.Application.DataContracts.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/NDB.Application.DataContracts/Query.cs b/NDB.Application.DataContracts/Query.cs new file mode 100644 index 0000000..138ed1f --- /dev/null +++ b/NDB.Application.DataContracts/Query.cs @@ -0,0 +1,6 @@ +using MediatR; + +namespace NDB.Application.DataContracts +{ + public abstract class Query : IRequest, IBaseRequest { } +} diff --git a/NDB.Extensions.Swagger/Filters/DtoSchemaFilter.cs b/NDB.Extensions.Swagger/Filters/DtoSchemaFilter.cs new file mode 100644 index 0000000..9c2dc43 --- /dev/null +++ b/NDB.Extensions.Swagger/Filters/DtoSchemaFilter.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace NDB.Extensions.Swagger.Filters +{ + public class DtoSchemaFilter : ISchemaFilter + { + public void Apply(OpenApiSchema schema, SchemaFilterContext context) + { + var targetType = context.Type; + while (targetType != null) + { + if (typeof(ICommand).IsAssignableFrom(targetType)) + { + foreach (var property in schema.Properties.ToList()) + { + property.Value.ReadOnly = false; + + switch (property.Key) + { + case "metadata": + schema.Properties.Remove(property.Key); + break; + default: + break; + } + } + } + + targetType = targetType.DeclaringType; + } + } + } +} diff --git a/NDB.sln b/NDB.sln index 6e5babd..b961467 100644 --- a/NDB.sln +++ b/NDB.sln @@ -27,7 +27,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "extensions", "extensions", EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "swagger", "swagger", "{FB4E71FD-9E32-4B5C-8019-1E8EBB9DDE9F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Extensions.Swagger", "NDB.Extensions.Swagger\NDB.Extensions.Swagger.csproj", "{CB9BDFCC-DB01-4A13-8091-9A241BE85CCD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Extensions.Swagger", "NDB.Extensions.Swagger\NDB.Extensions.Swagger.csproj", "{CB9BDFCC-DB01-4A13-8091-9A241BE85CCD}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "application", "application", "{564B5266-6F7E-4AFA-A7D4-B0E8F2E7107E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Application.DataContracts", "NDB.Application.DataContracts\NDB.Application.DataContracts.csproj", "{BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -59,6 +63,10 @@ Global {CB9BDFCC-DB01-4A13-8091-9A241BE85CCD}.Debug|Any CPU.Build.0 = Debug|Any CPU {CB9BDFCC-DB01-4A13-8091-9A241BE85CCD}.Release|Any CPU.ActiveCfg = Release|Any CPU {CB9BDFCC-DB01-4A13-8091-9A241BE85CCD}.Release|Any CPU.Build.0 = Release|Any CPU + {BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -67,6 +75,8 @@ Global {B50B55F0-9E6E-4061-9100-E2329A44E76B} = {E0202271-4E92-4DB8-900D-B5FD745B9278} {FB4E71FD-9E32-4B5C-8019-1E8EBB9DDE9F} = {B50B55F0-9E6E-4061-9100-E2329A44E76B} {CB9BDFCC-DB01-4A13-8091-9A241BE85CCD} = {FB4E71FD-9E32-4B5C-8019-1E8EBB9DDE9F} + {564B5266-6F7E-4AFA-A7D4-B0E8F2E7107E} = {E0202271-4E92-4DB8-900D-B5FD745B9278} + {BDDC2DB1-96DD-49F4-BCEF-55D06B74E6F3} = {564B5266-6F7E-4AFA-A7D4-B0E8F2E7107E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {87541BAB-3FAC-4ADB-A7FB-8228DA87843D}