I am not sure if I am approaching this task the correct way. I have a view model with multiple string properties. I pass the model through an interface to a service class that contains a void to repopulate the user input with my desired format.
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web.Helpers;
public class RegisterViewModel
{
public string UserId { get; set; }
public string Password { get; set; }
public string RecoveryAnswer { get; set; }
public int? EmpId { get; set; }
public string EId { get; set; }
public string UId { get; set; }
public string PeopleSafeId { get; set; }
public string StarsId { get; set; }
public string NamePrefix { get; set; }
public string NameFirst { get; set; }
public string NameMiddle { get; set; }
public string NameLast { get; set; }
public string NameSuffix { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
public string PhoneExtension { get; set; }
public int? DepartmentId { get; set; }
public int? TitleId { get; set; }
public int DomainId { get; set; }
public int? LocationId { get; set; }
}
public interface IRegisterService
{
RegisterViewModel CleanUserInput(RegisterViewModel model);
//...
}
public class RegisterService : IRegisterService
{
public RegisterViewModel CleanUserInput(RegisterViewModel model)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
model.UserId = model.UserId.ToUpper();
model.Password = Crypto.HashPassword(model.Password);
model.RecoveryAnswer = Crypto.HashPassword(model.RecoveryAnswer);
model.EId = model.EId.ToUpper();
model.UId = model.UId.ToUpper();
model.PeopleSafeId = model.PeopleSafeId.ToUpper();
model.StarsId = model.StarsId.ToUpper();
model.NamePrefix = textInfo.ToTitleCase(model.NamePrefix);
model.NameFirst = textInfo.ToTitleCase(model.NameFirst);
model.NameMiddle = textInfo.ToTitleCase(model.NameMiddle);
model.NameLast = textInfo.ToTitleCase(model.NameLast);
model.NameSuffix = textInfo.ToTitleCase(model.NameSuffix);
model.Email = model.Email.ToLower();
model.PhoneNumber = Regex.Replace(model.PhoneNumber, "[^0-9]", "");
model.PhoneExtension = Regex.Replace(model.PhoneNumber, "[^0-9]", "");
return model;
}
//...
}
Is there a better way to approach cleaning up this much input?