• T4:使用 T4 消除程序和配置文件中重复的字符串信息


    背景

    我们经常在配置文件中配置各种:id、name,然后在程序中使用这些配置获取信息,这导致了字符串重复出现在系统的多个地方,非常不利于维护,本文介绍采用 T4 来消除这种重复。

    T4 消除重复

    配置文件

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 
     3 <sqlMap namespace="Accounts" xmlns="http://ibatis.apache.org/mapping"
     4   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     5 
     6   <alias>
     7     <typeAlias alias="Account" type="WHTR.Domain.Accounts.Account, WHTR.Domain" />
     8   </alias>
     9 
    10   <statements>
    11     <select id="Account.GetByCustomIdAndAccountType"  parameterClass="Hashtable"  resultMap="Account">
    12       select
    13       *
    14       from
    15       [Account]
    16       where
    17       (CustomerId = #customerId#) and (AccountType = #accountType#)
    18     </select>
    19 
    20     <insert id="Account.Insert" parameterMap="Account">
    21       <generate table="Account"/>
    22       <selectKey resultClass="int" type="post" property="Id">
    23         select @@IDENTITY as value
    24       </selectKey>
    25     </insert>
    26 
    27     <update id="Account.Update" parameterClass="Account">
    28       update
    29       [Account]
    30       set
    31       CustomerId           =        #CustomerId#,
    32       Amount               =        #Amount#,
    33       AccountType          =        #AccountType#,
    34       FrozenAmount         =        #FrozenAmount#,
    35       InitialAmount        =        #InitialAmount#,
    36       Creator              =        #Creator#,
    37       CreateDate           =        #CreateDate#,
    38       Editor               =        #Editor#,
    39       EditDate             =        #EditDate#,
    40       IsDel                =        #IsDel#,
    41       Version              =        Version + 1
    42       where
    43       (Version = #Version#) and (Id = #Id#)
    44     </update>
    45   </statements>
    46   
    47 </sqlMap>

    原始代码

    1         public void Insert(Account account)
    2         {
    3             Mapper
    4                 .Instance()
    5                 .Insert("Account.Insert", account);
    6         }

    重构后的代码

    1         public void Insert(Account account)
    2         {
    3             Mapper
    4                 .Instance()
    5                 .Insert(Constants.Account.Insert, account);
    6         }
    7     }

    模版

    如何写 T4 这里就不介绍了。

  • 相关阅读:
    [线段树][数学]JZOJ 4237 Melancholy
    [规律]JZOJ 4222 恐怖的奴隶主
    [Tarjan][基环树]JZOJ 4221 互相追逐的点
    [斯特林数][自然数幂和]JZOJ 4220 WYF的盒子
    奇妙的骚操作
    [树形DP][概率期望]JZOJ 4225 宝藏
    操作系统基础知识
    计算机硬件知识整理
    ORM的查询
    ORM的记录添加和删除
  • 原文地址:https://www.cnblogs.com/happyframework/p/3679680.html
Copyright © 2020-2023  润新知