当前位置:网站首页>. Net automapper use
. Net automapper use
2022-07-07 22:18:00 【freelooppowter】
When decomposing complex software systems , One of the technologies most used by software designers is layering . Layered right has many advantages : Such as improving reusability 、 Low coupling , But it also has its shortcomings : For example, too many levels will affect performance 、 Sometimes it brings us cascading modifications .
It will bring cascading modifications. How to understand ? For example, when the demand changes, a data field displayed on the user interface should be added , You must add corresponding fields to the data , You must also make corresponding changes at each layer between the user interface and the database . There will be different data entity classes in different levels ( Some classes also include methods ), In order to realize data transmission between these entity classes, the transformation before the class must be carried out , This kind of conversion can complete the task by handwritten code for one-to-one conversion , Just like the picture below , And you have to work for the corresponding List<T> Class to write the corresponding conversion method , There is a simpler and more convenient solution for you ? That's what the title says AutoMapper, From the name, we can see that this is a tool for mapping .
Go straight to the code , First, define the entity class that needs to be converted , The second step is to inherit Profile Interface , To register .
using AutoMapper; namespace WindowsFormsApp1 { /// <summary> /// Imitate the data entity classes that need to be converted /// </summary> public class Source { public int SomeValue { get; set; } public string AnotherValue { get; set; } } /// <summary> /// Imitate the data entity classes that need to be converted /// </summary> public class Destination { public int SomeValue { get; set; } } /// <summary> /// adopt Profile register /// tell AutoMapper Which classes need to be converted , If A convert to B,B It should also be converted into A, Then you need to write two /// </summary> public class SourceProfile : Profile { public SourceProfile() { CreateMap<Source, Destination>(); CreateMap<Destination, Source>(); } } }
The third step is to Profile The mapping relationship written in the Chinese book is added to Mapper example , The routine needs to write a method to get the unique Mapper example ( The singleton pattern ), The mapping relationship is configured in the instance .
using AutoMapper; namespace WindowsFormsApp1 { public class AutoMapperConfigure { public static IMapper GetMapper() { var config = new MapperConfiguration(cfg => { // Scan the current assembly cfg.AddMaps(System.AppDomain.CurrentDomain.GetAssemblies()); }); var mapper = config.CreateMapper(); return mapper; } } }
Step 4 call , By default ,AutoMapper Map based on the same field name , And is Case insensitive Of . You can also modify the configuration , Realization AutoMapper Provided naming rule mapping .LowerUnderscoreNamingConvention
and PascalCaseNamingConvention
yes AutoMapper Two naming rules provided . The former is named lowercase and contains underscores , The latter is Pascal's naming rule ( Capitalize each word ).
private void promptMessageButton_Click(object sender, EventArgs e) { Source source = new Source() { SomeValue = 1, AnotherValue = "Another" }; var mapper = AutoMapperConfigure.GetMapper(); Destination destination = mapper.Map<Destination>(source); MessageBox.Show(destination.SomeValue.ToString()); }
Like the first screenshot, the more complex one involves the conversion between enumeration and enumeration description , even to the extent that List<string> convert to string, It can also be used. AutoMapper Realization . You can call the written static method on the converted member property , To realize the quasi exchange according to the rules , Here's the picture .
Reference material :https://www.cnblogs.com/gl1573/p/13098031.html
Interested students can also see the official documents , There are also examples :https://docs.automapper.org/en/latest/
The students who read the blog suggest writing it by themselves , To borrow a sentence from Xunzi : No smell, no smell , You can't hear it, you can't see it , If you don't know what you see , What you know is not what you do . If you have any questions, please discuss with me .
边栏推荐
- 反爬通杀神器
- Qt编写物联网管理平台39-报警联动
- 应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
- How polardb-x does distributed database hotspot analysis
- Record a garbled code during servlet learning
- Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
- 戴森官方直营店免费造型服务现已开放预约 先锋科技诠释护发造型理念,助力消费者解锁多元闪耀造型
- 使用 BlocConsumer 同时构建响应式组件和监听状态
- Programming mode - table driven programming
- An in-depth understanding of fp/fn/precision/recall
猜你喜欢
使用 BlocConsumer 同时构建响应式组件和监听状态
Validutil, "Rethinking the setting of semi supervised learning on graphs"
How does win11 time display the day of the week? How does win11 display the day of the week today?
Open source OA development platform: contract management user manual
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
100million single men and women "online dating", supporting 13billion IPOs
null == undefined
[JDBC Part 1] overview, get connection, CRUD
Tcp/ip protocol stack
Crawler (17) - Interview (2) | crawler interview question bank
随机推荐
OpenGL homework - Hello, triangle
Jerry's fast pairing does not support canceling pairing [article]
How to realize the movement control of characters in horizontal game
【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式
cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
Navicat connect 2002 - can't connect to local MySQL server through socket '/var/lib/mysql/mysql Sock 'solve
Kirin Xin'an operating system derivative solution | storage multipath management system, effectively improving the reliability of data transmission
[azure microservice service fabric] how to transfer seed nodes in the service fabric cluster
你可曾迷茫?曾经的测试/开发程序员,懵懂的小菜C鸟升级......
QT compile IOT management platform 39 alarm linkage
Debugging and handling the problem of jamming for about 30s during SSH login
Time standard library
大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
SAR image quality evaluation
How does win11 unblock the keyboard? Method of unlocking keyboard in win11
How to turn on win11 game mode? How to turn on game mode in win11
三元表达式、各生成式、匿名函数
Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]
Solve the problem of uni in uni app Request sent a post request without response.
DNS series (I): why does the updated DNS record not take effect?