Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogard committed Dec 31, 2020
1 parent 8f501cb commit fe89653
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 149 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ csharp_space_between_method_declaration_name_and_open_parenthesis=false
csharp_space_between_method_declaration_parameter_list_parentheses=false
csharp_space_between_parentheses=false
csharp_space_between_square_brackets=false
csharp_style_expression_bodied_accessors=true:suggestion
csharp_style_expression_bodied_constructors=true:suggestion
csharp_style_expression_bodied_methods=true:suggestion
csharp_style_expression_bodied_properties=true:suggestion
csharp_style_var_elsewhere=true:hint
csharp_style_var_for_built_in_types=true:hint
csharp_style_var_when_type_is_apparent=true:hint
Expand Down
141 changes: 0 additions & 141 deletions src/MediatR/Internal/HandlersOrderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,145 +48,4 @@ private static IEnumerable<ObjectDetails> RemoveOverridden(IList<ObjectDetails>
return handlersData.Where(w => !w.IsOverridden);
}
}

internal class ObjectDetails : IComparer<ObjectDetails>
{
public string Name { get; }

public string? AssemblyName { get; }

public string? Location { get; }

public object Value { get; }

public Type Type { get; }

public bool IsOverridden { get; set; }

public ObjectDetails(object value)
{
Value = value;
Type = Value.GetType();
var exceptionHandlerType = value.GetType();

Name = exceptionHandlerType.Name;
AssemblyName = exceptionHandlerType.Assembly.GetName().Name;
Location = exceptionHandlerType.Namespace?.Replace($"{AssemblyName}.", string.Empty);
}

public int Compare(ObjectDetails? x, ObjectDetails? y)
{
if (x == null)
{
return 1;
}

if (y == null)
{
return -1;
}

return CompareByAssembly(x, y) ?? CompareByNamespace(x, y) ?? CompareByLocation(x, y);
}

/// <summary>
/// Compare two objects according to current assembly
/// </summary>
/// <param name="x">First object to compare</param>
/// <param name="y">Second object to compare</param>
/// <returns>
/// An object has a higher priority if it belongs to the current assembly and the other is not;
/// If none of the objects belong to the current assembly, they can be considered equal;
/// If both objects belong to the current assembly, they can't be compared only by this criterion.
/// </returns>
private int? CompareByAssembly(ObjectDetails x, ObjectDetails y)
{
if (x.AssemblyName == AssemblyName && y.AssemblyName != AssemblyName)
{
return -1;
}

if (x.AssemblyName != AssemblyName && y.AssemblyName == AssemblyName)
{
return 1;
}
if (x.AssemblyName != AssemblyName && y.AssemblyName != AssemblyName)
{
return 0;
}

return null;
}

/// <summary>
/// Compare two objects according to current namespace
/// </summary>
/// <param name="x">First object to compare</param>
/// <param name="y">Second object to compare</param>
/// <returns>
/// An object has a higher priority if it belongs to the current/child namespace and the other is not;
/// If both objects belong to the current/child namespace, they can be considered equal;
/// If none of the objects belong to the current/child namespace, they can't be compared by this criterion.
/// </returns>
private int? CompareByNamespace(ObjectDetails x, ObjectDetails y)
{
if (Location is null || x.Location is null || y.Location is null)
{
return 0;
}

if (x.Location.StartsWith(Location, StringComparison.Ordinal) && !y.Location.StartsWith(Location, StringComparison.Ordinal))
{
return -1;
}

if (!x.Location.StartsWith(Location, StringComparison.Ordinal) && y.Location.StartsWith(Location, StringComparison.Ordinal))
{
return 1;
}
if (x.Location.StartsWith(Location, StringComparison.Ordinal) && y.Location.StartsWith(Location, StringComparison.Ordinal))
{
return 0;
}

return null;
}

/// <summary>
/// Compare two objects according to location in the assembly
/// </summary>
/// <param name="x">First object to compare</param>
/// <param name="y">Second object to compare</param>
/// <returns>
/// An object has a higher priority if it location is part of the current location and the other is not;
/// If both objects are part of the current location, the closest has higher priority;
/// If none of the objects are part of the current location, they can be considered equal.
/// </returns>
private int CompareByLocation(ObjectDetails x, ObjectDetails y)
{
if (Location is null || x.Location is null || y.Location is null)
{
return 0;
}

if (Location.StartsWith(x.Location, StringComparison.Ordinal) && !Location.StartsWith(y.Location, StringComparison.Ordinal))
{
return -1;
}

if (!Location.StartsWith(x.Location, StringComparison.Ordinal) && Location.StartsWith(y.Location, StringComparison.Ordinal))
{
return 1;
}
if (x.Location.Length > y.Location.Length)
{
return -1;
}
if (x.Location.Length < y.Location.Length)
{
return 1;
}
return 0;
}
}
}
146 changes: 146 additions & 0 deletions src/MediatR/Internal/ObjectDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;

