I'm comparing two distinct instances and if they are not the same I save to the dB, so I've given snippet of code below of the way I am comparing these instances, I realize I could use reflection and generalize but short of using reflections is there any other way I could optimize the code or make it neater?
public class QuestionModel
{
private string _selectedId = string.Empty;
public string SelectedId
{
get { return _selectedId; } set { _selectedId = value; }
}
private List<UserResponseModel> _userResponse;
public List<UserResponseModel> UserResponses
{
get { return _userResponse ?? (_userResponse = new List<UserResponseModel>()); }
set { _userResponse = value; }
}
}
public class UserResponseModel
{
public string QuestionId { get; set; }
public string QuestionText { get; set; }
public bool IsChecked { get; set; }
public int? Value { get; set; }
public string TextValue { get; set; }
public Byte[] Content { get; set; }
public string FilePath { get; set; }
public int? Points { get; set; }
public int? InputType { get; set; }
public bool? IsCheckedRadioButton1 { get; set; }
public bool? IsCheckedRadioButton2 { get; set; }
public bool? IsCheckedRadioButton3 { get; set; }
public bool? IsCheckedRadioButton4 { get; set; }
public bool? IsCheckedRadioButton5 { get; set; }
public bool IsCheckedCheckbox1 { get; set; }
public bool IsCheckedCheckbox2 { get; set; }
private string _selectedId = string.Empty;
public string SelectedId
{
get { return _selectedId; }
set { _selectedId = value; }
}
}
//snippet of code that compares instances of QuestionModel
QuestionModel questionModelInDb = null;
if (existingResponsesInDb.Any())
questionModelInDb = UtilityFunctions.MapResponsestoUserResponses(existingResponsesInDb);
if (questionModelInDb == null) return false;
var isSame = questionModelInDb.SelectedId == questionModel.SelectedId;
if (!isSame) return false;
var index = 0;
foreach (var response in questionModel.UserResponses)
{
if (isSame)
{
isSame = response.IsChecked == questionModelInDb.UserResponses[index].IsChecked;
isSame = response.IsCheckedCheckbox1 ==
questionModelInDb.UserResponses[index].IsCheckedCheckbox1 && isSame;
isSame = response.IsCheckedCheckbox2 ==
questionModelInDb.UserResponses[index].IsCheckedCheckbox2 &&
isSame;
isSame = response.IsCheckedRadioButton1 ==
questionModelInDb.UserResponses[index].IsCheckedRadioButton1 &&
isSame;
isSame = response.IsCheckedRadioButton2 ==
questionModelInDb.UserResponses[index].IsCheckedRadioButton2 &&
isSame;
isSame = response.IsCheckedRadioButton3 ==
questionModelInDb.UserResponses[index].IsCheckedRadioButton3 &&
isSame;
isSame = response.IsCheckedRadioButton4 ==
questionModelInDb.UserResponses[index].IsCheckedRadioButton4 &&
isSame;
isSame = response.IsCheckedRadioButton5 ==
questionModelInDb.UserResponses[index].IsCheckedRadioButton5 &&
isSame;
isSame = response.SelectedId ==
questionModelInDb.UserResponses[index].SelectedId &&
isSame;
isSame = (response.TextValue ?? String.Empty) ==
(questionModelInDb.UserResponses[index].TextValue ?? String.Empty) &&
isSame;
isSame = (response.Value ?? 0) ==
(questionModelInDb.UserResponses[index].Value ?? 0) &&
isSame;
index++;
}
else
break;
}
return isSame;