.NET 6开发TodoList应用之实现数据塑形

 更新时间:2022年01月05日 09:05:31   作者:CODE4NOTHING  
在查询的场景中,还有一类需求不是很常见,就是在前端请求中指定返回的字段。所以这篇文章主要介绍了.NET 6如何实现数据塑形,需要的可以参考一下

需求

在查询的场景中,还有一类需求不是很常见,就是在前端请求中指定返回的字段,所以关于搜索的最后一个主题我们就来演示一下关于数据塑形(Data Shaping)。

目标

实现数据塑形搜索请求。

原理与思路

对于数据塑形来说,我们需要定义一些接口和泛型类实现来完成通用的功能,然后修改对应的查询请求,实现具体的功能。

实现

定义通用接口和泛型类实现

IDataShaper.cs

using System.Dynamic;

namespace TodoList.Application.Common.Interfaces;

public interface IDataShaper<T>
{
    IEnumerable<ExpandoObject> ShapeData(IEnumerable<T> entities, string fieldString);
    ExpandoObject ShapeData(T entity, string fieldString);
}

并实现通用的功能:

DataShaper.cs

using System.Dynamic;
using System.Reflection;
using TodoList.Application.Common.Interfaces;

namespace TodoList.Application.Common;

public class DataShaper<T> : IDataShaper<T> where T : class
{
    public PropertyInfo[] Properties { get; set; }

    public DataShaper()
    {
        Properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    }

    public IEnumerable<ExpandoObject> ShapeData(IEnumerable<T> entities, string? fieldString)
    {
        var requiredProperties = GetRequiredProperties(fieldString);

        return GetData(entities, requiredProperties);
    }

    public ExpandoObject ShapeData(T entity, string? fieldString)
    {
        var requiredProperties = GetRequiredProperties(fieldString);

        return GetDataForEntity(entity, requiredProperties);
    }

    private IEnumerable<PropertyInfo> GetRequiredProperties(string? fieldString)
    {
        var requiredProperties = new List<PropertyInfo>();

        if (!string.IsNullOrEmpty(fieldString))
        {
            var fields = fieldString.Split(',', StringSplitOptions.RemoveEmptyEntries);
            foreach (var field in fields)
            {
                var property = Properties.FirstOrDefault(pi => pi.Name.Equals(field.Trim(), StringComparison.InvariantCultureIgnoreCase));
                if (property == null)
                {
                    continue;
                }

                requiredProperties.Add(property);
            }
        }
        else
        {
            requiredProperties = Properties.ToList();
        }

        return requiredProperties;
    }

    private IEnumerable<ExpandoObject> GetData(IEnumerable<T> entities, IEnumerable<PropertyInfo> requiredProperties)
    {
        return entities.Select(entity => GetDataForEntity(entity, requiredProperties)).ToList();
    }

    private ExpandoObject GetDataForEntity(T entity, IEnumerable<PropertyInfo> requiredProperties)
    {
        var shapedObject = new ExpandoObject();
        foreach (var property in requiredProperties)
        {
            var objectPropertyValue = property.GetValue(entity);
            shapedObject.TryAdd(property.Name, objectPropertyValue);
        }

        return shapedObject;
    }
}

定义扩展方法

为了使我们的Handle方法调用链能够直接应用,我们在Application/Extensions中新增一个DataShaperExtensions:

DataShaperExtensions.cs

using System.Dynamic;
using TodoList.Application.Common.Interfaces;

namespace TodoList.Application.Common.Extensions;

public static class DataShaperExtensions
{
    public static IEnumerable<ExpandoObject> ShapeData<T>(this IEnumerable<T> entities, IDataShaper<T> shaper, string? fieldString)
    {
        return shaper.ShapeData(entities, fieldString);
    }
}

然后再对我们之前写的MappingExtensions静态类中添加一个方法:

MappingExtensions.cs

// 省略其他...
public static PaginatedList<TDestination> PaginatedListFromEnumerable<TDestination>(this IEnumerable<TDestination> entities, int pageNumber, int pageSize)
{
    return PaginatedList<TDestination>.Create(entities, pageNumber, pageSize);   
}

添加依赖注入

在Application的DependencyInjection.cs中添加依赖注入:

DependencyInjection.cs

// 省略其他
services.AddScoped(typeof(IDataShaper<>), typeof(DataShaper<>));

修改查询请求和Controller接口

我们在上一篇文章实现排序的基础上增加一个字段用于指明数据塑形字段并对应修改Handle方法:

GetTodoItemsWithConditionQuery.cs

using System.Dynamic;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using MediatR;
using TodoList.Application.Common.Extensions;
using TodoList.Application.Common.Interfaces;
using TodoList.Application.Common.Mappings;
using TodoList.Application.Common.Models;
using TodoList.Application.TodoItems.Specs;
using TodoList.Domain.Entities;
using TodoList.Domain.Enums;

namespace TodoList.Application.TodoItems.Queries.GetTodoItems;

public class GetTodoItemsWithConditionQuery : IRequest<PaginatedList<ExpandoObject>>
{
    public Guid ListId { get; set; }
    public bool? Done { get; set; }
    public string? Title { get; set; }
    // 前端指明需要返回的字段
    public string? Fields { get; set; }
    public PriorityLevel? PriorityLevel { get; set; }
    public string? SortOrder { get; set; } = "title_asc";
    public int PageNumber { get; set; } = 1;
    public int PageSize { get; set; } = 10;
}

