Skip to content

Commit

Permalink
Small refactoring #44
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Feb 20, 2024
1 parent 9c0fa86 commit 25867c5
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Expand Up @@ -140,8 +140,8 @@ csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:suggestion
csharp_style_unused_value_assignment_preference = discard_variable:none
csharp_style_unused_value_expression_statement_preference = discard_variable:none

# 'using' directive preferences
csharp_using_directive_placement = outside_namespace:suggestion
Expand Down
1 change: 1 addition & 0 deletions OperationResults.sln
Expand Up @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
src\Directory.Build.props = src\Directory.Build.props
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{40EA0531-BCD9-4D33-9114-D1BB9EA7DDBA}"
Expand Down
Expand Up @@ -48,15 +48,16 @@ public async Task<Result<Person>> GetAsync(Guid id)
public async Task<Result<PersonWithImage>> GetWithImageAsync(Guid id)
{
var personResult = await GetAsync(id);

if (!personResult) // A shortcut to !personResult.Success
{
// The person operation failed
return Result.Fail(personResult.FailureReason);
}

var person = personResult.Content!;

var imageResult = await imageService.GetImageAsync();
if (imageResult.TryGet(out var imageFileContent) && imageFileContent is not null)
if (imageResult.TryGetContent(out var imageFileContent) && imageFileContent is not null)
{
// The image operation succeeded, return person with image
var personWithImage = new PersonWithImage
Expand Down
Expand Up @@ -3,5 +3,6 @@ namespace OperationResults.Sample.Shared.Models;
public class PersonWithImage
{
public Person Person { get; set; }

public byte[] Image { get; set; }
}
Expand Up @@ -33,6 +33,5 @@
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
</Project>
Expand Up @@ -33,6 +33,5 @@
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion src/OperationResults/IGenericResult{OfT}.cs
@@ -1,8 +1,10 @@
using System.Diagnostics.CodeAnalysis;

namespace OperationResults;

public interface IGenericResult<T> : IGenericResult
{
public T? Content { get; }

public bool TryGet(out T? value);
public bool TryGetContent([NotNullWhen(returnValue: true)] T? content);
}
1 change: 0 additions & 1 deletion src/OperationResults/OperationResults.csproj
Expand Up @@ -26,7 +26,6 @@
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
6 changes: 4 additions & 2 deletions src/OperationResults/Result{OfT}.cs
@@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;

namespace OperationResults;

public class Result<T> : IGenericResult<T>
Expand Down Expand Up @@ -29,9 +31,9 @@ internal Result(bool success = true, T? content = default, int failureReason = F
ValidationErrors = validationErrors;
}

public bool TryGet(out T? value)
public bool TryGetContent([NotNullWhen(returnValue: true)] T? content)
{
value = Content;
content = Content;
return Success;
}

Expand Down

0 comments on commit 25867c5

Please sign in to comment.