心情小札:近期換了工作,苦逼於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>() { "aaa@bb.com", "acx@cc.com" } }, new Person { Id = Guid.NewGuid().ToString(), Name = "John2", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" } }, new Person { Id = Guid.NewGuid().ToString(), Name = "John3", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" } }, new Person { Id = Guid.NewGuid().ToString(), Name = "John4", Age = 22, Address = new Address() { Phone = "1880393", Street = "Shanghai", ZipCode = "121212" }, Emails = new List<string>() { "aaa@bb.com", "acx@cc.com" } } }; 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會自動按照"映射過的基礎類型"動態的幫你獲取想要的數據。
本文的代碼:示例
說明:代碼淺顯易懂,然人非完人,編碼不規范的地方、手誤、或代碼存在缺陷、或嚴重性能問題,希望園友們批評指正。