我有像这样的DTO:
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}";
}
}
在客户端(C#中的WPF应用程序)id上:
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
// ....
}
在服务器端,没有setter 的所有道具都是NULL。看起来构造函数被忽略。 这是设计吗?
由于这些道具在Redis中形成了一个键,我需要确保它们具有值。这是正确的,我已经在我的DTO中声明了公共设置器,然后使用Validators来确保所有道具都已设置 ??
答案 0 :(得分:1)
是设计的DTO(数据传输对象)是指具有公共getter / setter和无参数构造函数的可序列化属性。