Skip to content

Commit fff7822

Browse files
committed
refactoring
1 parent 8992c20 commit fff7822

10 files changed

Lines changed: 178 additions & 165 deletions

File tree

Directory.Build.props

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<PropertyGroup>
44
<Authors>ManagedCode</Authors>
55
<Copyright>Copyright © 2021-$([System.DateTime]::Now.ToString(`yyyy`)) ManagedCode SAS</Copyright>
6-
<RepositoryUrl>https://github.com/managedcode/Communication</RepositoryUrl>
7-
<PackageProjectUrl>https://github.com/managedcode/Communication</PackageProjectUrl>
86
<EmbedUntrackedSources>true</EmbedUntrackedSources>
97
<PublishRepositoryUrl>true</PublishRepositoryUrl>
108
<IncludeSymbols>true</IncludeSymbols>
@@ -13,10 +11,15 @@
1311
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
1412
<PackageIcon>logo.png</PackageIcon>
1513
<PackageLicenseExpression>MIT</PackageLicenseExpression>
14+
<EnablePackageValidation>true</EnablePackageValidation>
1615
<PackageReadmeFile>README.md</PackageReadmeFile>
16+
17+
<RepositoryUrl>https://github.com/managedcode/Communication</RepositoryUrl>
18+
<PackageProjectUrl>https://github.com/managedcode/Communication</PackageProjectUrl>
1719
<Product>Managed Code - Communication</Product>
1820
<Version>1.0.3</Version>
1921
<PackageVersion>1.0.3</PackageVersion>
22+
2023
</PropertyGroup>
2124
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
2225
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
@@ -26,6 +29,9 @@
2629
<None Include="$(SolutionDir)\README.md" Pack="true" Visible="false" PackagePath="\"/>
2730
</ItemGroup>
2831
<ItemGroup>
29-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
32+
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
33+
<PrivateAssets>all</PrivateAssets>
34+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
35+
</PackageReference>
3036
</ItemGroup>
3137
</Project>

ManagedCode.Communication.Tests/DeserializationTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ public void DeserializeValueResult_WithTextJson<T>(Result<T> result)
5656
// Assert
5757
deserialized.Should().BeEquivalentTo(result);
5858
}
59-
59+
6060
public static IEnumerable<object[]> GetResults()
6161
{
62-
yield return new object[] {Result.Succeed()};
63-
yield return new object[] {Result.Fail()};
64-
yield return new object[] {Result.Fail(new Exception("Test exception"))};
65-
yield return new object[] {Result.Fail(new Error<ErrorCode>("Test error", ErrorCode.InvalidState))};
62+
yield return new object[] { Result.Succeed() };
63+
yield return new object[] { Result.Fail() };
64+
yield return new object[] { Result.Fail(new Exception("Test exception")) };
65+
yield return new object[] { Result.Fail(new Error<ErrorCode>("Test error", ErrorCode.InvalidState)) };
6666
}
6767

6868
public static IEnumerable<object[]> GetValueResults()
6969
{
70-
yield return new object[] {Result<int>.Succeed(2)};
71-
yield return new object[] {Result<string>.Succeed("Test string")};
72-
yield return new object[] {Result<int>.Fail()};
73-
yield return new object[] {Result<int>.Fail(new Exception("Test exception"))};
74-
yield return new object[] {Result<int>.Fail(new Error<ErrorCode>("Test error", ErrorCode.InvalidState))};
70+
yield return new object[] { Result<int>.Succeed(2) };
71+
yield return new object[] { Result<string>.Succeed("Test string") };
72+
yield return new object[] { Result<int>.Fail() };
73+
yield return new object[] { Result<int>.Fail(new Exception("Test exception")) };
74+
yield return new object[] { Result<int>.Fail(new Error<ErrorCode>("Test error", ErrorCode.InvalidState)) };
7575
}
7676
}

