標籤:
心情小劄:近期換了工作,苦逼於22:00後下班,房間一篇狼藉~ 小翠鄙視到:"你就適合生活在垃圾堆中!!!"
晚上瀏覽部落格園 看到一篇非常實用的部落格:.NET平台開源項目預覽(14)最快的對象映射組件Tiny Mapper,花了10分鐘簡單瞭解了一下。
看評論也是挺有價值,同時也看到許多新手同學問道在實際項目中使用的情況。
下面就原作者的代碼的基礎上略作調整,闡述一下在實際項目情境中的使用:
第一步:瞭解類庫方法:TinyMapper 主要有兩個函數:
TinyMapper.Bind<T1, T2>();//綁定映射關係
TinyMapper.Map<T>(obj);//從對象擷取想要的對象
第二步:初始化Mapping設定
說明:類似的Mapping設定無非兩種模式:代碼靜態對象初始化模式,xml配置模式,很榮幸TinyMapper支援的是靜態字典。
public static class TinyMapContext { public static void InitMapping() { TinyMapper.Bind<Person, PersonDto>(); TinyMapper.Bind<Person, PersonDto>(config => { config.Ignore(x => x.Id);//忽略ID欄位 config.Bind(x => x.Name, y => y.UserName);//將源類型和目標類型的欄位對應綁定起來 config.Bind(x => x.Age, y => y.Age);//將源類型和目標類型的欄位對應綁定起來 }); TinyMapper.Bind<Person, PersonDto>(config => { config.Ignore(x => x.Id);//忽略ID欄位 //將源類型和目標類型的欄位對應綁定起來 config.Bind(x => x.Name, y => y.UserName); config.Bind(x => x.Age, y => y.Age); config.Bind(x => x.Address, y => y.Address); config.Bind(x => x.Emails, y => y.Emails); }); TinyMapper.Bind<PersonDto,Person>(); TinyMapper.Bind<PersonDto,Person>(config => { config.Bind(x => x.Id,y=>y.Id); config.Bind(x => x.UserName, y => y.Name); config.Bind(x => x.Age, y => y.Age); }); TinyMapper.Bind<PersonDto,Person>(config => { config.Bind(x => x.Id,y=>y.Id);//忽略ID欄位 //將源類型和目標類型的欄位對應綁定起來 config.Bind(x =>x.UserName,y=> y.Name); config.Bind(x => x.Age, y => y.Age); config.Bind(x => x.Address, y => y.Address); config.Bind(x => x.Emails, y => y.Emails); }); } public static T GetMapObject<T>(object obj) where T:class { return TinyMapper.Map<T>(obj); } }
說明:以上mapping映射中,針對於原作者的代碼,額外添加了:由PersonDto=》Person的映射關係。
T GetMapObject<T>(object obj) where T:class 的作用會在後面的代碼中體現出來。 簡單一個方法,威力不可小噓~~
第三步:DtoModel -》Model
從資料庫模型映射到領域模型:
var p = TinyMapContext.GetMapObject<Person>(personDto);
第四步:Model-》DtoModel
從領域模型到資料庫模型:
var personDto = TinyMapContext.GetMapObject<PersonDto>(person);
第五步:List<Model>=>List<DtoModel> 或者List<DtoModel>=>List<Model>
/// <summary> /// 測試清單對象。 /// </summary> static void Test4() { List<Person> personList = new List<Person>(){ new Person { Id = Guid.NewGuid().ToString(), Name = "John1", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "[email protected]", "[email protected]" } }, new Person { Id = Guid.NewGuid().ToString(), Name = "John2", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "[email protected]", "[email protected]" } }, new Person { Id = Guid.NewGuid().ToString(), Name = "John3", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "[email protected]", "[email protected]" } }, new Person { Id = Guid.NewGuid().ToString(), Name = "John4", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "[email protected]", "[email protected]" } } }; var personDtoList = TinyMapContext.GetMapObject<List<PersonDto>>(personList); foreach (var item in personDtoList) { DebugOutputUtil.DebugOutput(item.UserName); } var pList = TinyMapContext.GetMapObject<List<Person>>(personDtoList); foreach (var item in pList) { DebugOutputUtil.DebugOutput(item.Name); } }
特別說明:對於擷取清單類型的對象,不需要在靜態初始化映射中額外添加 IList類型的對象。 TinyMapper會自動按照"映射過的基礎類型"動態幫你擷取想要的資料。
本文的代碼:樣本
說明:代碼淺顯易懂,然人非完人,編碼不規範的地方、手誤、或代碼存在缺陷、或嚴重性能問題,希望園友們批評指正。
.NET平台開源項目預覽-最快的對象映射組件Tiny Mapper之項目實踐