From 57b5db3e2fbc1c82aaf4821c6c9a2298fc4a681c Mon Sep 17 00:00:00 2001 From: James Gunn Date: Thu, 5 Oct 2023 13:35:04 +0100 Subject: [PATCH] Add a convention to bind JourneyInstance properties by default --- ...BindJourneyInstancePropertiesConvention.cs | 19 +++++++++++++++++++ .../TeachingRecordSystem.SupportUi/Program.cs | 3 +++ 2 files changed, 22 insertions(+) create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Infrastructure/Conventions/BindJourneyInstancePropertiesConvention.cs diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Infrastructure/Conventions/BindJourneyInstancePropertiesConvention.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Infrastructure/Conventions/BindJourneyInstancePropertiesConvention.cs new file mode 100644 index 0000000000..5addae2139 --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Infrastructure/Conventions/BindJourneyInstancePropertiesConvention.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using Microsoft.AspNetCore.Mvc.ModelBinding; + +namespace TeachingRecordSystem.SupportUi.Infrastructure.Conventions; + +public class BindJourneyInstancePropertiesConvention : IPageApplicationModelConvention +{ + public void Apply(PageApplicationModel model) + { + var journeyInstanceProperties = model.HandlerProperties.Where(p => + p.ParameterType == typeof(JourneyInstance) || + (p.ParameterType.IsGenericType && p.ParameterType.GetGenericTypeDefinition() == typeof(JourneyInstance<>))); + + foreach (var journeyInstanceProperty in journeyInstanceProperties) + { + journeyInstanceProperty.BindingInfo ??= new BindingInfo(); + } + } +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Program.cs b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Program.cs index 52de1fe24d..0d623b290e 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Program.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.SupportUi/Program.cs @@ -17,6 +17,7 @@ using TeachingRecordSystem.Core.Infrastructure.Configuration; using TeachingRecordSystem.SupportUi; using TeachingRecordSystem.SupportUi.Infrastructure; +using TeachingRecordSystem.SupportUi.Infrastructure.Conventions; using TeachingRecordSystem.SupportUi.Infrastructure.Filters; using TeachingRecordSystem.SupportUi.Infrastructure.FormFlow; using TeachingRecordSystem.SupportUi.Infrastructure.ModelBinding; @@ -103,6 +104,8 @@ builder.Services .AddRazorPages(options => { + options.Conventions.Add(new BindJourneyInstancePropertiesConvention()); + options.Conventions.AddFolderApplicationModelConvention( "/Persons/PersonDetail", model =>