Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #12

Merged
merged 13 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ManagedCode.Communication.Extensions</RootNamespace>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<!--NuGet-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public sealed class CommandSurrogateConverter : IConverter<Command, CommandSurro
{
public Command ConvertFromSurrogate(in CommandSurrogate surrogate)
{
return new Command(surrogate.Id);
return new Command(surrogate.Id, surrogate.CommandType);
}

public CommandSurrogate ConvertToSurrogate(in Command value)
{
return new CommandSurrogate(value.Id);
return new CommandSurrogate(value.Id, value.CommandType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public sealed class CommandTSurrogateConverter<T> : IConverter<Command<T>, Comma
{
public Command<T> ConvertFromSurrogate(in CommandTSurrogate<T> surrogate)
{
return new Command<T>(surrogate.Id, surrogate.Value);
return new Command<T>(surrogate.Id, surrogate.CommandType, surrogate.Value);
}

public CommandTSurrogate<T> ConvertToSurrogate(in Command<T> value)
{
return new CommandTSurrogate<T>(value.Id, value.Value);
return new CommandTSurrogate<T>(value.Id, value.CommandType, value.Value);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>true</IsPackable>
<RootNamespace>ManagedCode.Communication</RootNamespace>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<!--NuGet-->
Expand All @@ -17,9 +17,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.2.3" />
<PackageReference Include="Microsoft.Orleans.Runtime" Version="7.2.3" />
<PackageReference Include="Microsoft.Orleans.Serialization.Abstractions" Version="7.2.3" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="8.1.0" />
<PackageReference Include="Microsoft.Orleans.Runtime" Version="8.1.0" />
<PackageReference Include="Microsoft.Orleans.Serialization.Abstractions" Version="8.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace ManagedCode.Communication.Surrogates;

// This is the surrogate which will act as a stand-in for the foreign type.
// Surrogates should use plain fields instead of properties for better perfomance.
[Immutable]
[GenerateSerializer]
public struct CollectionResultTSurrogate<T>
Expand All @@ -29,6 +27,4 @@ public CollectionResultTSurrogate(bool isSuccess, T[]? collection, int pageNumbe
[Id(4)] public int TotalItems;
[Id(5)] public Error[]? Errors;
[Id(6)] public Dictionary<string, string>? InvalidObject;
}

// This is a converter which converts between the surrogate and the foreign type.
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ namespace ManagedCode.Communication.Surrogates;
[GenerateSerializer]
public struct CommandSurrogate
{
[Id(0)] public string? Id;
[Id(0)] public string Id;
[Id(1)] public string CommandType;

public CommandSurrogate(string? id)
public CommandSurrogate(string id, string commandType)
{
Id = id;
CommandType = commandType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace ManagedCode.Communication.Surrogates;
[GenerateSerializer]
public struct CommandTSurrogate<T>
{
[Id(0)] public string? Id;
[Id(1)] public T? Value;
[Id(0)] public string Id;
[Id(1)] public string CommandType;
[Id(2)] public T? Value;

public CommandTSurrogate(string? id, T? value)
public CommandTSurrogate(string id, string commandType, T? value)
{
Id = id;
CommandType = commandType;
Value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace ManagedCode.Communication.Surrogates;

// This is the surrogate which will act as a stand-in for the foreign type.
// Surrogates should use plain fields instead of properties for better perfomance.
[Immutable]
[GenerateSerializer]
public struct ErrorSurrogate
Expand All @@ -19,6 +17,4 @@ public ErrorSurrogate(Exception? exception, string message, string? errorCode =
[Id(0)] public string? ErrorCode;
[Id(1)] public string Message;
[Id(2)] public Exception? Exception;
}

// This is a converter which converts between the surrogate and the foreign type.
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace ManagedCode.Communication.Surrogates;

// This is the surrogate which will act as a stand-in for the foreign type.
// Surrogates should use plain fields instead of properties for better perfomance.
[Immutable]
[GenerateSerializer]
public struct ResultSurrogate
Expand All @@ -21,6 +19,4 @@ public ResultSurrogate(bool isSuccess, Error[]? errors, Dictionary<string, strin
[Id(1)] public Error[]? Errors;

[Id(2)] public Dictionary<string, string>? InvalidObject;
}

// This is a converter which converts between the surrogate and the foreign type.
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace ManagedCode.Communication.Surrogates;

// This is the surrogate which will act as a stand-in for the foreign type.
// Surrogates should use plain fields instead of properties for better perfomance.
[Immutable]
[GenerateSerializer]
public struct ResultTSurrogate<T>
Expand All @@ -25,5 +23,3 @@ public ResultTSurrogate(bool isSuccess, T? value, Error[]? errors, Dictionary<st

[Id(3)] public T? Value;
}

// This is a converter which converts between the surrogate and the foreign type.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;

Expand Down Expand Up @@ -47,4 +50,98 @@ public void SucceedPaged()
ok.TotalPages.Should().Be(15000 / 100);
ok.PageSize.Should().Be(100);
}

[Fact]
public void FailWithoutError()
{
var fail = CollectionResult<int>.Fail();
fail.IsSuccess.Should().BeFalse();
fail.IsFailed.Should().BeTrue();
fail.GetError().Should().BeNull();
Assert.Throws<Exception>(() => fail.ThrowIfFail());
Assert.True(fail == false);
Assert.False(fail);
fail.AsTask().Result.IsSuccess.Should().BeFalse();
fail.AsValueTask().Result.IsSuccess.Should().BeFalse();
}

[Fact]
public void FailWithError()
{
var fail = CollectionResult<int>.Fail("Test Error");
fail.IsSuccess.Should().BeFalse();
fail.IsFailed.Should().BeTrue();
fail.GetError().Should().NotBeNull();
fail.GetError()?.Message.Should().Be("Test Error");
Assert.Throws<Exception>(() => fail.ThrowIfFail());
Assert.True(fail == false);
Assert.False(fail);
fail.AsTask().Result.IsSuccess.Should().BeFalse();
fail.AsValueTask().Result.IsSuccess.Should().BeFalse();
}

[Fact]
public void InvalidWithoutMessage()
{
var invalid = CollectionResult<int>.Invalid();
invalid.IsSuccess.Should().BeFalse();
invalid.IsInvalid.Should().BeTrue();
invalid.InvalidObject.Should().NotBeNull();
invalid.InvalidObject?.ContainsKey("message").Should().BeTrue();
invalid.InvalidObject?["message"].Should().Be(nameof(CollectionResult<int>.Invalid));
}

[Fact]
public void InvalidWithMessage()
{
var invalid = CollectionResult<int>.Invalid("Test Invalid");
invalid.IsSuccess.Should().BeFalse();
invalid.IsInvalid.Should().BeTrue();
invalid.InvalidObject.Should().NotBeNull();
invalid.InvalidObject?.ContainsKey("message").Should().BeTrue();
invalid.InvalidObject?["message"].Should().Be("Test Invalid");
}

[Fact]
public void SucceedWithEmptyCollection()
{
var ok = CollectionResult<int>.Succeed(new int[0]);
ok.IsSuccess.Should().BeTrue();
ok.Collection.Length.Should().Be(0);
}

[Fact]
public void FailWithException()
{
var fail = CollectionResult<int>.Fail(new Exception("Test Exception"));
fail.IsSuccess.Should().BeFalse();
fail.GetError()?.Message.Should().Be("Test Exception");
}

[Fact]
public void InvalidWithKeyValue()
{
var invalid = CollectionResult<int>.Invalid("TestKey", "TestValue");
invalid.IsSuccess.Should().BeFalse();
invalid.InvalidObject?.ContainsKey("TestKey").Should().BeTrue();
invalid.InvalidObject?["TestKey"].Should().Be("TestValue");
}

[Fact]
public async Task FromTaskWithException()
{
var task = Task.FromException<IEnumerable<int>>(new Exception("Test Exception"));
var result = await CollectionResult<int>.From(task);
result.IsSuccess.Should().BeFalse();
result.GetError()?.Message.Should().Be("Test Exception");
}

[Fact]
public async Task FromValueTaskWithException()
{
var valueTask = new ValueTask<IEnumerable<int>>(Task.FromException<IEnumerable<int>>(new Exception("Test Exception")));
var result = await CollectionResult<int>.From(valueTask);
result.IsSuccess.Should().BeFalse();
result.GetError()?.Message.Should().Be("Test Exception");
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<IsPackable>false</IsPackable>
<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
<GenerateProgramFile>false</GenerateProgramFile>
Expand All @@ -26,25 +26,25 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Orleans.TestingHost" Version="7.2.3" />
<PackageReference Include="Microsoft.Orleans.TestingHost" Version="8.1.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.4" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading
Loading