当前位置:网站首页>4. Object mapping Mapster
4. Object mapping Mapster
2022-07-07 05:33:00 【MASA team】
Preface
We often encounter object mapping in projects , Such as Model and Dto Mapping between , Or a deep copy of an object , We need to realize these by ourselves . here , There will be a lot of code for initializing objects in the project , The code is pretty boring to write , Is there any way to reduce our workload , So that we can spend time on business functions ?
at present ,.Net Object mapping framework in , Powerful and excellent object mapping frameworks already exist , The most used ones are :
Speaking of the object mapping framework , What people think of most is AutoMapper
, Maybe a lot of people Mapster
Never heard of it , But there's no denying it Mapster
It is really a good object mapping framework , However, due to the lack of Chinese documents , As a result, the popularity in China is not very high , Today we will introduce Mapster
What functions are provided , How to use it in a project ,Masa
Provided Mapster
What else has been done ?
Mapster brief introduction
Mapster
It's a simple to use , Powerful object mapping framework , since 2014 It's past since the year of open source 8 A year , Up to now ,github I already have 2.6k Of star, And keep it every year 3 Distribution frequency of times , Its function and AutoMapper similar , Provide object to object mapping 、 And support IQueryable Mapping to objects , And AutoMapper
comparison , Better performance in terms of speed and memory usage , You can only use 1/3 In the case of memory 4 Times performance improvement , Let's take a look at Mapster
How to use ?
preparation
Create a new console project
Assignment.Mapster
, And installMapster
dotnet add package Mapster --version 7.3.0
Map to new object
The new class
UserDto
public class UserDto { public int Id { get; set; } public string Name { get; set; } public uint Gender { get; set; } public DateTime BirthDay { get; set; } }
Create a new anonymous object , As the source of the object to be converted
var user = new { Id = 1, Name = "Tom", Gender = 1, BirthDay = DateTime.Parse("2002-01-01") };
take user The source object is mapped to the target object (UserDto)
var userDto = user.Adapt<UserDto>(); Console.WriteLine($" Map to new object ,Name: {userDto.Name}");
Run the console program to verify that the conversion is successful :
data type
In addition to providing object-to-object mapping , It also supports data type conversion , Such as :
Basic types
Provide the function of type mapping , similar Convert.ChangeType()
string res = "123"; decimal i = res.Adapt<decimal>(); //equal to (decimal)123; Console.WriteLine($" The result is :{i == int.Parse(res)}");
Run the console program :
Enumeration type
Map enumerations to numeric types , It also supports string to enumeration and enumeration to string mapping , Than .NET The default implementation of is twice as fast
var fileMode = "Create, Open".Adapt<FileMode>();// be equal to FileMode.Create | FileMode.Open Console.WriteLine($" The result of enumeration type conversion is :{fileMode == (FileMode.Create | FileMode.Open)}");
Run the console program to verify that the conversion is successful :
Queryable Expand
Mapster Provides Queryable An extension of , Used to implement DbContext On demand lookup for , for example :
The new class
UserDbContext
using Assignment.Mapster.Domain; using Microsoft.EntityFrameworkCore; namespace Assignment.Mapster.Infrastructure; public class UserDbContext : DbContext { public DbSet<User> User { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var dataBaseName = Guid.NewGuid().ToString(); optionsBuilder.UseInMemoryDatabase(dataBaseName);// Use memory database , Convenient test } }
The new class
User
public class User { public int Id { get; set; } public string Name { get; set; } public uint Gender { get; set; } public DateTime BirthDay { get; set; } public DateTime CreationTime { get; set; } public User() { CreationTime = DateTime.Now; } }
Using a Queryable Extension method of
ProjectToType
using (var dbContext = new UserDbContext()) { dbContext.Database.EnsureCreated(); dbContext.User.Add(new User() { Id = 1, Name = "Tom", Gender = 1, BirthDay = DateTime.Parse("2002-01-01") }); dbContext.SaveChanges(); var userItemList = dbContext.User.ProjectToType<UserDto>().ToList(); }
Run the console program to verify that the conversion is successful :
besides ,Mapster
Pre mapping is also provided / post-processing , Copy and merge and mapping configuration nesting support , Details can be viewed file , since Mapster
Already so powerful , Then I can use it directly , Why even use Masa
Provided Mapper Well ?
What is? Masa.Contrib.Data.Mapping.Mapster?
Masa.Contrib.Data.Mapping.Mapster
Is based on Mapster
An object to object mapper for , And in the original Mapster
Add auto get and use best constructor mapping based on , Support nested mapping , Reduce the workload of mapping .
Mapping rule
When the target object does not have a constructor : Use an empty constructor , Mapping to fields and attributes .
Multiple constructors exist for the target object : Get the best constructor mapping
Best constructor : The number of constructor parameters of the target object is found in descending order , The parameter names are consistent ( Case insensitive ) And the parameter type is consistent with the source object attribute
preparation
Create a new console project
Assignment.Masa.Mapster
, And installMasa.Contrib.Data.Mapping.Mapster
,Microsoft.Extensions.DependencyInjection
dotnet add package Masa.Contrib.Data.Mapping.Mapster --version 0.4.0-rc.4 dotnet add package Microsoft.Extensions.DependencyInjection --version 6.0.0
The new class
OrderItem
public class OrderItem { public string Name { get; set; } public decimal Price { get; set; } public int Number { get; set; } public OrderItem(string name, decimal price) : this(name, price, 1) { } public OrderItem(string name, decimal price, int number) { Name = name; Price = price; Number = number; } }
The new class
Order
public class Order { public string Name { get; set; } public decimal TotalPrice { get; set; } public List<OrderItem> OrderItems { get; set; } public Order(string name) { Name = name; } public Order(string name, OrderItem orderItem) : this(name) { OrderItems = new List<OrderItem> { orderItem }; TotalPrice = OrderItems.Sum(item => item.Price * item.Number); } }
Modify the class
Program
using Assignment.Masa.Mapster.Domain.Aggregate; using Masa.BuildingBlocks.Data.Mapping; using Masa.Contrib.Data.Mapping.Mapster; using Microsoft.Extensions.DependencyInjection; Console.WriteLine("Hello Masa Mapster!"); IServiceCollection services = new ServiceCollection(); services.AddMapping(); var request = new { Name = "Teach you to learn Dapr ……", OrderItem = new OrderItem("Teach you to learn Dapr hand by hand", 49.9m) }; var serviceProvider = services.BuildServiceProvider(); var mapper = serviceProvider.GetRequiredService<IMapper>(); var order = mapper.Map<Order>(request); Console.WriteLine($"{nameof(Order.TotalPrice)} is {order.TotalPrice}");// Console output 49.9 Console.ReadKey();
If the conversion is successful ,TotalPrice The value should be 49.9, Then we run the console program to verify whether the conversion is successful :
How to achieve
We mentioned above Masa.Contrib.Data.Mapping.Mapster
You can automatically get and use the best constructor mapping , And then complete the object to object mapping , So how does it come true ? Will it affect the performance ?
It is used to automatically obtain and use the best constructor mapping Mapster
The function of constructor mapping provided by , By specifying the constructor , Complete object to object mapping .
see file
summary
at present Masa.Contrib.Data.Mapping.Mapster
Its function is relatively weak , The current version is similar to Mapster
It only adds a function of automatically obtaining and using the best constructor , Let's easily complete the mapping when facing a class that has no empty constructor and has multiple constructors , There is no need to write an extra line of code .
But I think Masa
Version of Mapping The biggest benefit is that the project depends on BuildingBlocks
Under the IMapper
, instead of Mapster
, This makes our project separate from the specific mapper implementation , If we are asked that the project must use AutoMapper
, Just implement AutoMapper
Version of IMapper
that will do , No need to change too many business codes , Just replace the referenced package , This is also BuildingBlocks
The charm of
Source code of this chapter
Assignment04
https://github.com/zhenlei520/MasaFramework.Practice
Open source address
MASA.BuildingBlocks:https://github.com/masastack/MASA.BuildingBlocks
MASA.Contrib:https://github.com/masastack/MASA.Contrib
MASA.Utils:https://github.com/masastack/MASA.Utils
MASA.EShop:https://github.com/masalabs/MASA.EShop
MASA.Blazor:https://github.com/BlazorComponent/MASA.Blazor
If you treat our MASA Framework Interested in , Whether it's code contribution 、 Use 、 carry Issue, Welcome to contact us
边栏推荐
- LabVIEW is opening a new reference, indicating that the memory is full
- The navigation bar changes colors according to the route
- 论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
- AIDL 与Service
- [question] Compilation Principle
- 2039: [Bluebridge cup 2022 preliminaries] Li Bai's enhanced version (dynamic planning)
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
- 基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
- MySQL数据库学习(7) -- pymysql简单介绍
- Complete code of C language neural network and its meaning
猜你喜欢
导航栏根据路由变换颜色
Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
漏电继电器JD1-100
《2》 Label
JVM(二十) -- 性能监控与调优(一) -- 概述
Make web content editable
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
Use Zhiyun reader to translate statistical genetics books
随机推荐
Harmonyos fourth training
A cool "ghost" console tool
JVM (19) -- bytecode and class loading (4) -- talk about class loader again
DOM node object + time node comprehensive case
2039: [Bluebridge cup 2022 preliminaries] Li Bai's enhanced version (dynamic planning)
Is the human body sensor easy to use? How to use it? Which do you buy between aqara green rice and Xiaomi
Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
Jhok-zbg2 leakage relay
How can professional people find background music materials when doing we media video clips?
Dbsync adds support for mongodb and ES
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation
京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
AOSP ~binder communication principle (I) - Overview
English语法_名词 - 所有格
Knapsack problem unrelated to profit (depth first search)
MySQL数据库学习(8) -- mysql 内容补充
K6el-100 leakage relay
利用OPNET进行网络仿真时网络层协议(以QoS为例)的使用、配置及注意点
Under the trend of Micah, orebo and apple homekit, how does zhiting stand out?
Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET