• WPF调用webapi并展示数据(二):类库实体类的构建


    1. 创建类库放置实体

    2. 创建文件夹Models,在文件夹中创建类BaseDto

    1. //用于在属性更改时通知侦听器
    2. public class BaseDto : INotifyPropertyChanged
    3. {
    4. public int Id { get; set; }
    5. //通知属性更改的事件
    6. public event PropertyChangedEventHandler PropertyChanged;
    7. ///
    8. /// 实现通知更新
    9. ///
    10. public void OnPropertyChanged([CallerMemberName] string propertyName = "")
    11. {
    12. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    13. }
    14. }

    3.在文件夹中创建类DailyDto

    1. public class DailyDto : BaseDto
    2. {
    3. private int id;
    4. private string title;
    5. public int Id
    6. {
    7. get { return id; }
    8. set
    9. {
    10. id = value;
    11. OnPropertyChanged();
    12. }
    13. }
    14. public string Title
    15. {
    16. get { return title; }
    17. set { title = value; OnPropertyChanged(); }
    18. }
    19. }

    4. 在文件夹中创建类Page

    1. public class Page
    2. {
    3. public int PageIndex { get; set; } // 当前页码
    4. public int PageSize { get; set; } // 每页记录数
    5. }

    5.在类库创建类ToDoParameter

    1. public class ToDoParameter : Page
    2. {
    3. public int? Status { get; set; }
    4. }

    6.在类库创建类IPagedList

    1. ///
    2. /// 为任何类型的分页列表提供接口
    3. ///
    4. public interface IPagedList<T>
    5. {
    6. ///
    7. /// 获取索引起始值
    8. ///
    9. int IndexFrom { get; }
    10. ///
    11. /// 获取页索引(当前)
    12. ///
    13. int PageIndex { get; }
    14. ///
    15. /// 获取页面大小
    16. ///
    17. int PageSize { get; }
    18. ///
    19. /// 获取类型列表的总计数
    20. ///
    21. int TotalCount { get; }
    22. ///
    23. /// 获取页面总数
    24. ///
    25. int TotalPages { get; }
    26. ///
    27. /// 获取当前页项
    28. ///
    29. IList Items { get; }
    30. ///
    31. /// 获取前一页
    32. ///
    33. bool HasPreviousPage { get; }
    34. ///
    35. /// 获取下一页
    36. ///
    37. /// The has next page.
    38. bool HasNextPage { get; }
    39. }

    7.在类库创建类PagedList

    1. /// 页类型的数据
    2. public class PagedList<T> : IPagedList<T>
    3. {
    4. ///
    5. /// 获得页的起始页
    6. ///
    7. public int PageIndex { get; set; }
    8. ///
    9. /// 获得页大小
    10. ///
    11. public int PageSize { get; set; }
    12. ///
    13. /// 获得总数
    14. ///
    15. public int TotalCount { get; set; }
    16. ///
    17. /// 获得总页数
    18. ///
    19. public int TotalPages { get; set; }
    20. ///
    21. /// 从索引起
    22. ///
    23. public int IndexFrom { get; set; }
    24. ///
    25. /// 数据
    26. ///
    27. public IList Items { get; set; }
    28. ///
    29. /// 获取前一页
    30. ///
    31. public bool HasPreviousPage => PageIndex - IndexFrom > 0;
    32. ///
    33. /// 获取下一页
    34. ///
    35. public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;
    36. public PagedList(IEnumerable source, int pageIndex, int pageSize, int indexFrom)
    37. {
    38. if (indexFrom > pageIndex)
    39. {
    40. throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
    41. }
    42. if (source is IQueryable querable)
    43. {
    44. PageIndex = pageIndex;
    45. PageSize = pageSize;
    46. IndexFrom = indexFrom;
    47. TotalCount = querable.Count();
    48. TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
    49. Items = querable.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToList();
    50. }
    51. else
    52. {
    53. PageIndex = pageIndex;
    54. PageSize = pageSize;
    55. IndexFrom = indexFrom;
    56. TotalCount = source.Count();
    57. TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
    58. Items = source.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToList();
    59. }
    60. }
    61. public PagedList() => Items = new T[0];
    62. }
    63. public class PagedList<TSource, TResult> : IPagedList<TResult>
    64. {
    65. public int PageIndex { get; }
    66. public int PageSize { get; }
    67. public int TotalCount { get; }
    68. public int TotalPages { get; }
    69. public int IndexFrom { get; }
    70. public IList Items { get; }
    71. public bool HasPreviousPage => PageIndex - IndexFrom > 0;
    72. public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;
    73. public PagedList(IEnumerable source, Func, IEnumerable> converter, int pageIndex, int pageSize, int indexFrom)
    74. {
    75. if (indexFrom > pageIndex)
    76. {
    77. throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
    78. }
    79. if (source is IQueryable querable)
    80. {
    81. PageIndex = pageIndex;
    82. PageSize = pageSize;
    83. IndexFrom = indexFrom;
    84. TotalCount = querable.Count();
    85. TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
    86. var items = querable.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToArray();
    87. Items = new List(converter(items));
    88. }
    89. else
    90. {
    91. PageIndex = pageIndex;
    92. PageSize = pageSize;
    93. IndexFrom = indexFrom;
    94. TotalCount = source.Count();
    95. TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
    96. var items = source.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToArray();
    97. Items = new List(converter(items));
    98. }
    99. }
    100. public PagedList(IPagedList source, Func, IEnumerable> converter)
    101. {
    102. PageIndex = source.PageIndex;
    103. PageSize = source.PageSize;
    104. IndexFrom = source.IndexFrom;
    105. TotalCount = source.TotalCount;
    106. TotalPages = source.TotalPages;
    107. Items = new List(converter(source.Items));
    108. }
    109. }
    110. public static class PagedList
    111. {
    112. public static IPagedList<T> Empty<T>() => new PagedList();
    113. public static IPagedList<TResult> From<TResult, TSource>(IPagedList source, Func, IEnumerable> converter) => new PagedList(source, converter);
    114. }

    8.在类库创建类ApiResponse

    1. public class ApiResponse
    2. {
    3. public ApiResponse(string title, bool status = false)
    4. {
    5. this.Title = title;
    6. this.Status = status;
    7. }
    8. public ApiResponse(bool status, object result)
    9. {
    10. this.Status = status;
    11. this.Result = result;
    12. }
    13. public string? Title { get; set; }
    14. public bool Status { get; set; }
    15. public object? Result { get; set; }
    16. }
    17. public class ApiResponse<T>
    18. {
    19. public bool Status { get; set; }
    20. public T? Result { get; set; }
    21. }
  • 相关阅读:
    Java内部类(自用)
    数据结构:树形数据结构
    如何恢复edge的自动翻译功能
    RabbitMQ(六)延时队列
    C++中的继承(下)
    从一道面试题看函数柯里化
    MFC C++ BMP图片向右旋转90度示例函数 WCHAR与CHAR互转 CStringW CStringA互转
    php 随机生成指定金额范围内的随机数
    Perl区分文件换行符类型
    Vue学习——组件(22)
  • 原文地址:https://blog.csdn.net/Ronion123/article/details/136366682