ManagedCode.Communication.Tests/ManagedCode.Communication.Tests.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
</None>
1818
</ItemGroup>
1919
<ItemGroup>
20-
<PackageReference Include="FluentAssertions" Version="6.5.1" />
21-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
22-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
23-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
24-
<PackageReference Include="System.Text.Json" Version="6.0.6" />
25-
<PackageReference Include="xunit" Version="2.4.1" />
20+
<PackageReference Include="FluentAssertions" Version="6.5.1"/>
21+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0"/>
22+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/>
23+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1"/>
24+
<PackageReference Include="System.Text.Json" Version="6.0.6"/>
25+
<PackageReference Include="xunit" Version="2.4.1"/>
2626
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
2727
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2828
<PrivateAssets>all</PrivateAssets>
@@ -38,7 +38,7 @@
3838
</ItemGroup>
3939

4040
<ItemGroup>
41-
<ProjectReference Include="..\ManagedCode.Communication\ManagedCode.Communication.csproj" />
41+
<ProjectReference Include="..\ManagedCode.Communication\ManagedCode.Communication.csproj"/>
4242
</ItemGroup>
4343

4444
</Project>

ManagedCode.Communication/BaseResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected BaseResult(bool isSuccess)
1414
protected BaseResult(Error<TErrorCode> error)
1515
{
1616
IsSuccess = false;
17-
Errors = new List<Error<TErrorCode>> {error};
17+
Errors = new List<Error<TErrorCode>> { error };
1818
}
1919

2020
protected BaseResult(List<Error<TErrorCode>> errors)
@@ -29,7 +29,6 @@ protected BaseResult(bool isSuccess, List<Error<TErrorCode>> errors)
2929
Errors = errors;
3030
}
3131

32-
3332
public bool IsSuccess { get; }
3433
public bool IsFail => !IsSuccess;
3534
public Error<TErrorCode>? Error => Errors?.FirstOrDefault();

ManagedCode.Communication/ManagedCode.Communication.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</PropertyGroup>
1717

1818
<ItemGroup>
19-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
19+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1"/>
2020
</ItemGroup>
2121

2222
</Project>

