簡體   English   中英

在將 Mediatr 用於 CQRS 時,如果屬性具有默認值,是否在未提及時添加自身

[英]While using Mediatr for CQRS, if a property has a default value, does it add itself when not mentioning it

在我的 API 中,我將 Mediatr 用於我的 CQRS,將 EF Core 用於數據庫訪問和實體配置。

我給了我的Spending實體這樣的配置:

public class SpendingConfiguration : IEntityTypeConfiguration<Spending>
    {
        public void Configure(EntityTypeBuilder<Spending> builder)
        {
            builder.Property(p => p.Cost1)
                .HasDefaultValue(6.50)
                .IsRequired();

            builder.Property(p => p.Cost2)
                .HasDefaultValue(6.50)
                .IsRequired();

            builder.Property(p => p.Cost3)
                .HasDefaultValue(6.50)
                .IsRequired();

            builder.Property(p => p.CurrentSpending)
                .HasDefaultValue(0.0)
                .IsRequired();

            builder.Property(p => p.SpendingLimit)
                .IsRequired();

            builder.Property(p => p.CreatedDateTime)
                .HasDefaultValueSql("getutcdate()");
        }
    }

當我發送AddSpendingCommand時,我需要使用 Mediatr 創建一個AddSpendingCommand和一個AddSpendingCommandHandler

AddSpendingCommand看起來像這樣:

public class AddSpendingCommand : IRequest<Guid>
    {
        // ... other properties
        public string CurrentSpending { get; set; }
        public string SpendingLimit { get; set; }
    }

AddSpendingCommandHandler是這樣的:

public class AddSpendingCommandHandler
        : IRequestHandler<AddSpendingCommand, Guid>
    {
        // variables declaration

        public AddSpendingCommandHandler(IMapper mapper,
            ISpendingRepository repository)
        {
            // ctor stuff
        }

        public async Task<Guid> Handle(
            AddSpendingCommand request, CancellationToken cancellationToken)
        {
            // validation stuff

            var convertedSpending = new Spending
            {
                CurrentSpending = double.Parse(request.CurrentSpending),
                SpendingLimit = double.Parse(request.SpendingLimit)
            };

            // mapping stuff

            return spending.SpendingId;
        }
    }

我的問題是,如您所見,我已經為Spending實體的某些屬性添加了默認值 在這種情況下,我是否仍需要將那些具有默認值的屬性添加AddSpendingCommand方法並手動將它們 map 添加到AddSpendingCommandHandlerconvertedSpending object 或者我可以繞過它,只需添加我想要更改的屬性,而不是添加屬性AddSpendingCommand中的默認值而不是convertedSpending object 中的默認值,無論如何,這些具有默認值的屬性將在我運行spending = mapper.Map<Spending>(convertedSpending);使用它們的默認值自行添加 到 map 他們到他們的 DTO?

我假設CurrentSpendingSpendingLimitAddSpendingCommand中的字符串,以便您可以對它們進行一些適當的驗證,但目的是當它們不是數字時它們應該無效。

然后有兩個問題:在哪里將字符串解析為 double,以及在哪里將命令 object 到您的域 object map。對於在哪里實現這些問題沒有硬性規定,但我會告訴你我做了什么在過去。

對於將字符串解析為double的第一個問題,您可以將它們放在命令中,因為這樣更能體現其意圖:

public class AddSpendingCommand : IRequest<Guid>
{
    // ... other properties
    public string CurrentSpending { get; set; }
    public string SpendingLimit { get; set; }

    // simple implementation; you can put checks here
    internal double CurrentSpendingParsed => double.Parse(CurrentSpending);
    internal double SpendingLimitParsed => double.Parse(SpendingLimit);
}

或者,如果您有默認值,例如在Constants文件中聲明:

public class AddSpendingCommand : IRequest<Guid>
{
    // ... other properties
    public string CurrentSpending { get; set; }
    public string SpendingLimit { get; set; }

    internal double CurrentSpendingParsed => double.TryParse(CurrentSpending, out double parsed) ? parsed : Constants.CurrentSpendingDefault;
    internal double SpendingLimitParsed => double.TryParse(SpendingLimit, out double parsed) ? parsed : Constants.SpendingLimitDefault;
}

對於將命令 object 映射到您的域 object 的第二個問題,您可以將其放入您的映射器中。 從技術上講,您還可以將解析邏輯放在映射器中,但這會使它變得比需要的更復雜。 它使映射器 object 承擔比它已經擁有的(映射)更多的責任(解析)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM