I need to refactor following class:
public class Message
{
public Guid ID { get; set; }
public string MessageIn { get; set; }
public string MessageOut { get; set; }
public int StatusCode { get; set; } //EDIT could be changed during message lifecycle
public bool IsDeletable
{
get
{
switch (this.StatusCode)
{
case 12:
case 13:
case 22:
case 120:
return true;
default:
return false;
}
}
}
public bool IsEditable
{
get
{
switch (this.StatusCode)
{
case 12:
case 13:
case 22:
case 120:
return true;
default:
return false;
}
}
}
public string Message
{
get
{
switch (this.StatusCode)
{
case 11:
case 110:
return this.MessageIn;
case 12:
case 13:
case 22:
case 120:
return this.MessageOut;
default:
return string.Empty;
}
}
}
}
- I would like to remove the business rules
IsDeletableandIsEditable - I would like to remove these
switchstatements at the same time
I am not sure if it's worth knowing that I am mapping entity to database table through Entity Framework.
One more problem that I have is that fields MessageIn and MessageOut are dependent on StatusCode. One of them are always populated. I could create new property but still the switch case is there:
public string Message
{
get
{
switch (this.StatusCode)
{
case 10:
case 12:
case 13:
case :
return this.MessageIn;
default:
return this.MessageOut;
}
}
set { // switch again}
}
StatusCodeis integral to aMessageand edit/delete is directly related toStatusCodethen it looks reasonable as is. \$\endgroup\$public bool IsDeletable {get{return IsEditable;}}? Currently, the two switches are identical. \$\endgroup\$