ManagedCode.Communication/Result/Result.Execute.cs

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static async Task<Result> Execute(Task task, CancellationToken cancellati
4242
{
4343
return new Result(Error<ErrorCode>.FromException(task.Exception));
4444
}
45-
45+
4646
try
4747
{
4848
await task;
@@ -65,80 +65,4 @@ public static async Task<Result> Execute(Func<Task<Result>> task, CancellationTo
6565
return new Result(Error<ErrorCode>.FromException(e));
6666
}
6767
}
68-
}
69-
70-
public sealed partial class Result<T>
71-
{
72-
public static Result<T> Execute(Func<Result<T>> func)
73-
{
74-
try
75-
{
76-
return func();
77-
}
78-
catch (Exception e)
79-
{
80-
return new Result<T>(Error<ErrorCode>.FromException(e));
81-
}
82-
}
83-
84-
public static Result<T> Execute(Func<T> func)
85-
{
86-
try
87-
{
88-
var result = func();
89-
return Succeed(result);
90-
}
91-
catch (Exception e)
92-
{
93-
return new Result<T>(Error<ErrorCode>.FromException(e));
94-
}
95-
}
96-
97-
public static async Task<Result<T>> Execute(Func<Task<T>> task, CancellationToken cancellationToken = default)
98-
{
99-
try
100-
{
101-
var result = await task();
102-
return Succeed(result);
103-
}
104-
catch (Exception e)
105-
{
106-
return new Result<T>(Error<ErrorCode>.FromException(e));
107-
}
108-
}
109-
110-
public static async Task<Result<T>> Execute(Func<Task<Result<T>>> task, CancellationToken cancellationToken = default)
111-
{
112-
try
113-
{
114-
return await task();
115-
}
116-
catch (Exception e)
117-
{
118-
return new Result<T>(Error<ErrorCode>.FromException(e));
119-
}
120-
}
121-
122-
public static async Task<Result<T>> Execute(Task<T> task, CancellationToken cancellationToken = default)
123-
{
124-
if (task.IsCompleted)
125-
{
126-
return Succeed(task.Result);
127-
}
128-
129-
if (task.IsCanceled || task.IsFaulted)
130-
{
131-
return new Result<T>(Error<ErrorCode>.FromException(task.Exception));
132-
}
133-
134-
try
135-
{
136-
var result = await task;
137-
return Succeed(result);
138-
}
139-
catch (Exception e)
140-
{
141-
return new Result<T>(Error<ErrorCode>.FromException(e));
142-
}
143-
}
14468
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace ManagedCode.Communication;
6+
7+
public sealed partial class Result<T>
8+
{
9+
public static Result<T> Execute(Func<Result<T>> func)
10+
{
11+
try
12+
{
13+
return func();
14+
}
15+
catch (Exception e)
16+
{
17+
return new Result<T>(Error<ErrorCode>.FromException(e));
18+
}
19+
}
20+
21+
public static Result<T> Execute(Func<T> func)
22+
{
23+
try
24+
{
25+
var result = func();
26+
return Succeed(result);
27+
}
28+
catch (Exception e)
29+
{
30+
return new Result<T>(Error<ErrorCode>.FromException(e));
31+
}
32+
}
33+
34+
public static async Task<Result<T>> Execute(Func<Task<T>> task, CancellationToken cancellationToken = default)
35+
{
36+
try
37+
{
38+
var result = await task();
39+
return Succeed(result);
40+
}
41+
catch (Exception e)
42+
{
43+
return new Result<T>(Error<ErrorCode>.FromException(e));
44+
}
45+
}
46+
47+
public static async Task<Result<T>> Execute(Func<Task<Result<T>>> task, CancellationToken cancellationToken = default)
48+
{
49+
try
50+
{
51+
return await task();
52+
}
53+
catch (Exception e)
54+
{
55+
return new Result<T>(Error<ErrorCode>.FromException(e));
56+
}
57+
}
58+
59+
public static async Task<Result<T>> Execute(Task<T> task, CancellationToken cancellationToken = default)
60+
{
61+
if (task.IsCompleted)
62+
{
63+
return Succeed(task.Result);
64+
}
65+
66+
if (task.IsCanceled || task.IsFaulted)
67+
{
68+
return new Result<T>(Error<ErrorCode>.FromException(task.Exception));
69+
}
70+
71+
try
72+
{
73+
var result = await task;
74+
return Succeed(result);
75+
}
76+
catch (Exception e)
77+
{
78+
return new Result<T>(Error<ErrorCode>.FromException(e));
79+
}
80+
}
81+
}

ManagedCode.Communication/Result/Result.Fail.cs renamed to ManagedCode.Communication/Result/Result.T.Fail.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
namespace ManagedCode.Communication;
55

6-
76
public sealed partial class Result<T> : BaseResult<T, ErrorCode>
87
{
98
public static Result<T> Fail()
109
{
1110
return new Result<T>(false);
1211
}
13-
12+
1413
public static Result<T> Fail(T result)
1514
{
1615
return new Result<T>(false);
@@ -25,12 +24,12 @@ public static Result<T> Fail(Error<ErrorCode> error, T value)
2524
{
2625
return new Result<T>(error, value);
2726
}
28-
27+
2928
public static Result<T> Fail(List<Error<ErrorCode>> errors)
3029
{
3130
return new Result<T>(errors);
3231
}
33-
32+
3433
public static Result<T> Fail(List<Error<ErrorCode>> errors, T value)
3534
{
3635
return new Result<T>(errors, value);
@@ -40,7 +39,7 @@ public static Result<T> Fail(Exception? exception)
4039
{
4140
return new Result<T>(Error<ErrorCode>.FromException(exception));
4241
}
43-
42+
4443
public static Result<T> Fail(Exception? exception, T value)
4544
{
4645
return new Result<T>(Error<ErrorCode>.FromException(exception), value);

0 commit comments

Comments
 (0)