using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using Dpz.Core.EnumLibrary;

namespace Dpz.Core.Public.ViewModel.DynamicPages;

/// <summary>
/// Represents the VmDynamicPage type.
/// </summary>
public class VmDynamicPage
{
    /// <summary>
    /// Gets or sets Id.
    /// </summary>
    [Required]
    public string? Id { get; set; }

    /// <summary>
    /// Gets or sets Content.
    /// </summary>
    [Required]
    public string? Content { get; set; }

    /// <summary>
    /// 脚本 地址
    /// </summary>
    [XmlIgnore]
    public SortedDictionary<int, string>? Scripts { get; set; }

    /// <summary>
    /// Gets or sets SerializableScripts.
    /// </summary>
    [XmlArray(ElementName = "Scripts")]
    [XmlArrayItem(ElementName = "Script")]
    [JsonIgnore]
    public KeyValueElement[] SerializableScripts =>
        Scripts?.Select(x => new KeyValueElement(x.Key, x.Value)).ToArray() ?? [];

    /// <summary>
    /// 样式 地址
    /// </summary>
    [XmlIgnore]
    public SortedDictionary<int, string>? Styles { get; set; }

    /// <summary>
    /// Gets or sets SerializableStyles.
    /// </summary>
    [XmlArray(ElementName = "Styles")]
    [XmlArrayItem(ElementName = "Style")]
    [JsonIgnore]
    public KeyValueElement[] SerializableStyles =>
        Styles?.Select(x => new KeyValueElement(x.Key, x.Value)).ToArray() ?? [];

    /// <summary>
    /// Content-Type
    /// </summary>
    public PageContentType ContentType { get; set; }

    /// <summary>
    /// content type
    /// </summary>
    public string ContentTypeStr => ContentType.ToString();

    /// <summary>
    /// Gets or sets Creator.
    /// </summary>
    public VmUserInfo? Creator { get; set; }

    /// <summary>
    /// Gets or sets CreateTime.
    /// </summary>
    public DateTime CreateTime { get; set; } = DateTime.Now;

    /// <summary>
    /// Gets or sets LastUpdateTime.
    /// </summary>
    public DateTime LastUpdateTime { get; set; } = DateTime.Now;
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

这是一个用于动态页面的 ViewModel 类 VmDynamicPage,主要用于表示和序列化动态页面数据。让我详细解释一下这个类的各个部分:

类的作用

VmDynamicPage 是一个视图模型类,用于处理动态页面的数据传输和序列化,支持 JSON 和 XML 两种序列化格式。

主要属性解析

基础属性

  • Id: 页面的唯一标识符,标记为必填字段
  • Content: 页面的主要内容,标记为必填字段

脚本和样式管理

[XmlIgnore]
public SortedDictionary<int, string>? Scripts { get; set; }
  • 使用 SortedDictionary<int, string> 存储脚本文件,键为优先级/顺序,值为脚本地址
  • XmlIgnore 表示在 XML 序列化时忽略此属性
[XmlArray(ElementName = "Scripts")]
[XmlArrayItem(ElementName = "Script")]
[JsonIgnore]
public KeyValueElement[] SerializableScripts
  • 提供 XML 序列化友好的脚本数据格式
  • SortedDictionary 转换为 KeyValueElement 数组
  • JsonIgnore 表示在 JSON 序列化时忽略此属性

样式 (StylesSerializableStyles) 的设计模式与脚本完全相同。

内容类型

  • ContentType: 使用枚举 PageContentType 表示页面内容类型
  • ContentTypeStr: 提供内容类型的字符串表示形式

元数据

  • Creator: 创建者信息,类型为 VmUserInfo
  • CreateTime: 创建时间,默认为当前时间
  • LastUpdateTime: 最后更新时间,默认为当前时间

设计特点

1. 双重序列化支持

这个类巧妙地处理了不同序列化格式的需求:

  • JSON序列化: 直接使用 ScriptsStyles 字典
  • XML序列化: 使用 SerializableScriptsSerializableStyles 数组

2. 有序管理

使用 SortedDictionary<int, string> 确保脚本和样式文件按照指定顺序加载,这对于前端资源的依赖关系管理很重要。

3. 数据验证

使用 [Required] 注解确保关键字段不为空。

使用场景

这个类适用于:

  • CMS(内容管理系统)中的动态页面管理
  • 需要动态加载脚本和样式的页面系统
  • 支持多种序列化格式的 Web 应用程序

这种设计模式很好地分离了内部数据结构和外部序列化格式,提供了灵活性和兼容性。

评论加载中...