Reputation: 35
I'm with a simple doubt, what's best approach to setting properties of a class that depends of the values of another class.
So I have this three classes that two of them will be serialized and sent over tcp. So, the the two classes in the end are dtos, the third one it's the one that has the values/rules to setting the properties.
So one simple way to do the job it's something like this:
class Source
{
public string[] Prop1 { get; set; }
public string[] Prop2 { get; set; }
}
class Values
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
class Final
{
private readonly Source _source;
private readonly Values _values;
public int Index1 { get {
int index = Array.IndexOf(_source.Prop1, _values.Value1);
if (index == -1)
throw new ArgumentOutOfRangeException($"Source available values: {string.Join(", ", _source.Prop1)} but {_values.Value1} received.");
return index;
} }
public int Index2 { get; set; }
public Final(Source source, Values values)
{
this._source = source;
this._values = values;
}
}
This is a simple sample, in the real code I have arround 20 properties to set, and the logic it's the same for almost all props. I don't like to see this code in the getter of the properties, so I'm asking for help to decide what should be a best/good approach to do this.
Thanks
Upvotes: 0
Views: 508
Reputation: 447
First of all, DTO objects are meant to transfer data between processes and nothing else. So there shouldn't be any logic inside your DTO classes, only raw data. And all the data preparation/validating logic should be done in a separate class.
Second:
I have arround 20 properties to set, and the logic it's the same for almost all props.
If I understood you correctly, you want to set the properties of Final
class (or any other similar class). For properties with get/set logic that doesn't repeat among other properties, you should just leave the logic inside getters and setters, that's what they are for. But obviously don't pile every line of your logic inside getters/setters, separate and break your logic into methods if needed.
Like here Index1
and Index2
use the exact same logic, while DifferentIndex
for example might use just a part of it and do something differently in addition:
public int Index1 => GetSourceIndexOfValue(_source.Prop1, _values.Value1);
public int Index2 => GetSourceIndexOfValue(_source.Prop2, _values.Value2);
public int DifferentIndex
{
get
{
var result = GetSourceIndexOfValue(_source.Prop1, _values.Value2);
//Do something different to result (like use
//other methods to change or validate data) if required
//var validatedResult = ValidateResult(result);
return validatedResult;
}
}
private int GetSourceIndexOfValue(string[] sourceProp, string valueProp)
{
int index = Array.IndexOf(sourceProp, valueProp);
if (index == -1)
throw new ArgumentOutOfRangeException($"Source available values: {string.Join(", ", sourceProp)} but {valueProp} received.");
return index;
}
Also I would suggest naming your DTO classes like SourceDTO
and ValuesDTO
for better readability.
Hope this helps!
Upvotes: 2