I've seen developers mutating request models in controller actions like this:
public class ProfileController
{
private const string DefaultCulture = "en-US";
public Task SaveProfile(string culture, ProfileModel profile)
{
profile.Culture = string.IsNullOrWhiteSpace(culture) ? DefaultCulture : culture;
// then profile is passed to a service/repository to be saved to the DB
}
}
Not sure why it was done that way, but I would like to avoid mutating request models. So, I'm thinking of using init-only setters on all request models to make them immutable and force developers to map to a domain model:
public class ProfileController
{
private const string DefaultCulture = "en-US";
public Task SaveProfile(string culture, ProfileModel profile)
{
var profileDomain = new ProfileDomain
{
Culture = string.IsNullOrWhiteSpace(culture) ? DefaultCulture : culture;
};
// This will cause a compile error when ProfileModel.Culture becomes init-only
// profile.Culture = string.IsNullOrWhiteSpace(culture) ? DefaultCulture : culture;
}
}
Are there any drawback or risks that I should consider if I change all request models to init-only setters?