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; }
 }
相关推荐
冷眼Σ(-᷅_-᷄๑)5 小时前
Path.Combine容易被忽略的细节
c#·.net
SongYuLong的博客10 小时前
C# (定时器、线程)
开发语言·c#
百锦再12 小时前
详解基于C#开发Windows API的SendMessage方法的鼠标键盘消息发送
windows·c#·计算机外设
无敌最俊朗@13 小时前
unity3d————协程原理讲解
开发语言·学习·unity·c#·游戏引擎
程序设计实验室13 小时前
在网页上调起本机C#程序
c#
林子漾14 小时前
【paper】分布式无人水下航行器围捕智能目标
分布式·wpf
Crazy Struggle16 小时前
.NET 8 强大功能 IHostedService 与 BackgroundService 实战
c#·.net·.net core
fs哆哆16 小时前
C#编程:优化【性别和成绩名次】均衡分班
开发语言·c#
fathing18 小时前
c# 调用c++ 的dll 出现找不到函数入口点
开发语言·c++·c#
wyh要好好学习20 小时前
C# WPF 记录DataGrid的表头顺序,下次打开界面时应用到表格中
开发语言·c#·wpf