Skip to content

Commit

Permalink
Lazy binder creation, with factory, to prevent checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroservienti committed Apr 1, 2021
1 parent 31f659d commit f9f377f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#if NET5_0 || NETCOREAPP3_1

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -12,20 +11,7 @@ public static class HttpRequestModelBinderExtension
public static Task<T> Bind<T>(this HttpRequest request) where T : new()
{
var context = request.HttpContext;
RequestModelBinder binder;
try
{
binder = context.RequestServices.GetRequiredService<RequestModelBinder>();
}
catch (InvalidOperationException e)
{
throw new InvalidOperationException("Unable to resolve one of the services required to support model binding. " +
"Make sure the application is configured to use MVC services by calling either " +
$"services.{nameof(MvcServiceCollectionExtensions.AddControllers)}(), or " +
$"services.{nameof(MvcServiceCollectionExtensions.AddControllersWithViews)}(), or " +
$"services.{nameof(MvcServiceCollectionExtensions.AddMvc)}(), or " +
$"services.{nameof(MvcServiceCollectionExtensions.AddRazorPages)}().", e);
}
var binder = context.RequestServices.GetRequiredService<RequestModelBinder>();

return binder.Bind<T>(request);
}
Expand Down
24 changes: 23 additions & 1 deletion src/ServiceComposer.AspNetCore/ViewModelCompositionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
#if NETCOREAPP3_1 || NET5_0
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Options;
#endif

namespace ServiceComposer.AspNetCore
{
Expand Down Expand Up @@ -79,7 +84,24 @@ internal void InitializeServiceCollection()
});
}

Services.AddSingleton<RequestModelBinder>();
Services.AddSingleton(container =>
{
var modelBinderFactory = container.GetService<IModelBinderFactory>();
var modelMetadataProvider = container.GetService<IModelMetadataProvider>();
var mvcOptions = container.GetService<IOptions<MvcOptions>>();

if (modelBinderFactory == null || modelMetadataProvider == null || mvcOptions == null)
{
throw new InvalidOperationException("Unable to resolve one of the services required to support model binding. " +
"Make sure the application is configured to use MVC services by calling either " +
$"services.{nameof(MvcServiceCollectionExtensions.AddControllers)}(), or " +
$"services.{nameof(MvcServiceCollectionExtensions.AddControllersWithViews)}(), or " +
$"services.{nameof(MvcServiceCollectionExtensions.AddMvc)}(), or " +
$"services.{nameof(MvcServiceCollectionExtensions.AddRazorPages)}().");
}

return new RequestModelBinder(modelBinderFactory, modelMetadataProvider, mvcOptions);
});
#endif

if (AssemblyScanner.IsEnabled)
Expand Down

0 comments on commit f9f377f

Please sign in to comment.