Ⅰ. Naming Conventions
1. Table Naming
Rule 1a ( Prefix) 新加的Table要加上適當的前缀
e.g. mUsr, eTxn, tmpRollex, tSmsInfo, zLogEvt
前綴 |
描述Desc |
m |
表示相對固定的,e.g. mUser ,mCustomer |
e |
表示會變動的,比如交易記錄eTxn |
temp |
表示臨時的,可以刪除 e.g. tempRollex |
t |
表示由程序臨時生成,提供給程式臨時調用。e.g. tSmsInfo temp與t的區別是,在程序正式交付使用后,temp不參與數據交互,可以刪除,而t不能刪除。 |
z |
表示日誌類.e.g. zLogEvt |
(注:Insight的database前缀有两个地方有区别,màVld, eà去掉) |
Rule 1b (Extra Field) 每個Table加上4个Field以記錄操作信息:
Field Name |
Date Type |
描述Desc |
wCreateDate |
Datetime |
創建時間 |
wCreateUser |
Bigint |
創建用戶 |
wStampDate |
Datetime |
修改時間 |
wStampUser |
Bigint |
修改用戶 |
Rule 1c (db table) 新建Table.sql,先在.eap文檔裏面加說明,變量類型全部大寫,并對齊, CONSTRAINT要加在字段裏面.
//good
CREATE TABLE [tSmsInfo] (
[wRowId] BIGINT IDENTITY(1,1) NOT NULL,
[wBatchNo] UNIQUEIDENTIFIER NOT NULL,
[wTemplateType] VARCHAR(30) NULL,
CONSTRAINT [PK_tSmsInfo] PRIMARY KEY CLUSTERED ([wRowId])
);
//bad
CREATE TABLE [tSmsInfo] (
[wRowId] bigint identity(1,1) NOT NULL, /* 自增 */
[wBatchNo] uniqueidentifier NOT NULL, /*批次号,GUID。 */
[wTemplateType] varchar(30) NULL, /* 模板*/
);
GO
ALTER TABLE [tSmsInfo]
ADD CONSTRAINT [UQ_tSmsInfo_wRowId] UNIQUE ([wRowId]);
GO
新建Table要點說明 |
e.g. |
◆將Table及備註加到eap文件 |
|
◆變量類型全部大寫 |
BIGINT IDENTITY(1,1) NOT NULL |
◆對齊,變量類型之間要對齊 |
[wBatchNo] UNIQUEIDENTIFIER NOT NULL, [wTemplateType] VARCHAR(30) NULL, |
◆CONSTRAINT等加到字段裏面 |
[wTemplateType] VARCHAR(30) NULL, CONSTRAINT [PK_tSmsInfo] ... |
◆Create Table sql字段不用加備註 |
/* 模板*/ |
2.Column字段
Rule 2a (Prefix) 字段以w作為前綴 ,e.g. wName, wUserId
3.View
Rule 3a (Prefix) view以vw作為前綴 ,e.g. vwUser, vwCustomer
4.Stored Proc
Rule 4a (Prefix) Stored Proc要加上適當的前綴
e.g. spaUser,spqUserLst,sprMember,spzLogEvt
前綴 |
描述Desc |
spa |
表示Insert Or Update. |
spq |
表示query查詢. |
spr |
表示 Report用到的sp |
spz |
與z為前綴的日誌類table的操作. |
Rule 4b (Parameter) 參數以@p作為前綴
e.g. @pUserId, @pRemark
Rule 4c (Variables) 變量以@d 作為前綴,以區別參數
e.g. @dUserName, @dDtCur
Rule 4d (Syntax) 語句對齊並且要有間隔等,關聯字大寫,Table整齊劃一,一目了然.
//good
SELECT c.wCustId, c.wCName AS wCustName
FROM [dbo].[vwCustomerDtl] AS c LEFT OUTER JOIN
[dbo].[mCustPrefer] AS m ON c.wCustId = m.wCustId
WHERE c.wCardType <> 'STAFF'
ORDER BY c.wCardType DESC;
//bad
select c.wCustId, c.wCName wCustName from vwCustomerDtl c LEFT OUTER JOIN
mCustPrefer m on c.wCustId = m.wCustId where c.wCardType <> 'STAFF' order by c.wCardType desc
Store Proc要點說明 |
e.g. |
◆Select與From要換行,並且間隔一個Tab,From、Where與Order By要對齊 |
SELECT *** FROM *** WHERE *** |
◆幾個 Table Join要換行,並且Table之間要對齊. |
FROM [dbo].[vwCustomerDtl] AS c LEFT OUTER JOIN [dbo].[mCustPrefer] AS m ON |
◆關鍵字要大寫 |
SELECT, FROM, ORDER BY |
◆重命名中間在用AS |
c.wCName AS wCustName,[vwCustomerDtl] AS c |
◆Table名要加上dbo |
[dbo].[vwCustomerDtl] |
◆一個語句結束后要加分號“;” |
; |
c#
1. Main
Rule 1a 定義變量,儘量避免用var,用具體的變數類型, 便于閱讀代碼,。
e.g.
//good
string _msg = string.Empty;
//bad
var _msg = string.Empty;
Rule 1b 數據庫查詢,儘量少用Linq, 與多個Table關聯的查詢,要用Stored Proc,方便Debug以及修改. e.g.
//good
List<spqGetSiteLstByTaskIdResult> _lst = db.spqGetSiteLstByTaskId(pTaskId).ToList()
//bad
var query = from pt in this.ObjectContext.SpaGetPartiesTypes(wCategory)/
join so in db.SiteOrgs.Where(x => x.wSiteRowId == wSiteRowId) on pt.wRowId equals so.wOrgTypeId into ps
from so in ps.DefaultIfEmpty(new SiteOrg() { wOrgTypeId = -1, wRECNO = -1, wSiteRowId = -1 })
join uo in db.VldUserOrgs on so.wRECNO equals uo.RECNO into psu ***