I have a rather verbose object with a lot of properties which I use to transport data to a SQL database. I would now like to provide a “light” version of this object that only uses some properties of the verbose one.
I’m using the light objects to provide through a REST api, and the verbose ones to transport the data, so ideally I could reverse the process as well (overwriting properties of verbose object with existing properties of light object, then save to database).
All these properties will have the same data types, as long as they exist in the light object.
Simple example:
class Verbose {
public string email;
public Guid id;
}
class Simple {
public string email;
// don't show Guid
}
Now I want to transform all objects of type Verbose to type Simple, ditching all unnecessary properties – is there a simple way to do this?
Ideally this should be reversible as well.
You can write a constructor that takes a verbose object
and vice versa.
That way, you have all the conversion in one place,
at least each conversion direction.
This question here might be of interest for you:
How to write a converter class? How to efficiently write mapping rules?
EDIT AS IMPORTANT TO OP
Verbose => Simple
If
Simpleis a subset ofVerbose, just deriveVerbosefromSimple.no conversion needed here.
Verbose IS a Simple then.