lundi 28 octobre 2019

Is there a pattern to conversion between classes

I have a class, from a native library, I want to convert to my own type, and I need to do that in several places. I created, then, a static method, so I don't need to repeat the instantiation of the class in so many places, but somehow it doesn't seem right.

What is the best way to do this kind of thing? Is there any known pattern that covers this subject?

class Record
{
  public String Id { get; set; }
  public String FirstName { get; set; }
  public String LastName { get; set; }
  public String FingerPrints { get; set; }

  //
  // This is a simplification of the method and in fact, I created several of
  // them, and the class doesn't look clean anymore.
  //
  public static Record CreateFromMaciRecord(MaciRecord maci)
  {
    return new Record
      {
        Id = maci.GetRecordId(),
        FirstName = Encoding.UTF8.GetString(maci.GetUserDataField("first_name")),
        LastName = Encoding.UTF8.GetString(maci.GetUserDataField("name"))
      };
  }
}

Aucun commentaire:

Enregistrer un commentaire