Skip to content

Commit f17e954

Browse files
committed
Add domain guard validation
1 parent b4ca1ae commit f17e954

File tree

7 files changed

+10
-9
lines changed

7 files changed

+10
-9
lines changed

src/Api/DummiesDatabase.db

0 Bytes
Binary file not shown.

src/Application/UseCases/Queries/GetDummy/GetDummyQueryHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public GetDummyQueryHandler(IDummyRepository dummyRepository)
1616

1717
public async Task<GetDummyQueryResponse> Handle(GetDummyQuery query, CancellationToken cancellationToken)
1818
{
19-
var dummyId = new DummyId(Guid.Parse(query.Id));
19+
var dummyId = new DummyId(query.Id);
2020
var dummy = await _dummyRepository.GetDummyByIdAsync(dummyId, cancellationToken);
2121
if (dummy is null)
2222
{

src/Application/UseCases/Queries/GetDummy/GetDummyQueryValidator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class GetDummyQueryValidator : AbstractValidator<GetDummyQuery>
77
public GetDummyQueryValidator()
88
{
99
RuleFor(x => x.Id)
10-
.NotEmpty()
11-
.Must(x => Guid.TryParse(x, out var _));
10+
.NotEmpty();
1211
}
1312
}

src/Domain/Models/Entities/Dummy.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace Domain.Models.Entities;
44

55
public sealed class Dummy
66
{
7-
public DummyId Id { get; private init; }
8-
public DummyName Name { get; private init; }
7+
public DummyId Id { get; }
8+
public DummyName Name { get; }
99

1010
private Dummy(DummyId id, DummyName name)
1111
{
@@ -15,6 +15,8 @@ private Dummy(DummyId id, DummyName name)
1515

1616
public static Dummy CreateDummy(DummyId id, DummyName name)
1717
{
18+
ArgumentException.ThrowIfNullOrWhiteSpace(id.Value);
19+
ArgumentException.ThrowIfNullOrWhiteSpace(name.Value);
1820
var dummy = new Dummy(id, name);
1921
return dummy;
2022
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Domain.Models.ValueObjects;
22

3-
public sealed record DummyId(Guid Value)
3+
public sealed record DummyId(string Value)
44
{
5-
public static implicit operator DummyId(Guid id) => new(id);
5+
public static implicit operator DummyId(string id) => new(id);
66
}

src/Infrastructure/Persistence/Configurations/DummyConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ public void Configure(EntityTypeBuilder<Dummy> builder)
2525
});
2626
}
2727

28-
private static Dummy CreateDummy(Guid id) => Dummy.CreateDummy(id, $"{id:N}");
28+
private static Dummy CreateDummy(Guid id) => Dummy.CreateDummy($"id-{id:N}", $"name-{id:N}");
2929
}

src/Infrastructure/Persistence/Repositories/DummyRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task<ICollection<Dummy>> SearchDummiesAsync(string keyword, Cancell
3838

3939
public async Task<Dummy> AddDummyAsync(DummyName name, CancellationToken cancellationToken)
4040
{
41-
var id = new DummyId(Guid.NewGuid());
41+
var id = new DummyId($"{Guid.NewGuid():N}");
4242
var dummy = Dummy.CreateDummy(id, name);
4343
await _context.Set<Dummy>().AddAsync(dummy, cancellationToken);
4444
await _context.SaveChangesAsync(cancellationToken);

0 commit comments

Comments
 (0)