2009年7月1日,我正式成为一名光荣的程序员。工作了差不多三个多月,开始有丁点心得体会。于是把这些东西都写下来,也希望能帮助后来人,也能提醒自己不断前进。
First and foremost,做程序员要懒(估计有人会说:kao,刚说了要前进,现在就说要懒),这也是一个前辈教我们的(希望老板看了不会误会,呵呵)。这里的“懒”也是有情景的,工作不能偷懒,但是做重复劳动的时候能懒则要懒。这里说懒,并不是要代码偷工减料,当然地,字段不能加少一个是否为null的判断,字符串不能少加一个长度的判断。这个时候,在我心中冒出了一个关键字--“for”。"for“意思就是交给计算机批量处理,也就是让计算机来处理重复劳动,而不是程序员本身。我觉得程序员应该把重点放在架构设计和业务逻辑,而不是因为某几个数据库字段不能插入空值,导致出错,而经常被叫去修bug。我们需要更加精确工具来完成这些事情,而不是看着数据库表几十个字段发呆。
呃,说了这么久,其实就是为了引出自己写的一套解决方案(说成解决方案不知道会不会说得太大了。。。),这套解决方案主要是基于CodeSmith的,以后可能会用Winform自己写一部分。功能有选择数据库,生成数据访问层(基于Ado.Net的),生成业务实体(业务实体支持Nullable类型);选择数据表里面的列,生成一些代码片段,如函数参数以及自动为不能为空的列加上if判断。
下面为生成的代码:
实体类:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace Entities
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary><c>Employees</c> Business Object.</summary>
[Serializable]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public partial class Employees
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
EmployeeID#region EmployeeID
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Int32 m_employeeID;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Gets or sets EmployeeID</summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Int32 EmployeeID
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_employeeID; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_employeeID = value;}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
LastName#region LastName
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String m_lastName;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Gets or sets LastName</summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String LastName
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_lastName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_lastName = value;}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
FirstName#region FirstName
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String m_firstName;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Gets or sets FirstName</summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String FirstName
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_firstName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_firstName = value;}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Title#region Title
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String m_title;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Gets or sets Title</summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String Title
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_title; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_title = value;}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
BirthDate#region BirthDate
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DateTime? m_birthDate;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Gets or sets BirthDate</summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime? BirthDate
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_birthDate; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_birthDate = value;}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
HireDate#region HireDate
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private DateTime? m_hireDate;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>Gets or sets HireDate</summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime? HireDate
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_hireDate; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ m_hireDate = value;}
}
#endregion
}
}数据库访问层:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static Employees Find(Int32 employeeID)
{
Employees obj = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlConnection db = null;
SqlCommand cmd = null;
SqlDataReader dr = null;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sql = @"SELECT
[EmployeeID]
, [LastName]
, [FirstName]
, [Title]
, [TitleOfCourtesy]
, [BirthDate]
, [HireDate]
, [Address]
, [City]
, [Region]
, [PostalCode]
, [Country]
, [HomePhone]
, [Extension]
, [Photo]
, [Notes]
, [ReportsTo]
, [PhotoPath]
FROM [dbo].[Employees](NOLOCK)
WHERE
([EmployeeID] = @EmployeeID)
";
db = getConnection();
cmd = new SqlCommand(sql, db);
cmd.Parameters.AddWithValue("@EmployeeID",employeeID);
db.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
obj = new Employees();
obj.EmployeeID = (dr["EmployeeID"] == System.DBNull.Value) ? 0 : Convert.ToInt32(dr["EmployeeID"].ToString().Trim());
obj.LastName = (dr["LastName"] == System.DBNull.Value) ? null : dr["LastName"].ToString().Trim();
obj.FirstName = (dr["FirstName"] == System.DBNull.Value) ? null : dr["FirstName"].ToString().Trim();
obj.Title = (dr["Title"] == System.DBNull.Value) ? null : dr["Title"].ToString().Trim();
obj.TitleOfCourtesy = (dr["TitleOfCourtesy"] == System.DBNull.Value) ? null : dr["TitleOfCourtesy"].ToString().Trim();
obj.BirthDate = (dr["BirthDate"] == System.DBNull.Value) ? (DateTime?)null : Convert.ToDateTime(dr["BirthDate"].ToString().Trim());
obj.HireDate = (dr["HireDate"] == System.DBNull.Value) ? (DateTime?)null : Convert.ToDateTime(dr["HireDate"].ToString().Trim());
obj.Address = (dr["Address"] == System.DBNull.Value) ? null : dr["Address"].ToString().Trim();
obj.City = (dr["City"] == System.DBNull.Value) ? null : dr["City"].ToString().Trim();
obj.Region = (dr["Region"] == System.DBNull.Value) ? null : dr["Region"].ToString().Trim();
obj.PostalCode = (dr["PostalCode"] == System.DBNull.Value) ? null : dr["PostalCode"].ToString().Trim();
obj.Country = (dr["Country"] == System.DBNull.Value) ? null : dr["Country"].ToString().Trim();
obj.HomePhone = (dr["HomePhone"] == System.DBNull.Value) ? null : dr["HomePhone"].ToString().Trim();
obj.Extension = (dr["Extension"] == System.DBNull.Value) ? null : dr["Extension"].ToString().Trim();
obj.Photo = (dr["Photo"] == System.DBNull.Value) ? null : dr["Photo"].ToString().Trim();
obj.Notes = (dr["Notes"] == System.DBNull.Value) ? null : dr["Notes"].ToString().Trim();
obj.ReportsTo = (dr["ReportsTo"] == System.DBNull.Value) ? (int?)null : Convert.ToInt32(dr["ReportsTo"].ToString().Trim());
obj.PhotoPath = (dr["PhotoPath"] == System.DBNull.Value) ? null : dr["PhotoPath"].ToString().Trim();
}
}
//catch(Exception ex)
//{
//}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (dr != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dr.Close();
dr.Dispose();
}
cmd.Dispose();
if (db.State == ConnectionState.Open) db.Close();
db.Dispose();
}
return obj;
}代码片段:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
//所有被选中的字段
Int32 EmployeeID, String LastName, String FirstName, String Title, String TitleOfCourtesy, DateTime BirthDate, DateTime HireDate, String Address, String City, String Region, String PostalCode, String Country, String HomePhone, String Extension, Byte[] Photo, String Notes, Int32 ReportsTo, String PhotoPath,
//其中不能为空的字段
Int32 EmployeeID, String LastName, String FirstName, String Title, String TitleOfCourtesy, DateTime BirthDate, DateTime HireDate, String Address, String City, String Region, String PostalCode, String Country, String HomePhone, String Extension, Byte[] Photo, String Notes, Int32 ReportsTo, String PhotoPath,
if (obj.EmployeeID==null){
//DoSomethingHere;
}
if (obj.LastName==null){
//DoSomethingHere;
}
if (obj.FirstName==null){
//DoSomethingHere;
}该模板可以在这里下载:
http://code.google.com/p/idalgen/downloads/list
第二点,要提高自己的附加价值。某天做DBA的杨总同学发了一篇文章给我,给了我在职场中很大的提示。文章大概说的是,很多DBA总是埋怨自己公司给自己的薪水低,说人家阿里巴巴那些大公司的DBA工资有多高。文章中举了一个例子,3个DBA针对硬盘进行扩容,DBA甲是等到硬盘爆炸了,才过去抢救的;DBA乙则很勤奋,每时每刻都盯着硬盘,怕他一下子饱满;DBA丙则写了一个小工具,每天记录下硬盘的容量,然后自动推测下次需要扩容的时间。这种情况下,如果要评定薪水,您会给谁最高的薪水呢?
综上所述,这三个月程序员生涯得出的结论是,要做一个懒的,会提高自己附加价值的程序员。