zoukankan      html  css  js  c++  java
  • 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 这里就不介绍了。

  • 相关阅读:
    [转]利用docker进行java开发小demo
    markdown简介及语法
    Thinking in Java 之classpath理解
    docker官方windows安装
    Thinking in Java笔记之类及对象的初始化
    开发工具之play framework
    图解phpstorm常用快捷键
    Git相关
    Yii 1.1 cookie删不掉
    ajax跨域,这应该是最全的解决方案了
  • 原文地址:https://www.cnblogs.com/happyframework/p/3679680.html
Copyright © 2011-2022 走看看