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

1. 创建类库放置实体

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

cs 复制代码
 //用于在属性更改时通知侦听器
 public class BaseDto : INotifyPropertyChanged
 {
     public int Id { get; set; }

     //通知属性更改的事件
     public event PropertyChangedEventHandler PropertyChanged;

     /// <summary>
     /// 实现通知更新
     /// </summary>
     public void OnPropertyChanged([CallerMemberName] string propertyName = "")
     {
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
     }
 }

3.在文件夹中创建类DailyDto

cs 复制代码
public class DailyDto : BaseDto
{
    private int id;
    private string title;

    public int Id
    {
        get { return id; }
        set
        {
            id = value;
            OnPropertyChanged();
        }
    }
    public string Title
    {
        get { return title; }
        set { title = value; OnPropertyChanged(); }
    }
}

4. 在文件夹中创建类Page

cs 复制代码
 public class Page
 {
     public int PageIndex { get; set; } // 当前页码
     public int PageSize { get; set; } // 每页记录数
 }

5.在类库创建类ToDoParameter

cs 复制代码
  public class ToDoParameter : Page
  {
      public int? Status { get; set; }
  }

6.在类库创建类IPagedList

cs 复制代码
 /// <summary>
 /// 为任何类型的分页列表提供接口
 /// </summary>
 public interface IPagedList<T>
 {
     /// <summary>
     /// 获取索引起始值
     /// </summary>
     int IndexFrom { get; }
     /// <summary>
     /// 获取页索引(当前)
     /// </summary>
     int PageIndex { get; }
     /// <summary>
     /// 获取页面大小
     /// </summary>
     int PageSize { get; }
     /// <summary>
     /// 获取类型列表的总计数
     /// </summary>
     int TotalCount { get; }
     /// <summary>
     /// 获取页面总数
     /// </summary>
     int TotalPages { get; }
     /// <summary>
     /// 获取当前页项
     /// </summary>
     IList<T> Items { get; }
     /// <summary>
     /// 获取前一页
     /// </summary>
     bool HasPreviousPage { get; }

     /// <summary>
     /// 获取下一页
     /// </summary>
     /// <value>The has next page.</value>
     bool HasNextPage { get; }
 }

7.在类库创建类PagedList

cs 复制代码
/// 页类型的数据
public class PagedList<T> : IPagedList<T>
{
    /// <summary>
    /// 获得页的起始页
    /// </summary>
    public int PageIndex { get; set; }
    /// <summary>
    /// 获得页大小
    /// </summary>
    public int PageSize { get; set; }
    /// <summary>
    /// 获得总数
    /// </summary>
    public int TotalCount { get; set; }
    /// <summary>
    /// 获得总页数
    /// </summary>
    public int TotalPages { get; set; }
    /// <summary>
    /// 从索引起
    /// </summary>
    public int IndexFrom { get; set; }

    /// <summary>
    /// 数据
    /// </summary>
    public IList<T> Items { get; set; }

    /// <summary>
    /// 获取前一页
    /// </summary>
    public bool HasPreviousPage => PageIndex - IndexFrom > 0;

    /// <summary>
    /// 获取下一页
    /// </summary>
    public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;

    public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int indexFrom)
    {
        if (indexFrom > pageIndex)
        {
            throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
        }

        if (source is IQueryable<T> querable)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            IndexFrom = indexFrom;
            TotalCount = querable.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            Items = querable.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToList();
        }
        else
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            IndexFrom = indexFrom;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            Items = source.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToList();
        }
    }

    public PagedList() => Items = new T[0];
}


public class PagedList<TSource, TResult> : IPagedList<TResult>
{

    public int PageIndex { get; }

    public int PageSize { get; }

    public int TotalCount { get; }

    public int TotalPages { get; }

    public int IndexFrom { get; }

    public IList<TResult> Items { get; }

    public bool HasPreviousPage => PageIndex - IndexFrom > 0;

    public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;


    public PagedList(IEnumerable<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> converter, int pageIndex, int pageSize, int indexFrom)
    {
        if (indexFrom > pageIndex)
        {
            throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
        }

        if (source is IQueryable<TSource> querable)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            IndexFrom = indexFrom;
            TotalCount = querable.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            var items = querable.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToArray();

            Items = new List<TResult>(converter(items));
        }
        else
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            IndexFrom = indexFrom;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            var items = source.Skip((PageIndex - IndexFrom) * PageSize).Take(PageSize).ToArray();

            Items = new List<TResult>(converter(items));
        }
    }

    public PagedList(IPagedList<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> converter)
    {
        PageIndex = source.PageIndex;
        PageSize = source.PageSize;
        IndexFrom = source.IndexFrom;
        TotalCount = source.TotalCount;
        TotalPages = source.TotalPages;

        Items = new List<TResult>(converter(source.Items));
    }
}

public static class PagedList
{
   
    public static IPagedList<T> Empty<T>() => new PagedList<T>();
    
    public static IPagedList<TResult> From<TResult, TSource>(IPagedList<TSource> source, Func<IEnumerable<TSource>, IEnumerable<TResult>> converter) => new PagedList<TSource, TResult>(source, converter);
}

8.在类库创建类ApiResponse

cs 复制代码
 public class ApiResponse
 {
     public ApiResponse(string title, bool status = false)
     {
         this.Title = title;
         this.Status = status;
     }
     public ApiResponse(bool status, object result)
     {
         this.Status = status;
         this.Result = result;
     }
     public string? Title { get; set; }

     public bool Status { get; set; }

     public object? Result { get; set; }
 }

 public class ApiResponse<T>
 {
     public bool Status { get; set; }

     public T? Result { get; set; }
 }
相关推荐
hez201017 小时前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉6 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫7 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫8 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6258 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902118 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠9 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫11 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech11 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf13 天前
C#摸鱼实录——IoC与DI案例详解
c#