namespace MediatR.Internal
{
internal class ObjectDetails : IComparer<ObjectDetails>
{
public string Name { get; }

public string? AssemblyName { get; }

public string? Location { get; }

public object Value { get; }

public Type Type { get; }

public bool IsOverridden { get; set; }

public ObjectDetails(object value)
{
Value = value;
Type = Value.GetType();
var exceptionHandlerType = value.GetType();

Name = exceptionHandlerType.Name;
AssemblyName = exceptionHandlerType.Assembly.GetName().Name;
Location = exceptionHandlerType.Namespace?.Replace($"{AssemblyName}.", string.Empty);
}

public int Compare(ObjectDetails? x, ObjectDetails? y)
{
if (x == null)
{
return 1;
}

if (y == null)
{
return -1;
}

return CompareByAssembly(x, y) ?? CompareByNamespace(x, y) ?? CompareByLocation(x, y);
}

/// <summary>
/// Compare two objects according to current assembly
/// </summary>
/// <param name="x">First object to compare</param>
/// <param name="y">Second object to compare</param>
/// <returns>
/// An object has a higher priority if it belongs to the current assembly and the other is not;
/// If none of the objects belong to the current assembly, they can be considered equal;
/// If both objects belong to the current assembly, they can't be compared only by this criterion.
/// </returns>
private int? CompareByAssembly(ObjectDetails x, ObjectDetails y)
{
if (x.AssemblyName == AssemblyName && y.AssemblyName != AssemblyName)
{
return -1;
}

if (x.AssemblyName != AssemblyName && y.AssemblyName == AssemblyName)
{
return 1;
}
if (x.AssemblyName != AssemblyName && y.AssemblyName != AssemblyName)
{
return 0;
}

return null;
}

/// <summary>
/// Compare two objects according to current namespace
/// </summary>
/// <param name="x">First object to compare</param>
/// <param name="y">Second object to compare</param>
/// <returns>
/// An object has a higher priority if it belongs to the current/child namespace and the other is not;
/// If both objects belong to the current/child namespace, they can be considered equal;
/// If none of the objects belong to the current/child namespace, they can't be compared by this criterion.
/// </returns>
private int? CompareByNamespace(ObjectDetails x, ObjectDetails y)
{
if (Location is null || x.Location is null || y.Location is null)
{
return 0;
}

if (x.Location.StartsWith(Location, StringComparison.Ordinal) && !y.Location.StartsWith(Location, StringComparison.Ordinal))
{
return -1;
}

if (!x.Location.StartsWith(Location, StringComparison.Ordinal) && y.Location.StartsWith(Location, StringComparison.Ordinal))
{
return 1;
}
if (x.Location.StartsWith(Location, StringComparison.Ordinal) && y.Location.StartsWith(Location, StringComparison.Ordinal))
{
return 0;
}

return null;
}

/// <summary>
/// Compare two objects according to location in the assembly
/// </summary>
/// <param name="x">First object to compare</param>
/// <param name="y">Second object to compare</param>
/// <returns>
/// An object has a higher priority if it location is part of the current location and the other is not;
/// If both objects are part of the current location, the closest has higher priority;
/// If none of the objects are part of the current location, they can be considered equal.
/// </returns>
private int CompareByLocation(ObjectDetails x, ObjectDetails y)
{
if (Location is null || x.Location is null || y.Location is null)
{
return 0;
}

if (Location.StartsWith(x.Location, StringComparison.Ordinal) && !Location.StartsWith(y.Location, StringComparison.Ordinal))
{
return -1;
}

if (!Location.StartsWith(x.Location, StringComparison.Ordinal) && Location.StartsWith(y.Location, StringComparison.Ordinal))
{
return 1;
}
if (x.Location.Length > y.Location.Length)
{
return -1;
}
if (x.Location.Length < y.Location.Length)
{
return 1;
}
return 0;
}
}
}
6 changes: 2 additions & 4 deletions src/MediatR/Internal/RequestHandlerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ internal class RequestHandlerWrapperImpl<TRequest, TResponse> : RequestHandlerWr
where TRequest : IRequest<TResponse>
{
public override Task<object?> Handle(object request, CancellationToken cancellationToken,
ServiceFactory serviceFactory)
{
return Handle((IRequest<TResponse>)request, cancellationToken, serviceFactory)
ServiceFactory serviceFactory) =>
Handle((IRequest<TResponse>)request, cancellationToken, serviceFactory)
.ContinueWith(t =>
{
if (t.IsFaulted && t.Exception?.InnerException is not null)
Expand All @@ -54,7 +53,6 @@ internal class RequestHandlerWrapperImpl<TRequest, TResponse> : RequestHandlerWr
}
return (object?)t.Result;
}, cancellationToken);
}

public override Task<TResponse> Handle(IRequest<TResponse> request, CancellationToken cancellationToken,
ServiceFactory serviceFactory)
Expand Down
6 changes: 2 additions & 4 deletions src/MediatR/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,13 @@ public Task Publish<TNotification>(TNotification notification, CancellationToken
return PublishNotification(notification, cancellationToken);
}

public Task Publish(object notification, CancellationToken cancellationToken = default)
{
return notification switch
public Task Publish(object notification, CancellationToken cancellationToken = default) =>
notification switch
{
null => throw new ArgumentNullException(nameof(notification)),
INotification instance => PublishNotification(instance, cancellationToken),
_ => throw new ArgumentException($"{nameof(notification)} does not implement ${nameof(INotification)}")
};
}

/// <summary>
/// Override in a derived class to control how the tasks are awaited. By default the implementation is a foreach and await of each handler
Expand Down

0 comments on commit fe89653

Please sign in to comment.