Home > Blockchain >  Constructor for different input types without code duplication in C#
Constructor for different input types without code duplication in C#

Time:12-22

I have a problem with the mapping of my data class. It contains 164 properties that are getting filled with data received from a soap web service. Dont question the property count, this is an very old predefined .csv-like data scheme that i can not change.

As far as i got it, everything works now, but since there are so many properties and I explicitly set every one of them (eg data.a1 = import.a1 OR data.a1 = export.a1) i have a big block of duplicated assignment code. Both web services deliver the same 164 fields plus some additional fields that differs for both classes. I think i only need to map the 164 identical fields and can ignore/discard the others.

What would be the correct approach to solve this problem? I thought about using 'object' or something as data type in the constructor so that i can pass data from both services, but it does not seem to work like i imagined.

Any advice would be greatly appreciated.

    /// <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;
        .
        .
        .
    }

CodePudding user response:

Well, if there are that many properties this is the time i'd say a mapping software is justified.

There's the classic automapper,

AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another.

https://automapper.org/

But there's also Mapster:

https://github.com/MapsterMapper/Mapster

Which has a code generation tool which may or may not be useful.

And others with all kinds of positive and negative aspects - these came to the top of my head.

CodePudding user response:

If you can modify the generated classes so each of your X and your Y inherit the same base, or implement the same interface that nominates 164 props then you can call one constructor from the other

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;
  ...
}
  • Related