andrewlock / StronglyTypedId

A Rosyln-powered generator for strongly-typed IDs
MIT License
1.5k stars 78 forks source link

Default ID Value Issue with StronglyTypedId #134

Closed foxminchan closed 4 months ago

foxminchan commented 4 months ago

Problem:

When attempting to retrieve data using the GetListQueryHandler, the system consistently returns the default ID 00000000-0000-0000-0000-000000000000. This issue introduces confusion and has the potential to lead to errors in data interpretation.

Code snippet:

GetListQueryHandler.cs

public sealed class GetListQueryHandler(Repository<Category> repository)
    : IQueryHandler<GetListQuery, Result<List<CategoryVm>>>
{
    public async Task<Result<List<CategoryVm>>> Handle(GetListQuery request, CancellationToken cancellationToken)
    {
        var result = await repository.ListAsync(cancellationToken);

        return Result<List<CategoryVm>>.Success([
            ..result.Select(c => new CategoryVm(
                c.Id,
                c.Name,
                c.Description
            ))
        ]);
    }
}

Category.cs

public sealed class Category : EntityBase, IAggregateRoot
{
    /// <summary>
    ///     EF mapping constructor
    /// </summary>
    public Category()
    {
    }

    public Category(string title, string? description)
    {
        Name = Guard.Against.NullOrEmpty(title);
        Description = description;
    }

    public CategoryId Id { get; set; } = new(Guid.NewGuid());
    public string? Name { get; set; }
    public string? Description { get; set; }
}

[StronglyTypedId]
public readonly partial struct CategoryId;

CategoryVm.cs

public sealed record CategoryVm(
    CategoryId Id,
    string? Name,
    string? Description
);

Screenshots

Data in database

image

Data when response

image