Skip to content

Commit c004ba1

Browse files
committed
minimal api fails with IParsableValueOf
1 parent 31a1e56 commit c004ba1

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1+
using System.ComponentModel;
12
using System.Diagnostics.CodeAnalysis;
23

34
namespace ValueOf.Extensions.Examples;
45

5-
public interface IValueOfParsable<TU, T> : IParsable<T> where T : ValueOf<TU, T>, IParsable<T>, new()
6+
public interface IParsableValueOf<TU, T> : IParsable<T> where T : ValueOf<TU, T>, IParsable<T>, new()
67
{
7-
public static T Parse(string s, IFormatProvider? provider)
8+
static T IParsable<T>.Parse(string s, IFormatProvider? provider)
89
{
9-
throw new NotImplementedException();
10+
var converter = TypeDescriptor.GetConverter(typeof(T));
11+
return (T)converter.ConvertFrom(s)!;
1012
}
1113

12-
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out T result)
14+
static bool IParsable<T>.TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider,
15+
[MaybeNullWhen(false)] out T result)
1316
{
14-
throw new NotImplementedException();
17+
result = default(T);
18+
var converter = TypeDescriptor.GetConverter(typeof(T));
19+
if (converter.CanConvertFrom(typeof(string)))
20+
{
21+
result = (T)converter.ConvertFrom(s)!;
22+
return true;
23+
}
24+
25+
return false;
1526
}
1627
}
1728

18-
public sealed class UserId : ValueOf<int, UserId>, IValueOfParsable<int, UserId>
29+
public sealed class UserId : ValueOf<int, UserId>, IParsableValueOf<int, UserId>
1930
{
2031
protected override void Validate()
2132
{
2233
var userId = Value;
2334
if (userId <= 0)
2435
throw new ArgumentOutOfRangeException(nameof(userId) + $" must be positive integer but is: {userId}");
2536
}
26-
2737
}

ValueOf.Extensions.Examples/Program.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,16 @@
5050

5151
app.MapControllers();
5252

53-
app.MapGet("/searchUsers",
54-
async ([FromServices] DemoDbContext _dbContext, [FromQuery] UserId? userId, [FromQuery] EmailAddress? email) =>
53+
app.MapGet("/searchUsers", async ([FromServices] DemoDbContext dbContext, [FromQuery] UserId? userId) =>
54+
{
55+
IQueryable<User> q = dbContext.Users;
56+
if (userId != null)
5557
{
56-
IQueryable<User> q = _dbContext.Users;
57-
if (userId != null)
58-
{
59-
q = q.Where(u => u.Id == userId);
60-
}
61-
62-
if (email != null)
63-
{
64-
q = q.Where(u => u.Email == email);
65-
}
58+
q = q.Where(u => u.Id == userId);
59+
}
6660

67-
return q.ToList();
68-
});
61+
return q.ToList();
62+
});
6963

7064

7165
app.Run();

0 commit comments

Comments
 (0)