I have following code:
public class Header
{
Line Lines { get { ...}}
public ICryptographer GetCryptographer(FieldGroup group)
{
...
}
}
public class Line
{
public Header Header { get; set; }
public FieldGroup FieldGroup { get; set; }
ICryptographer CryptoGrapher { get { return Header.GetCryptographer(FieldGroup); } }
public decimal? Month1
{
get { return _month1; }
set
{
if (_month1 != value)
Month1_Enc = CryptoGrapher.Encrypt(_month1 = value);
}
}
private decimal? _month1;
protected byte[] Month1_Enc { get; set; }
//The same repeated for Month2 to Month12
}
public interface ICryptographer
{
byte[] Encrypt(decimal? value);
decimal? DecryptDecimal(byte[] value);
}
public enum FieldGroup
{
...
}
Shortly properties Month1 to Month12 are of type decimal? that should be encrypted before they are saved in database. I have also few other classes that have encrypted properties. Every property code looks exactly the same as Month1 I showed here.
Ideally I would like to have something like this:
Encrypted<decimal?> Month1 { get; set;}
but this is impossible because each object may have different Cryptographer (symmetric key).
Is there a way to refactor it to avoid such repeatable code?
Should I care about such repetition ?