lundi 15 juin 2020

Pattern to create an object and populate its properties in other methods

I have created a Photo class which contains a lot of properties such as size, filename, latitude/longitude, location etc.

public class Photo {
    public string FileName { get; set; }
    public string Title { get; set; }
    public double Size { get; set; }
    public DateTime CapturedDate { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string LocationName { get; set; }
    public byte[] Data { get; set; }
}

I want to create an object of this class and populate its properties in a step by step approach, where each step needs to perform some kind of operation before setting some of the values on the object.

In a quick and dirty solution to make this work I simply created some void methods to fill the object properties, but I think this is anti-pattern.

var photo = new Photo();
photo.FileName = "Photo 1"
SetExifData(photo, photoStream);
SetLocationName(photo);
DoSomethingElse(photo);

void SetExifData(Photo photo, Stream photoStream) {
   //Read exit data from the photo stream and update the object
   photo.Latitude = "10,0";
   photo.Longitude = "10,0";
   photo.CapturedDate = ....
}
void SetLocationName(Photo photo){
   //Call external API to get location name from lat/lng
   photo.LocationName = "London"
}
void DoSomethingElse(Photo photo){
}

It would be optimal to pass this object to a kind of pipeline or builder. What pattern would be suitable for this scenario?

Aucun commentaire:

Enregistrer un commentaire