<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Automapper.jsJavascript 版的對(duì)象映射

          聯(lián)合創(chuàng)作 · 2023-09-22 02:56

          在C#項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常需要很多對(duì)象模型,如視圖模型、傳輸模型、領(lǐng)域模型、實(shí)體模型等,我相信很多開(kāi)發(fā)的朋友都明白最頭疼的無(wú)法使模型之間的賦值,傳統(tǒng)的方法就是一一賦值。終于春天來(lái)了,AutoMapper 的出現(xiàn)讓我們解放了,各種映射完全秒秒鐘的事情。

          最近在Javascript開(kāi)發(fā)過(guò)程中也遇到了相同的需求,果斷 github 搜索一番,納尼?沒(méi)有!??!沒(méi)辦法,誰(shuí)叫我是代碼工呢,果斷擼一把,不一會(huì),automapper.js 誕生了。

          使用文檔

          <script src="automapper.js" type="text/javascript"></script>
          • 示例演示

          // # 示例一
          
          var srcObj = { name: "百小僧", age: 25, gender: '男' }; // 源類型
          var destObj= { name: null, age: null, gender: null };   // 目標(biāo)類型
          
          // 創(chuàng)建映射關(guān)系
          automapper.createMap("src","dest");
          // 開(kāi)始映射
          automapper.map("src","dest",srcObj,destObj);
          // 輸出結(jié)果:
          // srcObj => { name: "百小僧", age: 25, gender: '男' }
          // destObj => { name: "百小僧", age: 25, gender: '男' }
          
          
          // # 示例二
          
          var srcObj = { name: "百小僧", age: 25, gender: '男' }; // 源類型
          var destObj = { firstName: null, age: null, sex: null, birthday: null };    // 目標(biāo)類型
          
          // 創(chuàng)建映射關(guān)系
          automapper.createMap("src", "dest")
              .forMember("firstName", function (src, dest) { return "Mr." + src.name })
              .forMember("sex", srcObj.gender)
              .forMember("birthday", '1993-05-27');
          // 開(kāi)始映射
          automapper.map("src", "dest", srcObj, destObj);
          // 輸出結(jié)果:
          // srcObj => { name: "百小僧", age: 25, gender: '男' }
          // destObj => {firstName: "Mr.百小僧", age: 25, sex: "男", birthday: "1993-05-27"}


          automapper.js 目前還是第一版,實(shí)現(xiàn)代碼也非常簡(jiǎn)單

          ; !function (win) {
              "use strict";
          
              var AutoMapper = function () {
                  this.profiles = {};
                  this.version = '1.0.0';
              };
          
              AutoMapper.prototype.createMap = function (srcKey, destKey) {
                  var that = this, combineKey = srcKey + "->" + destKey;
                  if (!that.profiles.hasOwnProperty(combineKey)) that.profiles[combineKey] = {};
          
                  var funcs = {
                      forMember: function (prop, any) {
                          that.profiles[combineKey][prop] = any;
                          return funcs;
                      }
                  };
                  return funcs;
              };
          
              AutoMapper.prototype.map = function (srcKey, destKey, srcObj, destObj) {
                  var that = this, combineKey = srcKey + "->" + destKey;
                  if (!that.profiles.hasOwnProperty(combineKey)) throw "Could not find mapping with a source of " + srcKey + " and a destination of " + destKey;
          
                  var profile = that.profiles[combineKey];
                  for (var prop in destObj) {
                      if (!profile.hasOwnProperty(prop)) destObj[prop] = srcObj.hasOwnProperty(prop) ? srcObj[prop] : destObj[prop];
                      else {
                          var output = profile[prop];
                          if (typeof output === "function") destObj[prop] = output(srcObj, destObj);
                          else destObj[prop] = output;
                      }
                  }
              };
          
              win.automapper = new AutoMapper();
          }(window);
          瀏覽 17
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  青娱乐黄片 | 91久久爽久久爽爽久久片 | 欧美精品卡一操逼 | 成人乱妇无码AV在线 | 国产精品无码成人网站视频 |