欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.NET 6開(kāi)發(fā)TodoList應(yīng)用之實(shí)現(xiàn)數(shù)據(jù)塑形

 更新時(shí)間:2022年01月05日 09:05:31   作者:CODE4NOTHING  
在查詢(xún)的場(chǎng)景中,還有一類(lèi)需求不是很常見(jiàn),就是在前端請(qǐng)求中指定返回的字段。所以這篇文章主要介紹了.NET 6如何實(shí)現(xiàn)數(shù)據(jù)塑形,需要的可以參考一下

需求

在查詢(xún)的場(chǎng)景中,還有一類(lèi)需求不是很常見(jiàn),就是在前端請(qǐng)求中指定返回的字段,所以關(guān)于搜索的最后一個(gè)主題我們就來(lái)演示一下關(guān)于數(shù)據(jù)塑形(Data Shaping)。

目標(biāo)

實(shí)現(xiàn)數(shù)據(jù)塑形搜索請(qǐng)求。

原理與思路

對(duì)于數(shù)據(jù)塑形來(lái)說(shuō),我們需要定義一些接口和泛型類(lèi)實(shí)現(xiàn)來(lái)完成通用的功能,然后修改對(duì)應(yīng)的查詢(xún)請(qǐng)求,實(shí)現(xiàn)具體的功能。

實(shí)現(xiàn)

定義通用接口和泛型類(lèi)實(shí)現(xiàn)

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

并實(shí)現(xiàn)通用的功能:

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

定義擴(kuò)展方法

為了使我們的Handle方法調(diào)用鏈能夠直接應(yīng)用,我們?cè)贏pplication/Extensions中新增一個(gè)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);
    }
}

然后再對(duì)我們之前寫(xiě)的MappingExtensions靜態(tài)類(lèi)中添加一個(gè)方法:

MappingExtensions.cs

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

添加依賴(lài)注入

在Application的DependencyInjection.cs中添加依賴(lài)注入:

DependencyInjection.cs

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

修改查詢(xún)請(qǐng)求和Controller接口

我們?cè)?a href="http://www.dbjr.com.cn/article/233579.htm" target="_blank">上一篇文章實(shí)現(xiàn)排序的基礎(chǔ)上增加一個(gè)字段用于指明數(shù)據(jù)塑形字段并對(duì)應(yīng)修改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()
                // 進(jìn)行數(shù)據(jù)塑形和分頁(yè)返回
                .ShapeData(_shaper, request.Fields)
                .PaginatedListFromEnumerable(request.PageNumber, request.PageSize)
            );
    }
}

對(duì)應(yīng)修改Controller:

TodoItemController.cs

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

驗(yàn)證

啟動(dòng)Api項(xiàng)目,執(zhí)行查詢(xún)TodoItem的請(qǐng)求:

請(qǐng)求

響應(yīng)

我們?cè)侔阎爸v到的過(guò)濾和搜索添加到請(qǐng)求里來(lái):

請(qǐng)求

響應(yīng)

總結(jié)

對(duì)于數(shù)據(jù)塑形的請(qǐng)求,關(guān)鍵步驟就是使用反射獲取待返回對(duì)象的所有配置的可以返回的屬性,再通過(guò)前端傳入的屬性名稱(chēng)進(jìn)行過(guò)濾和值的重組進(jìn)行返回。實(shí)現(xiàn)起來(lái)是比較簡(jiǎn)單的。但是在實(shí)際的使用過(guò)程中我不推薦這樣用,除了某些非常適用的特殊場(chǎng)景。個(gè)人更偏向于向前端提供明確的接口定義。

到此這篇關(guān)于.NET 6開(kāi)發(fā)TodoList應(yīng)用之實(shí)現(xiàn)數(shù)據(jù)塑形的文章就介紹到這了,更多相關(guān).NET 6數(shù)據(jù)塑形內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • ASP.NET?MVC使用異步Action的方法

    ASP.NET?MVC使用異步Action的方法

    這篇文章介紹了ASP.NET?MVC使用異步Action的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • ASP.NET如何使用web服務(wù)的會(huì)話(huà)狀態(tài)

    ASP.NET如何使用web服務(wù)的會(huì)話(huà)狀態(tài)

    這篇文章主要介紹了ASP.NET如何使用web服務(wù)的會(huì)話(huà)狀態(tài),使用一個(gè)GridView中的會(huì)話(huà)對(duì)象來(lái)展示最近的計(jì)算結(jié)果,感興趣的小伙伴們可以參考一下
    2015-11-11
  • 淺析ASP.NET路由模型工作原理

    淺析ASP.NET路由模型工作原理

    很多人知道Asp.Net中路由怎么用的,卻不知道路由模型內(nèi)部的運(yùn)行原理,今天我就給大家吹下ASP.NET的路由模塊是如何工作的
    2016-03-03
  • ASP.NET MVC 中實(shí)現(xiàn)基于角色的權(quán)限控制的處理方法

    ASP.NET MVC 中實(shí)現(xiàn)基于角色的權(quán)限控制的處理方法

    在ASP.NET MVC中,通過(guò)使用其所提供的內(nèi)置
    2013-03-03
  • ASP.NET驗(yàn)證碼實(shí)現(xiàn)(附源碼)

    ASP.NET驗(yàn)證碼實(shí)現(xiàn)(附源碼)

    這篇文章主要介紹了ASP.NET驗(yàn)證碼實(shí)現(xiàn)過(guò)程,并為大家分享了源碼下載,感興趣的小伙伴們可以參考一下
    2015-11-11
  • asp.net 半角全角轉(zhuǎn)化工具

    asp.net 半角全角轉(zhuǎn)化工具

    asp.net下半角和全角字符轉(zhuǎn)換工具實(shí)現(xiàn)代碼
    2008-12-12
  • 仿vs實(shí)現(xiàn)WPF好看的進(jìn)度條

    仿vs實(shí)現(xiàn)WPF好看的進(jìn)度條

    由于WPF自帶的進(jìn)度條其實(shí)不怎么好看,而且沒(méi)啥視覺(jué)效果。下面給大家分享的是仿VS的進(jìn)度條效果的代碼,有需要的小伙伴可以參考下。
    2015-06-06
  • 詳解Spring Boot 中使用 Java API 調(diào)用 lucene

    詳解Spring Boot 中使用 Java API 調(diào)用 lucene

    這篇文章主要介紹了詳解Spring Boot 中使用 Java API 調(diào)用 lucene,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • ASP.NET使用TreeView顯示文件的方法

    ASP.NET使用TreeView顯示文件的方法

    這篇文章主要介紹了ASP.NET使用TreeView顯示文件的方法,包括控件的使用、頁(yè)面加載代碼及節(jié)點(diǎn)事件,是WEB應(yīng)用程序中非常實(shí)用的技巧,需要的朋友可以參考下
    2014-09-09
  • .NET?Core中使用gRPC的方法

    .NET?Core中使用gRPC的方法

    gRPC 一開(kāi)始由 google 開(kāi)發(fā),是一款語(yǔ)言中立、平臺(tái)中立、開(kāi)源的遠(yuǎn)程過(guò)程調(diào)用(RPC)系統(tǒng),所以叫g(shù)(google)RPC,這篇文章主要介紹了.NET?Core中使用gRPC,需要的朋友可以參考下
    2022-09-09

最新評(píng)論