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; }
}