Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Better checking of baseRequestType and checking assumptions with Debu…
…g.Assert instead of writing them as comments
  • Loading branch information
alexandruchirita4192 committed May 20, 2022
commit ac4a49ee054c62152227e48741fbe03f28c78082
15 changes: 10 additions & 5 deletions samples/MediatR.Examples.Windsor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace MediatR.Examples.Windsor;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
Expand Down Expand Up @@ -61,8 +62,7 @@ private static IMediator BuildMediator(WrappingWriter writer)
|| !service.IsConstructedGenericType
|| !(service.GetGenericTypeDefinition()
?.IsAssignableTo(typeof(IRequestExceptionHandler<,,>)) ?? false)
|| genericArguments.Length != 3
|| !(genericArguments[0].BaseType?.IsClass ?? false))
|| genericArguments.Length != 3)
{
return resolvedType;
}
Expand All @@ -71,9 +71,10 @@ private static IMediator BuildMediator(WrappingWriter writer)
var baseRequestType = genericArguments[0].BaseType;
var response = genericArguments[1];
var exceptionType = genericArguments[2];

// Check if the base request type is valid
if (!baseRequestType.IsClass
if (baseRequestType == null
|| !baseRequestType.IsClass
|| baseRequestType == typeof(object)
|| ((!baseRequestType.GetInterfaces()
?.Any(i => i.IsAssignableFrom(typeof(IRequest<>)))) ?? true))
Expand All @@ -84,9 +85,13 @@ private static IMediator BuildMediator(WrappingWriter writer)
var exceptionHandlerInterfaceType = typeof(IRequestExceptionHandler<,,>).MakeGenericType(baseRequestType, response, exceptionType);
var enumerableExceptionHandlerInterfaceType = typeof(IEnumerable<>).MakeGenericType(exceptionHandlerInterfaceType);

// This is assumed Array because this method calls ResolveAll when a IEnumerable<> is passed as argument
var firstArray = serviceFactory.Invoke(enumerableExceptionHandlerInterfaceType) as Array;
Debug.Assert(firstArray != null, $"Array '{nameof(firstArray)}' should not be null because this method calls ResolveAll when a {typeof(IEnumerable<>).FullName} " +
$"is passed as argument in argument named '{nameof(type)}'");

var secondArray = resolvedType is Array ? resolvedType as Array : new[] { resolvedType };
Debug.Assert(secondArray != null, $"Array '{nameof(secondArray)}' should not be null because '{nameof(resolvedType)}' is an array or created as an array");

var resultArray = Array.CreateInstance(typeof(object), firstArray.Length + secondArray.Length);
Array.Copy(firstArray, resultArray, firstArray.Length);
Array.Copy(secondArray, 0, resultArray, firstArray.Length, secondArray.Length);
Expand Down