【问题标题】:Request DTO with private setters使用私人二传手请求 DTO
【发布时间】:2017-03-27 22:35:28
【问题描述】:

我有一个像这样的 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

    // .... 
}

在服务器端,所有 没有设置器的道具都是 NULL。 构造函数似乎被忽略了这是设计的吗?

由于这些 props 在 Redis 中构成一个键,因此我需要确保它们具有值。是否正确,我确实在我的 DTO 中声明了公共设置器,然后使用验证器来确保设置所有道具??

【问题讨论】:

    标签: servicestack


    【解决方案1】:

    是的,DTO(数据传输对象)在设计上意味着具有可序列化的属性,带有公共 getter/setter 和无参数构造函数。

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      相关资源
      最近更新 更多