比AutoMapper輕量快速簡潔的實體映射庫YeaJur.Mapper ...
之前在做實體深拷貝時一直使用的AutoMapper,但是總是覺得其寫法比繁瑣,效率也不算很高,自己寫了一個輕量級的實體拷貝庫YeaJur.Mapper,和AutoMapper 測試對比如下
PK項 |
YeaJur.Mapper |
AutoMapper |
||||
版本 |
1.0 |
5.2 |
||||
大小 |
6KB |
247KB |
||||
相同類型深拷貝——實例(json格式) |
[ { "Products": [ { "Id": "7ef9516e-e019-4b54-9636-89fc56b2906f", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223405)/", "Price": 12.32, "SupplierInfo": { "Id": "00293c28-b29a-4e2c-b5a2-6411d8a67d9e", "Name": "YeaJur.Mapper", "Other": null } }, { "Id": "42f8157a-0f25-472b-9b15-15bc876abeff", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223406)/", "Price": 12.32, "SupplierInfo": { "Id": "24e5b9af-5108-462b-924f-b79bf9c1b160", "Name": "YeaJur.Mapper", "Other": null } }, { "Id": "70a3414b-2c76-4417-a47c-4b807301f0f5", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223406)/", "Price": 12.32, "SupplierInfo": { "Id": "1990168f-244d-42b8-8276-7ee08e0829a0", "Name": "YeaJur.Mapper", "Other": null } } ], "SellerInfo": { "Id": "31875b3f-970d-4383-9d2f-b769c3e7cc87", "Name": "YeaJur.Mapper", "Remark": "YeaJur.Mapper" } }, { "Products": [ { "Id": "cd78b31d-0e83-4586-a202-f45349eabe98", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223406)/", "Price": 12.32, "SupplierInfo": { "Id": "e4ade655-22e3-4850-96dc-02f7059479b8", "Name": "YeaJur.Mapper", "Other": null } }, { "Id": "20d2219b-d520-4c58-840e-c11b10e8878f", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223406)/", "Price": 12.32, "SupplierInfo": { "Id": "3817b018-369a-4413-b0df-cde5a877d0d7", "Name": "YeaJur.Mapper", "Other": null } }, { "Id": "b83d5db6-1216-4a56-a448-d5c478a30b32", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223406)/", "Price": 12.32, "SupplierInfo": { "Id": "321b2dad-7d66-42d0-b75d-c019cd297a1b", "Name": "YeaJur.Mapper", "Other": null } } ], "SellerInfo": { "Id": "c7e7f104-3484-4640-9003-ad6ddd5cb3dc", "Name": "YeaJur.Mapper", "Remark": "YeaJur.Mapper" } }, { "Products": [ { "Id": "1d8c2158-008c-4ad1-8803-915a5e32c695", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223406)/", "Price": 12.32, "SupplierInfo": { "Id": "58c89199-c1fc-4c94-8d74-3ee3db5f074b", "Name": "YeaJur.Mapper", "Other": null } }, { "Id": "311ca89e-be6c-4bcf-8730-12abb1388319", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223406)/", "Price": 12.32, "SupplierInfo": { "Id": "87f3c2ce-d6bd-4cfb-bcf0-9a48615b223c", "Name": "YeaJur.Mapper", "Other": null } }, { "Id": "5d23505c-206a-4bdc-9086-92532fcf37d7", "Name": "YeaJur.Mapper", "Count": 32, "CreateTime": "/Date(1484796223406)/", "Price": 12.32, "SupplierInfo": { "Id": "e5582774-ca39-43e0-8e78-9511ea96061b", "Name": "YeaJur.Mapper", "Other": null } } ], "SellerInfo": { "Id": "9f1b4e00-bf00-4946-8e8a-a2780c118079", "Name": "YeaJur.Mapper", "Remark": "YeaJur.Mapper" } } ] |
|||||
相同類型深拷貝——寫法 |
var model = list.Map(); |
AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap<List<Order>, List<Order>>(); }); var model = AutoMapper.Mapper.Map<List<Order>>(list); |
||||
相同類型深拷貝——運行結果樣例 |
|
|||||
相同類型深拷貝——運行平均速度 |
12.2ms(包括創建實體時間) |
160.2ms(包括創建實體時間) |
||||
不同類型深拷貝——實例(json格式) |
{ "Id": "b0ade704-0a59-481a-b4b8-bf64a2fdaa8f", "Name": "YeaJur.Mapper", "Remark": "YeaJur.Mapper.Seller" } 和 { "Id": "b0ade704-0a59-481a-b4b8-bf64a2fdaa8f", "Name": "YeaJur.Mapper", "Other": null } |
|||||
不同類型深拷貝——寫法 |
Supplier model2 = model.Map<Supplier>();
或
Supplier model2 = model.Map<Seller,Supplier>(); |
AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap<Seller, Supplier>(); }); Supplier model2 = AutoMapper.Mapper.Map<Seller, Supplier>(model); |
||||
不同類型深拷貝——運行結果樣例 |
|
|||||
不同類型深拷貝——運行平均速度 |
9.6ms(包括創建實體時間) |
174.6ms(包括創建實體時間) |
||||
不同類型自定義欄位深拷貝——實例(json格式) |
{ "Id": "b0ade704-0a59-481a-b4b8-bf64a2fdaa8f", "Name": "YeaJur.Mapper.Seller", "Remark": "YeaJur.Mapper.Seller" } 和 { "Id": "b0ade704-0a59-481a-b4b8-bf64a2fdaa8f", "Name": "YeaJur.Mapper", "Other": null } |
|||||
不同類型自定義欄位深拷貝——寫法 |
var dic = new Dictionary<string, string> { {nameof(Seller.Remark), nameof(Supplier.Other)} }; Supplier model2 = model.Map< Supplier>(dic); 或 Supplier model2 = model.Map<Seller, Supplier>(dic); |
AutoMapper.Mapper.Initialize(cfg => { cfg.CreateMap<Seller, Supplier>(); cfg.ReplaceMemberName(nameof(Seller.Remark), nameof(Supplier.Other)); }); Supplier model2 = AutoMapper.Mapper.Map<Seller, Supplier>(model); |
||||
不同類型自定義欄位深拷貝——運行結果樣例 |
|
|||||
不同類型自定義欄位深拷貝——運行平均速度 |
10.2ms(包括創建實體時間) |
176.4ms(包括創建實體時間) |
在項目的nuget中搜索 Yeajur.Mapper 即可下載使用
https://www.nuget.org/packages/YeaJur.Mapper