-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31b0c8c
commit 297cecf
Showing
12 changed files
with
387 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
src/DotNetCore.CAP/Abstractions/ModelBinding/ModelBindingContext.cs
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/DotNetCore.CAP/Abstractions/ModelBinding/ModelBindingResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace DotNetCore.CAP.Abstractions.ModelBinding | ||
{ | ||
/// <summary> | ||
/// Contains the result of model binding. | ||
/// </summary> | ||
public struct ModelBindingResult | ||
{ | ||
/// <summary> | ||
/// Creates a <see cref="ModelBindingResult"/> representing a failed model binding operation. | ||
/// </summary> | ||
/// <returns>A <see cref="ModelBindingResult"/> representing a failed model binding operation.</returns> | ||
public static ModelBindingResult Failed() | ||
{ | ||
return new ModelBindingResult(model: null, isSuccess: false); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a <see cref="ModelBindingResult"/> representing a successful model binding operation. | ||
/// </summary> | ||
/// <param name="model">The model value. May be <c>null.</c></param> | ||
/// <returns>A <see cref="ModelBindingResult"/> representing a successful model bind.</returns> | ||
public static ModelBindingResult Success(object model) | ||
{ | ||
return new ModelBindingResult(model, isSuccess: true); | ||
} | ||
|
||
private ModelBindingResult(object model, bool isSuccess) | ||
{ | ||
Model = model; | ||
IsSuccess = isSuccess; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the model associated with this context. | ||
/// </summary> | ||
public object Model { get; } | ||
|
||
public bool IsSuccess { get; } | ||
|
||
public override string ToString() | ||
{ | ||
if (IsSuccess) | ||
{ | ||
return $"Success '{Model}'"; | ||
} | ||
else | ||
{ | ||
return $"Failed"; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace DotNetCore.CAP.Internal | ||
{ | ||
internal struct HashCodeCombiner | ||
{ | ||
private long _combinedHash64; | ||
|
||
public int CombinedHash | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get { return _combinedHash64.GetHashCode(); } | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private HashCodeCombiner(long seed) | ||
{ | ||
_combinedHash64 = seed; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Add(IEnumerable e) | ||
{ | ||
if (e == null) | ||
{ | ||
Add(0); | ||
} | ||
else | ||
{ | ||
var count = 0; | ||
foreach (object o in e) | ||
{ | ||
Add(o); | ||
count++; | ||
} | ||
Add(count); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static implicit operator int(HashCodeCombiner self) | ||
{ | ||
return self.CombinedHash; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Add(int i) | ||
{ | ||
_combinedHash64 = ((_combinedHash64 << 5) + _combinedHash64) ^ i; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Add(string s) | ||
{ | ||
var hashCode = (s != null) ? s.GetHashCode() : 0; | ||
Add(hashCode); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Add(object o) | ||
{ | ||
var hashCode = (o != null) ? o.GetHashCode() : 0; | ||
Add(hashCode); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Add<TValue>(TValue value, IEqualityComparer<TValue> comparer) | ||
{ | ||
var hashCode = value != null ? comparer.GetHashCode(value) : 0; | ||
Add(hashCode); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static HashCodeCombiner Start() | ||
{ | ||
return new HashCodeCombiner(0x1505L); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using DotNetCore.CAP.Abstractions.ModelBinding; | ||
using DotNetCore.CAP.Infrastructure; | ||
|
||
namespace DotNetCore.CAP.Internal | ||
{ | ||
public class ComplexTypeModelBinder : IModelBinder | ||
{ | ||
private readonly ParameterInfo _parameterInfo; | ||
|
||
public ComplexTypeModelBinder(ParameterInfo parameterInfo) | ||
{ | ||
_parameterInfo = parameterInfo; | ||
} | ||
|
||
public Task<ModelBindingResult> BindModelAsync(string content) | ||
{ | ||
try | ||
{ | ||
var type = _parameterInfo.ParameterType; | ||
var value = Helper.FromJson(content, type); | ||
return Task.FromResult(ModelBindingResult.Success(value)); | ||
} | ||
catch (Exception) | ||
{ | ||
return Task.FromResult(ModelBindingResult.Failed()); | ||
|
||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.