Reputation: 1566
I have a DTO like so:
public class UserPreferenceDto
{
public long Id { get; set; }
// other props with getter and setter
//props WITHOUT setter, they are set in the ctor
public string UserName { get; }
public string ComputerName { get; }
public string ApplicationCode { get; }
public string ApplicationVersion { get; }
public string PreferenceGroup { get; }
public UserPreferenceDto(string userName, string computerName, string applicationCode, string applicationVersion, string preferenceGroup)
{
// Initializers of other props
UserName = userName.ToLower();
ComputerName = computerName.ToLower();
ApplicationCode = applicationCode.ToLower();
ApplicationVersion = applicationVersion.ToLower();
PreferenceGroup = preferenceGroup.ToLower();
}
public string GetUrnKey()
{
return $"pref:{UserName}:{ComputerName}:{ApplicationCode}:{ApplicationVersion}:{PreferenceGroup}:{Id}";
}
}
On the client (WPF app in C#) id have:
private void ChangeApplicationTheme()
{
Debug.WriteLine($"Changeing application theme to {SelectedTheme}...");
CurrentApp.SetApplicationTheme(SelectedTheme);
var themePrefDto = new UserPreferenceDto(CurrentApp.GetCurrentUserLogonId(), System.Environment.MachineName, "AccountManagerClient", "1.0.000", "Theme");
themePrefDto.CUser = CurrentApp.GetCurrentUserLogonId();
themePrefDto.Preferences.Add("Theme", SelectedTheme);
StorePreferenceItem(themePrefDto);
}
private async void StorePreferenceItem(UserPreferenceDto preferenceItem)
{
#region Update server
IsOperationRunning = true;
OperationProgressText = $"Updating preferences item {preferenceItem}";
try
{
var ssc = CurrentApp.ServiceClient;
var pref = new CreateUserPreference(preferenceItem);
await ssc.PostAsync(pref);
//...
}
catch (WebServiceException we)
{
// ....
}
catch (Exception ex)
{
// ...
}
finally
{
IsOperationRunning = false;
OperationProgressText = string.Empty;
}
#endregion
// ....
}
On the serverside all props which do NOT have a setter are NULL. Looks like the constructor is ignored. Is this by design?
Since these props form a key in Redis, I need to make sure they have a value. Is it correct, that I have do declare public setters in my DTO and then use Validators to make sure all props are set??
Upvotes: 1
Views: 1148
Reputation: 143389
Yes DTOs (Data Transfer Objects) by design are meant to have serializable properties with public getters/setters and a parameterless constructor.
Upvotes: 1