Skip to content

Commit

Permalink
Post & Put results for aspnet
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Oct 6, 2024
1 parent 212fe49 commit 25f8087
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace Shiny.Mediator.AspNet.Infrastructure;

public class MediatorContractModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
var type = bindingContext.Model!.GetType();
if (type.IsValueType)
{
var ctor = type.GetConstructors().Single();
var parameters = ctor.GetParameters();
foreach (var p in parameters)
{
var result = bindingContext.ValueProvider.GetValue(p.Name);
}
//Activator.CreateInstance()
}
else
{
var props = type
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.CanWrite)
.ToList();

foreach (var prop in props)
{
var result = bindingContext.ValueProvider.GetValue(prop.Name);
}
}
// bindingContext.ActionContext.RouteData.Values
return Task.CompletedTask;
}
}
// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ModelBinding;
//
// namespace Shiny.Mediator.AspNet.Infrastructure;
//
// public class MediatorContractModelBinder : IModelBinder
// {
// public Task BindModelAsync(ModelBindingContext bindingContext)
// {
// var type = bindingContext.Model!.GetType();
// if (type.IsValueType)
// {
// var ctor = type.GetConstructors().Single();
// var parameters = ctor.GetParameters();
// foreach (var p in parameters)
// {
// var result = bindingContext.ValueProvider.GetValue(p.Name);
// }
// //Activator.CreateInstance()
// }
// else
// {
// var props = type
// .GetProperties(BindingFlags.Public | BindingFlags.Instance)
// .Where(x => x.CanWrite)
// .ToList();
//
// foreach (var prop in props)
// {
// var result = bindingContext.ValueProvider.GetValue(prop.Name);
// }
// }
// // bindingContext.ActionContext.RouteData.Values
// return Task.CompletedTask;
// }
// }
22 changes: 4 additions & 18 deletions src/Shiny.Mediator.AspNet/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,36 +274,22 @@ public static void MapResultType<TRequest, TResult>(WebApplication app, Mediator
{
routerBuilder = app.MapPost(
attribute.UriTemplate,
async (
(
[FromServices] IMediator mediator,
[FromBody] TRequest request,
CancellationToken cancellationToken
) =>
{
var result = await mediator
.Request(request, cancellationToken)
.ConfigureAwait(false);
return Results.Ok(result);
}
) => mediator.Request(request, cancellationToken)
);
}
else if (attribute.Method == HttpMethod.Put)
{
routerBuilder = app.MapGet(
attribute.UriTemplate,
async (
(
[FromServices] IMediator mediator,
[FromBody] TRequest request,
CancellationToken cancellationToken
) =>
{
var result = await mediator
.Request(request, cancellationToken)
.ConfigureAwait(false);
return Results.Ok(result);
}
) => mediator.Request(request, cancellationToken)
);
}
else if (attribute.Method == HttpMethod.Get)
Expand Down

0 comments on commit 25f8087

Please sign in to comment.