我的資料類的映射有問題。它包含 164 個屬性,這些屬性填充了從soap web 服務接收到的資料。不要質疑屬性計數,這是一個非常古老的預定義 .csv 類資料方案,我無法更改。
據我所知,現在一切正常,但是由于有這么多屬性并且我明確設定了每個屬性(例如 data.a1 = import.a1 OR data.a1 = export.a1)我有一大塊重復的分配代碼。兩種 Web 服務都提供相同的 164 個欄位以及一些對兩個類不同的附加欄位。我想我只需要映射 164 個相同的欄位,并且可以忽略/丟棄其他欄位。
解決這個問題的正確方法是什么?我想過在建構式中使用“物件”或其他東西作為資料型別,以便我可以從兩個服務傳遞資料,但它似乎不像我想象的那樣作業。
任何建議將不勝感激。
/// <summary> Initializes a new instance of the <see cref="DataTypeTest" /> class. </summary>
/// <param name="incomingData"> Record from Export Webservice that will be mapped to the constructed instance. </param>
public Stddat(webExport incomingData)
{
// stddat fields
this.Fields.SndRec = webExport.SndRec;
this.Fields.PrtNum = webExport.PrtNum;
this.Fields.MsgTyp = webExport.MsgTyp;
.
.
.
}
/// <summary> Initializes a new instance of the <see cref="DataTypeTest" /> class. </summary>
/// <param name="incomingData"> Record from Exchange Webservice that will be mapped to the constructed instance. </param>
public Stddat(webExchange incomingData)
{
// stddat fields
this.Fields.SndRec = webExchange.SndRec;
this.Fields.PrtNum = webExchange.PrtNum;
this.Fields.MsgTyp = webExchange.MsgTyp;
.
.
.
}
uj5u.com熱心網友回復:
好吧,如果有這么多屬性,那么我會說地圖軟體是合理的。
有經典的自動映射器,
AutoMapper 是一個簡單的小庫,旨在解決一個看似復雜的問題——擺脫將一個物件映射到另一個物件的代碼。
https://automapper.org/
但也有 Mapster:
https://github.com/MapsterMapper/Mapster
它有一個代碼生成工具,可能有用也可能沒用。
以及其他具有各種積極和消極方面的人——這些都出現在我的腦海中。
uj5u.com熱心網友回復:
如果您可以修改生成的類,以便您的 X 和 Y 中的每一個都繼承相同的基類,或者實作指定 164 個 props 的相同介面,那么您可以從另一個呼叫一個建構式
public Stddat(BaseClass incomingData){
//set 164 props here
}
public Stddat(WebExport incomingData) : this((BaseClass)incomingData) //set 164 props
{
this.UniqueToWebExport1 = incomingData.UniqueToWebExport1;
...
}
public Stddat(WebExchange incomingData) : this((BaseClass)incomingData) //set 164 props
{
this.UniqueToWebExchange1 = incomingData.UniqueToWebExchange1;
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/389750.html
下一篇:資料到達控制器但不查看
