Cola.Utils 3.0.0

dotnet add package Cola.Utils --version 3.0.0
                    
NuGet\Install-Package Cola.Utils -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cola.Utils" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cola.Utils" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Cola.Utils" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Cola.Utils --version 3.0.0
                    
#r "nuget: Cola.Utils, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Cola.Utils&version=3.0.0
                    
Install Cola.Utils as a Cake Addin
#tool nuget:?package=Cola.Utils&version=3.0.0
                    
Install Cola.Utils as a Cake Tool

Mapster 映射转换

  1. 基础类
public class Student
{
    public string StuName { get; set; }
    public string StuAddress { get; set; }
}
public class Student_DbModel
{
    public string StudentName { get; set; }
    public string StudentAddress { get; set; }
}
  1. Startup.cs 注册全局映射 Config,也可以不注册
// 使用 OdinInjectCore 注入
services.AddOdinTypeAdapter(opt =>
    {
        opt.ForType<ErrorCode_DbModel, ErrorCode_Model>()
                .Map(dest => dest.ShowMessage, src => src.CodeShowMessage);
    });
// 使用 .net core DI 注入
services.AddSingleton<ITypeAdapterMapster>(provider => new TypeAdapterMapster(opt =>
    {
        opt.ForType<ErrorCode_DbModel, ErrorCode_Model>()
                .Map(dest => dest.ShowMessage, src => src.CodeShowMessage);
    }));
  1. 使用时获取全局注册 Config
// 使用 OdinInjectCore 获取 TypeAdapterMapster
var mapsterConfig = OdinInjectCore.GetService<ITypeAdapterMapster>().GetConfig();
// 使用 .net core 默认 DI 获取 TypeAdapterMapster
var mapsterConfig = services.BuildServiceProvider().GetService<ITypeAdapterMapster>().GetConfig();
  1. 获取数据准备映射转换对象
// 通过 SqlSugar 获取数据库中的数据
List<Student_DbModel> stuDbModels = DbScoped.Sugar.Queryable<Student_DbModel>().ToList();
Student_DbModel stuDbModel = stuDbModels[0];
  1. 对象映射转换
// 使用全局映射配置转换目标对象类型
var stu = stuDbModel.OdinAdapter<Student_DbModel, Student>(
        OdinInjectCore.GetService<ITypeAdapterMapster>().GetConfig()
    );

// 使用自定义映射配置转换目标对象类型
// 需要注意的是: 因为没有传全局映射配置, 此时虽然全局配置也有 StudentName 属性映射 StuName 的配置,但是会以当前自定义配置为准
var stu = stuDbModel.OdinAdapter<Student_DbModel, Student>(
        opt =>
        {
            opt.Map(dest => dest.StuName, src => src.StudentName);
        }
    );

// 使用自定义映射+全局映射配置转换目标对象对象类型
// 需要注意的是: 因为全局映射配置中有 StudentName 属性映射 StuName 的配置,所以当自定义配置与全局配置都存在时,以全局配置为准
var stu = stuDbModel.OdinAdapter<Student_DbModel, Student>(
        opt =>
        {
            opt.Map(dest => dest.StuName, src => src.StudentName);
            opt.Map(dest => dest.StuAddress, src => src.StudentAddress);
        },
        OdinInjectCore.GetService<ITypeAdapterMapster>().GetConfig()
    );
  1. 集合映射转换

    将 stuDbModels List<ErrorCode_DbModel> 集合映射转换为 stuLst List<Student> 类型的集合

泛型参数说明:

参数名称 说明
Student_DbModel 映射的源类型
Student 转换的目标类型
List<Student> 最终转换后的集合类型
// 使用全局映射配置转换目标对象类型
var stuLst = stuDbModels.OdinCollectionAdapter<Student_DbModel, Student, List<Student>>(
        OdinInjectCore.GetService<ITypeAdapterMapster>().GetConfig()
    );

// 使用自定义映射配置转换目标对象类型
// 需要注意的是: 因为没有传全局映射配置, 此时虽然全局配置也有 StudentName 属性映射 StuName 的配置,但是会以当前自定义配置为准
var stuLst = stuDbModels.OdinCollectionAdapter<Student_DbModel, Student, List<Student>>(
        opt =>
        {
            opt.Map(dest => dest.StuName, src => src.StudentName);
        }
    );

// 使用自定义映射+全局映射配置转换目标对象对象类型
// 需要注意的是: 因为全局映射配置中有 StudentName 属性映射 StuName 的配置,所以当自定义配置与全局配置都存在时,以全局配置为准
var stuLst = stuDbModels.OdinCollectionAdapter<Student_DbModel, Student, List<Student>>(
        opt =>
        {
            opt.Map(dest => dest.StuName, src => src.StudentName);
            opt.Map(dest => dest.StuAddress, src => src.StudentAddress);
        },
        OdinInjectCore.GetService<ITypeAdapterMapster>().GetConfig()
    );

关于 Mapster 更详细的用法,请参照 Mapster 官网。

具体封装代码详见 Github

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on Cola.Utils:

Package Downloads
Cola.Core

Cola.Core.

Cola.Models.Core

Cola.Models.Core.

Cola.SnowFlake

Cola.SnowFlake.

Cola.Authen

Cola.Authen.

Cola.Swagger

Cola.Swagger.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 327 a month ago
2.0.1 207 a month ago
2.0.0 287 a month ago
1.1.0 291 a month ago