Skip to content

Commit

Permalink
#255 - Serialization version using converters.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Mar 20, 2020
1 parent 234ee2c commit d81315a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/Money.UI.Blazor/Bootstrap/BootstrapTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Money.Commands;
using Money.Models.Queries;
using Money.Services;
using Money.Services.Converters;
using Neptuo;
using Neptuo.Activators;
using Neptuo.Bootstrap;
Expand Down Expand Up @@ -113,6 +114,7 @@ private void Domain(Json json)
.AddStringTo<int>(Int32.TryParse)
.AddStringTo<decimal>(Decimal.TryParse)
.AddStringTo<bool>(Boolean.TryParse)
.Add(new VersionConverter())
.AddEnumSearchHandler(false);
//.AddJsonEnumSearchHandler()
//.AddJsonPrimitivesSearchHandler()
Expand Down
4 changes: 2 additions & 2 deletions src/Money.UI.Blazor/Components/AppVersion.razor
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<small class="text-muted">
<span>Client</span>
<span>
<Version Value="@Client" />
<ConvertToString Value="@Client" />
</span>

@if (Api != null)
{
<span>Api</span>
<span>
<Version Value="@Api" />
<ConvertToString Value="@Api" />
</span>
}
</small>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Neptuo;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -9,21 +10,15 @@

namespace Money.Components
{
public class Version : ComponentBase
public class ConvertToString<T> : ComponentBase
{
[Parameter]
public System.Version Value { get; set; }
public T Value { get; set; }

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
if (Value != null)
{
string text = Value.Revision == 0
? Value.ToString(3)
: Value.ToString(4);

builder.AddContent(0, $"v{text}");
}
if (Converts.Try(Value, out string stringValue))
builder.AddContent(0, stringValue);
}
}
}
4 changes: 2 additions & 2 deletions src/Money.UI.Blazor/Pages/VersionSupport.razor
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ else
Server (API)
</td>
<td>
<Version Value="@ApiVersion" />
<ConvertToString Value="@ApiVersion" />
</td>
</tr>

Expand All @@ -48,7 +48,7 @@ else
Client
</td>
<td>
<Version Value="@ClientVersion" />
<ConvertToString Value="@ClientVersion" />
</td>
</tr>
}
Expand Down
28 changes: 28 additions & 0 deletions src/Money.UI.Blazor/Services/Converters/VersionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Neptuo.Converters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Money.Services.Converters
{
public class VersionConverter : DefaultConverter<Version, string>
{
public override bool TryConvert(Version sourceValue, out string targetValue)
{
targetValue = null;
if (sourceValue != null)
{
string text = sourceValue.Revision == 0
? sourceValue.ToString(3)
: sourceValue.ToString(4);

targetValue = $"v{text}";
}

return true;
}
}
}

0 comments on commit d81315a

Please sign in to comment.