public class GetTodoItemsWithConditionQueryHandler : IRequestHandler<GetTodoItemsWithConditionQuery, PaginatedList<ExpandoObject>>
{
    private readonly IRepository<TodoItem> _repository;
    private readonly IMapper _mapper;
    private readonly IDataShaper<TodoItemDto> _shaper;

    public GetTodoItemsWithConditionQueryHandler(IRepository<TodoItem> repository, IMapper mapper, IDataShaper<TodoItemDto> shaper)
    {
        _repository = repository;
        _mapper = mapper;
        _shaper = shaper;
    }

    public Task<PaginatedList<ExpandoObject>> Handle(GetTodoItemsWithConditionQuery request, CancellationToken cancellationToken)
    {
        var spec = new TodoItemSpec(request);
        return Task.FromResult(
            _repository
                .GetAsQueryable(spec)
                .ProjectTo<TodoItemDto>(_mapper.ConfigurationProvider)
                .AsEnumerable()
                // 进行数据塑形和分页返回
                .ShapeData(_shaper, request.Fields)
                .PaginatedListFromEnumerable(request.PageNumber, request.PageSize)
            );
    }
}

对应修改Controller:

TodoItemController.cs

[HttpGet]
public async Task<ApiResponse<PaginatedList<ExpandoObject>>> GetTodoItemsWithCondition([FromQuery] GetTodoItemsWithConditionQuery query)
{
    return ApiResponse<PaginatedList<ExpandoObject>>.Success(await _mediator.Send(query));
}

验证

启动Api项目,执行查询TodoItem的请求:

请求

响应

我们再把之前讲到的过滤和搜索添加到请求里来:

请求

响应

总结

对于数据塑形的请求,关键步骤就是使用反射获取待返回对象的所有配置的可以返回的属性,再通过前端传入的属性名称进行过滤和值的重组进行返回。实现起来是比较简单的。但是在实际的使用过程中我不推荐这样用,除了某些非常适用的特殊场景。个人更偏向于向前端提供明确的接口定义。

到此这篇关于.NET 6开发TodoList应用之实现数据塑形的文章就介绍到这了,更多相关.NET 6数据塑形内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • visual studio 2017企业版本安装(附序列号)

    visual studio 2017企业版本安装(附序列号)

    这篇文章主要介绍了visual studio 2017企业版本安装,文末为大家分享了序列号,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • 用.NET Core写爬虫爬取电影天堂

    用.NET Core写爬虫爬取电影天堂

    本文给大家详细介绍了如何使用.NET Core写爬虫爬取电影天堂的方法和详细步骤,非常的细致,有需要的小伙伴可以参考下
    2016-12-12
  • 在ashx文件中使用session的解决思路

    在ashx文件中使用session的解决思路

    如果你要保证数据的安全性,你可以在ashx中使用session验证如:你的index.aspx中使用jquery回调ashx数据,那么在index.aspx page_load时session[checked]="true",在ashx中验证session是否存在
    2013-01-01
  • 菜渣开源一个基于 EMIT 的 AOP 库(.NET Core)的方法

    菜渣开源一个基于 EMIT 的 AOP 库(.NET Core)的方法

    CZGL.AOP 是 基于 EMIT 编写的 一个简单轻量的AOP框架,支持非侵入式代理,支持.NET Core/ASP.NET Core,以及支持多种依赖注入框架,本文介绍菜渣开源一个基于 EMIT 的 AOP 库(.NET Core)的相关知识,感兴趣的朋友一起看看吧
    2024-06-06
  • 解析.Net 4.0 中委托delegate的使用详解

    解析.Net 4.0 中委托delegate的使用详解

    本篇文章是对.Net 4.0 中委托delegate的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • ASP.Net巧用窗体母版页实例

    ASP.Net巧用窗体母版页实例

    这篇文章主要介绍了ASP.Net巧用窗体母版页的方法,以实例形式详细分析了母版页的用途及嵌套用法,具有一定的学习借鉴价值,需要的朋友可以参考下
    2014-11-11
  • .net core高吞吐远程方法如何调用组件XRPC详解

    .net core高吞吐远程方法如何调用组件XRPC详解

    这篇文章主要给大家介绍了关于.net core高吞吐远程方法如何调用组件XRPC的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • .NET CORE中比较两个文件内容是否相同的最快方法

    .NET CORE中比较两个文件内容是否相同的最快方法

    这篇文章主要给大家介绍了关于.NET CORE中比较两个文件内容是否相同的最快方法,文中通过示例代码介绍的非常详细,对大家学习或者使用.NET CORE具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • .NET Core 使用委托实现动态流程组装的思路详解

    .NET Core 使用委托实现动态流程组装的思路详解

    模拟管道模型中间件(Middleware)部分,运用委托,进行动态流程组装,本次代码实现就直接我之前写的动态代理实现AOP的基础上改的,就不另起炉灶了,主要思路就是运用委托,具体实现过程跟随小编一起看看吧
    2022-01-01
  • WPF实现左右移动(晃动)动画效果

    WPF实现左右移动(晃动)动画效果

    这篇文章主要为大家详细介绍了WPF实现左右移动或晃动动画效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12

最新评论