小小程序员的心得

  2009年7月1日,我正式成为一名光荣的程序员。工作了差不多三个多月,开始有丁点心得体会。于是把这些东西都写下来,也希望能帮助后来人,也能提醒自己不断前进。
  First and foremost,做程序员要懒(估计有人会说:kao,刚说了要前进,现在就说要懒),这也是一个前辈教我们的(希望老板看了不会误会,呵呵)。这里的“懒”也是有情景的,工作不能偷懒,但是做重复劳动的时候能懒则要懒。这里说懒,并不是要代码偷工减料,当然地,字段不能加少一个是否为null的判断,字符串不能少加一个长度的判断。这个时候,在我心中冒出了一个关键字--“for”。"for“意思就是交给计算机批量处理,也就是让计算机来处理重复劳动,而不是程序员本身。我觉得程序员应该把重点放在架构设计和业务逻辑,而不是因为某几个数据库字段不能插入空值,导致出错,而经常被叫去修bug。我们需要更加精确工具来完成这些事情,而不是看着数据库表几十个字段发呆。
  呃,说了这么久,其实就是为了引出自己写的一套解决方案(说成解决方案不知道会不会说得太大了。。。),这套解决方案主要是基于CodeSmith的,以后可能会用Winform自己写一部分。功能有选择数据库,生成数据访问层(基于Ado.Net的),生成业务实体(业务实体支持Nullable类型);选择数据表里面的列,生成一些代码片段,如函数参数以及自动为不能为空的列加上if判断。
  下面为生成的代码:
 实体类:
ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Text;

ExpandedBlockStart.gifContractedBlock.gif
namespace Entities {
    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary><c>Employees</c> Business Object.</summary>
    [Serializable]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public partial class Employees {
        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
EmployeeID#region EmployeeID

        
private Int32 m_employeeID;
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>Gets or sets EmployeeID</summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public Int32 EmployeeID {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return m_employeeID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { m_employeeID = value;}        
        }

        
        
#endregion

        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
LastName#region LastName

        
private String m_lastName;
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>Gets or sets LastName</summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public String LastName {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return m_lastName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { m_lastName = value;}        
        }

        
        
#endregion

        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
FirstName#region FirstName

        
private String m_firstName;
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>Gets or sets FirstName</summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public String FirstName {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return m_firstName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { m_firstName = value;}        
        }

        
        
#endregion

        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Title#region Title

        
private String m_title;
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>Gets or sets Title</summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public String Title {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return m_title; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { m_title = value;}        
        }

        
        
#endregion

        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
BirthDate#region BirthDate

        
private DateTime? m_birthDate;
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>Gets or sets BirthDate</summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public DateTime? BirthDate {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return m_birthDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { m_birthDate = value;}        
        }

        
        
#endregion

        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
HireDate#region HireDate

        
private DateTime? m_hireDate;
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>Gets or sets HireDate</summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public DateTime? HireDate {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return m_hireDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { m_hireDate = value;}        
        }

        
        
#endregion

    }

    
}
数据库访问层:
ContractedBlock.gif ExpandedBlockStart.gif Code
ExpandedBlockStart.gifContractedBlock.gifpublic static Employees Find(Int32 employeeID) {
            Employees obj 
= null;

            SqlConnection db 
= null;
            SqlCommand cmd 
= null;
            SqlDataReader dr 
= null;
        
            
try
ExpandedSubBlockStart.gifContractedSubBlock.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())
ExpandedSubBlockStart.gifContractedSubBlock.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
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (dr != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    dr.Close();
                    dr.Dispose();
                }

                cmd.Dispose();
                
if (db.State == ConnectionState.Open) db.Close();
                db.Dispose();
            }

            
            
return obj;
        }
代码片段:
ContractedBlock.gif ExpandedBlockStart.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丙则写了一个小工具,每天记录下硬盘的容量,然后自动推测下次需要扩容的时间。这种情况下,如果要评定薪水,您会给谁最高的薪水呢?
      综上所述,这三个月程序员生涯得出的结论是,要做一个懒的,会提高自己附加价值的程序员。

转载于:https://www.cnblogs.com/StephenHuang/archive/2009/09/24/1573634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值