for object serialization I created a DataTransferObject-Pendant for each of my objects. Each original object gets a ToDTO()-method that returns the appropriate DTO-Object with the properties to be saved . Most of my original objects are inherited from an other, so I would want each inheritance-level to care for their own properties. A simple example:
class base
{
private string _name;
public DTO_base ToDTO()
{
DTO_base result = new DTO_base();
result.Name = _name;
return result;
}
}
The inherited class should override the ToDTO() method, calling the parents method and adding its own properties to be saved, like:
class inherited : base
{
private string _description;
public new DTO_inherited ToDTO()
{
DTO_inherited result = base.ToDTO();
result.Description = _description;
return result;
}
}
Obviously, this wont work, because base.ToDTO() returns a DTO_base object. Can anyone suggest, how this feature would be implemented best?
Thanks in advance,
Frank
I would use a Template Method: