TinyMapper 使用總結

來源:互聯網
上載者:User

標籤:name   http   ram   基本   配置   nuget   param   img   ati   

初識TinyMapper

TinyMapper是開源的對象映射架構,功能和AutoMapper一樣。官網介紹,TinyMapper映射效率很高,是官方給的比較結果:

TinyMapper使用簡單,只有Bind,Mapper兩個操作;而且支援的配置也很簡單(很有限)。下面就讓我們來進一步認識下TinyMapper。

安裝TinyMapper

通過Nuget安裝TinyMapper,本次使用版本為2.0.8。

TinyMapper映射

1. 綁定映射關係

public static void Bind<TSource, TTarget>();public static void Bind<TSource, TTarget>(Action<IBindingConfig<TSource, TTarget>> config);

2. 執行映射,擷取映射結果

public static TTarget Map<TSource, TTarget>(TSource source, TTarget target = default(TTarget));public static TTarget Map<TTarget>(object source);

注意:TinyMapper的映射對象必須是Public類型。

簡易對應

建立兩個映射對象:

public class Product{public Guid Id { get; set; }public string Name { get; set; }public decimal Price { get; set; }}public class ProductDTO{public Guid Id { get; set; }public string Name { get; set; }public decimal Price { get; set; }}

綁定映射關係並執行映射:

[TestMethod]public void TestSimple(){var product = new Product(){Id = Guid.NewGuid(),Name = "Product" + DateTime.Now.Ticks,Price = 12};//1. 建立映射關係TinyMapper.Bind<Product, ProductDTO>();
//TinyMapper.Bind<Product, ProductDTO>(); //直接建立對象綁定關係也是可以的//2. 執行映射var productDto = TinyMapper.Map<ProductDTO>(product);Assert.IsNotNull(productDto);Assert.AreEqual(12, productDto.Price);Assert.IsNotNull(productDto.Name);}
集合映射

仍然使用上面的映射對象,集合驗證方式如下:

[TestMethod]public void TestList(){var products = new List<Product>(){new Product(){Id = Guid.NewGuid(),Name = "Product" + DateTime.Now.Ticks,Price = 5},new Product(){Id = Guid.NewGuid(),Name = "Product" + DateTime.Now.Ticks,Price = 10}};//1. 建立映射關係TinyMapper.Bind<List<Product>, List<ProductDTO>>();//2. 執行映射var productDtos = TinyMapper.Map<List<Product>>(products);Assert.IsNotNull(productDtos);Assert.AreEqual(2, productDtos.Count);}

  

執行映射轉換時,TinyMapper不支援介面類型:IList<T>, ICollection<T>, IEnumerable<T>.

TinyMapper.Map<ICollection<Product>>(products); //執行時會拋出異常
映射配置

TinyMapper支援簡單的映射配置:

指定欄位對應
public class Product{public Guid Id { get; set; }public string Name { get; set; }public decimal Price { get; set; }}public class ProductDTO{public string Id { get; set; }public string Name { get; set; }public decimal Money { get; set; }}[TestMethod]public void TestMapperConfig(){var product = new Product(){Id = Guid.NewGuid(),Name = "Product" + DateTime.Now.Ticks,Price = 12};//1. 建立映射關係TinyMapper.Bind<Product, ProductDTO>(cfg =>{cfg.Bind(src => src.Price, dest => dest.Money); //指定欄位對應});//2. 執行映射var productDto = TinyMapper.Map<ProductDTO>(product);Assert.IsNotNull(productDto);Assert.AreEqual(12, productDto.Money);}
映射時不管某些欄位
TinyMapper.Bind<Product, ProductDTO>(cfg =>{cfg.Ignore(src => src.Price); //映射時不管某些欄位});

注意:TinyMapper預設會根據欄位名稱進行映射,而不管欄位的類型。也就是說,如果來源物件欄位名稱與目標對象欄位名稱一致,但是類型不一致且不能進行強制轉換時,會拋出異常。

TinyMapper簡單封裝
public class TinyMapperUtil{/// <summary>/// 建立映射關係/// </summary>/// <typeparam name="TSource"></typeparam>/// <typeparam name="TDestination"></typeparam>public static void Bind<TSource, TDestination>(){TinyMapper.Bind<TSource, TDestination>();}/// <summary>/// 映射對象/// </summary>/// <typeparam name="TDestination"></typeparam>/// <param name="source"></param>/// <returns></returns>public static TDestination Map<TDestination>(object source){if (source == null){return default(TDestination);}else{return TinyMapper.Map<TDestination>(source);}}}
總結

以上基本是TinyMapper提供的所有特性及操作。相比AutoMapper,功能很有限,不支援扁平映射,也不支援介面集合轉換等。但使用起來還是很簡單的,仍然可以考慮在項目中使用。

相關資料可以參考官網介紹:http://tinymapper.net/

TinyMapper 使用總結

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.