簡體   English   中英

自動映射並使用DDD驗證配置

[英]Automapper and validate configuration with DDD

我在DDD方法中有簡單的域模型,因此創建實例的唯一方法是使用構造函數。

public CashCaseDifference(
        double amount,
        double originalAmount,
        double originalFcyAmount,
        CashCaseDifferenceCurrency currency,
        CashCaseDifferenceSource source,
        CashCaseDifferenceType type)
    {
        this.Amount = amount;
        this.OriginalAmount = originalAmount;
        this.OriginalFcyAmount = originalFcyAmount;
        this.Currency = currency;
        this.Source = source;
        this.Type = type;
    }

當我嘗試對配置進行單元測試時,我遇到了問題。

映射配置文件:

this.CreateMap<CashCaseOut, CashCaseDifference>()
                .ConstructUsing(source => new CashCaseDifference(
                    source.Amount.GetValueOrDefault(),
                    source.OriginalAmount.GetValueOrDefault(),
                    source.OriginalFcyAmount.GetValueOrDefault(),
                    (CashCaseDifferenceCurrency) source.Currency.GetValueOrDefault(),
                    (CashCaseDifferenceSource) source.SourceOfDifference.GetValueOrDefault(),
                    (CashCaseDifferenceType) source.Type.GetValueOrDefault()));

而我的單元測試方法:

  Mapper.Initialize(
            cfg => { cfg.AddProfile(new CashCaseOutToCashCaseDifference()); });
        Mapper.AssertConfigurationIsValid(););

它返回未映射屬性的錯誤:

Unmapped members were found. Review the types and members below.

“添加自定義映射表達式,忽略,添加自定義解析器或修改源/目標類型對於不匹配的構造函數,請添加無參數ctor,添加可選參數或映射所有構造函數參數”

Unmapped properties: Source

編輯:

施工效果不佳。

      Mapper.Initialize(cfg =>
        {
            cfg.AddProfile(new CashCaseOutToCashCaseDifference());
        });
        Mapper.AssertConfigurationIsValid();

  public CashCaseOutToCashCaseDifference()
    {
        this.CreateMap<
                CashDiffMS.Client.Models.CashCaseOut,
                Core.CashCases.Domain.Differences.CashCaseDifference>()
            .ForCtorParam("amount", opt => opt.MapFrom(src => src.Amount))
            .ForCtorParam("originalAmount", opt => opt.MapFrom(src => src.OriginalAmount))
            .ForCtorParam("originalFcyAmount", opt => opt.MapFrom(src => src.OriginalFcyAmount))
            .ForCtorParam("currency", opt => opt.MapFrom(src => src.Currency))
            .ForCtorParam("source", opt => opt.MapFrom(src => src.SourceOfDifference))
            .ForCtorParam("type", opt => opt.MapFrom(src => src.Type));
    }

public CashCaseDifference(
        double amount,
        double originalAmount,
        double originalFcyAmount,
        CashCaseDifferenceCurrency currency,
        CashCaseDifferenceSource source,
        CashCaseDifferenceType type)
    {
        this.Amount = amount;
        this.OriginalAmount = originalAmount;
        this.OriginalFcyAmount = originalFcyAmount;
        this.Currency = currency;
        this.Source = source;
        this.Type = type;
    }

結果是相同的:未Unmapped properties: Source

那是因為您正在手動進行映射。 您應該讓AM來做。 文檔在這里

暫無
暫無

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

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