网站首页 网站源码
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using Dpz.Core.EnumLibrary;
using Dpz.Core.Infrastructure;
using Dpz.Core.Public.Entity;
#nullable enable
namespace Dpz.Core.Public.ViewModel.DynamicPages;
public class VmDynamicPage : IMapFrom<DynamicPage>
{
[Required]
public string? Id { get; set; }
[Required]
public string? Content { get; set; }
/// <summary>
/// 脚本 地址
/// </summary>
[XmlIgnore]
public SortedDictionary<int, string>? Scripts { get; set; }
[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; }
[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();
public VmUserInfo? Creator { get; set; }
public DateTime CreateTime { get; set; } = DateTime.Now;
public DateTime LastUpdateTime { get; set; } = DateTime.Now;
}
上述代码定义了一个名为 VmDynamicPage
的类,它是一个视图模型(ViewModel),用于表示动态页面的相关数据。这个类实现了 IMapFrom<DynamicPage>
接口,表明它可以从 DynamicPage
实体映射而来。以下是对代码中各个部分的详细解释:
Id:
string?
[Required]
特性,表示该属性是必填的。Content:
string?
[Required]
特性,表示该属性是必填的。Scripts:
SortedDictionary<int, string>?
[XmlIgnore]
特性,表示在 XML 序列化时忽略该属性。SerializableScripts:
KeyValueElement[]
Scripts
字典,将其转换为 KeyValueElement
数组,以便在 XML 中表示。使用了 LINQ 的 Select
方法进行转换。Styles:
SortedDictionary<int, string>?
[XmlIgnore]
特性,表示在 XML 序列化时忽略该属性。SerializableStyles:
KeyValueElement[]
Styles
字典,将其转换为 KeyValueElement
数组,以便在 XML 中表示。ContentType:
PageContentType
PageContentType
。ContentTypeStr:
string
ContentType
的字符串表示,方便在需要字符串形式的地方使用。Creator:
VmUserInfo?
VmUserInfo
,可以为 null。CreateTime:
DateTime
LastUpdateTime:
DateTime
VmDynamicPage
类的主要功能是封装动态页面的相关信息,以便在应用程序中使用。它提供了必要的属性来描述页面的内容、样式、脚本、创建者信息以及时间戳等。通过实现 IMapFrom<DynamicPage>
接口,该类可以方便地与 DynamicPage
实体进行数据映射,适用于 MVC 或 MVVM 架构中的数据传输和视图展示。