RSS 2.0 Feed
2004-04 Entries
摘要:blogdriver不能添加随笔,而且像很多软件一样,告诉我“不好了,程序错了“,但是从不告诉我为什么错了,哪里错了。 关于对象建模还有疑惑,比如Order和Customer,可以这样class order{    int CustomerID;    int OrderID    void AddOrder()} class Customer{     int CustomerID;} 这是很笨的形式,不算是面向对象的 改进一下class order{    Customer Customer}class Customer{     int CustomerID;   OrderCollection OrderList;     void AddOrder();    OrderCollection GetOrderList();} 但是这样的话,要实例化一个Order还要实例化一个Customer对象,而且根据OrderID得到Order对象要通过Customer对象来完成。...[阅读全文]

posted @ | Feedback (7) | Filed Under [ 拥抱变化 ]

摘要:有人送了一幅快板,快板很难学呀。   blogdriver升级了依旧很难用,交互界面设计的很差,除了能够上传文件之外,其余的没什么可说的。 别了,Dr.Dobb's Jourmal China!   这本杂志还没看过呢,就这么休刊了?   Entity对象要能够排序,可以实现ICompareable接口。比如: class Contact {    private static PropertyInfo sortOrder;  public static PropertyInfo SortOrder  {   get{return sortOrder;}   set{sortOrder=value;}  }   int IComparable.CompareTo(object objectToCompare)   {   IComparable p1= (IComparable)sortOrder.GetValue(this,null);   IComparable p2= (IComparable)sortOrder.GetValue(objectToCompare,null);   return p1.CompareTo(p2);  } }   客户端排序的方法如下:    Contact.SortOrder = typeof(Contact).GetProperty("Rank");   contacts.Sort();   可是每个Entity都实现ICompareable接口也很麻烦的事情,需要想别的办法。  ...[阅读全文]

posted @ | Feedback (1) | Filed Under [ Dot Net ]

摘要:网络上可以搜索到很多TDD的文章,但是很大一部分只是讲述怎样使用NUnit等工具的使用。只有切身的去体会TDD的每一个环节,才能真正理解TDD。 工具:NUnit的原理 NUnit的原理很简单,就是新建一个TestFixture实例,然后依次调用TestFixture中的Test Case,然后纪录Test Case的运行结果NUnit的核心对象是TestCase, TestSuit, TestResult TestCase指一个Test Case,比如一个[Test]属性标记的方法TestSuit指一组Test Case,比如一个[TestFixture]属性标记的对象TestResult指Test Case运行的结果,TestResult是一个抽象类,在NUnit中,有两个类是继承自TestResult的:TestCaseResult和TestSuiteResult NUnit是怎样运行Test Case的 NUnit定义了一个处理Test Case的抽象类TestCaseTestCase类最重要的方法就是Run() public override TestResult Run(EventListener listener)public abstract void Run(TestCaseResult result); TestCase运行的结果会存入一个TestCaseResult对象 调用Run方法如果传入实现了EventListener接口的对象话,就可以在TestCase实际运行之前以及TestCase运行之后进行自定义的处理......listener.TestStarted(this);Run(testResult);listener.TestFinished(testResult);...... NUnit还定义了一个实现抽象类TestCase的通用模版public abstract class TemplateTestCase : TestCase TemplateTestCase中Run方法的基本框架为public override void Run(TestCaseResult testResult ){ try {  InvokeSetUp(); //设置环境  InvokeTestCase(); //运行Test Case  InvokeTearDown(); //恢复环境  ProcessNoException(testResult); //无异常退出 } catch {  ProcessException(testResult); //异常处理 }} 在Run方法中还会计算Test Case实际运行的时间和所用的内存DateTime start = DateTime.Now;long before = System.GC.GetTotalMemory( true ); .... //run test case long after = System.GC.GetTotalMemory( true );testResult.Leakage = after - before;DateTime stop = DateTime.Now;TimeSpan span = stop.Subtract(start);testResult.Time = (double)span.Ticks / (double)TimeSpan.TicksPerSecond; 下面几个类都是继承自TestCase类或者TemplateTestCase类NormalTestCase //一般的Test CaseNotRunnableTestCase //不可运行的Test CaseExpectedExceptionTestCase //定了期望异常的Test Case 为什么写的Test Case没有自动运行写Test Case时候要注意,Test Case必须是public的,无参数的,无返回值的函数参考:public class NotRunnableTestCase : TestCase{ public NotRunnableTestCase(MethodInfo method) : base(method.DeclaringType.FullName, method.Name) {  string reason;   if (method.IsAbstract)   reason = "it must not be abstract";  else if (method.IsStatic)   reason = "it must be an instance method";  else......[阅读全文]

posted @ | Feedback (5) | Filed Under [ 拥抱变化 ]

摘要:根据table中的数据实例化一个class时,处理DBNull实在是一件麻烦的事情,object类型的数据还好处理,比如string,值类型的数据处理起来就麻烦了,比如DateTime, enum, int等,现在还没有找到统一的解决办法。 对于枚举的数据,数据库里面存储以及UI显示的可能要求是string类型的数据,不是整数比如SalutationType.Lady,在数据库里存储的值可能“女人”,显示的值是“一个女人”JGTM提供一种解决办法 enum SalutationType{ [DisplayName("一个男人")]  [DBValue("男人")]  Gentleman,  [DisplayName("一个女人")]  [DBValue("女人")]  Lady,} 实验下来,这样做带来了很多负面的影响,看来我的这个要求有点不切实际,很多值没有必要为了强数据类型的验证就定义成enum类型。...[阅读全文]

posted @ | Feedback (13) |

摘要:A:DataReader 直接绑定,B: DataReader直接转换成EntityCollection,C:用Reflection将DataReader转换成EntityCollection,三种方式到底速度差多少? 用2000笔数据作试验。测试下来,直接转换成EntityCollection所化的时间与直接绑定相差无几,大概是1~1.5倍,用Reflection将DataReader转换成EntityCollection所化时间是直接绑定的4~5倍左右,用注释起来的代码的话花的时间就多了,大概8倍的样子。 但是转化成EntityCollection,还要写代码来处理排序。 直接绑定大致如此   private void BindByDirect(SqlDataReader dr)  {   contactData.DataSource= dr;   contactData.DataBind();  } 用EntityCollection   private void BindByEntityCollection(SqlDataReader dr)  {   ContactCollection contacts = new ContactCollection();   while(dr.Read())   {    Contact contact = new Contact();    contact.Contact_ID = dr["Contact_ID"].ToString();    contact.Contact_LastName = dr["Contact_LastName"].ToString() ;    contact.Contact_JobTitle = dr["Contact_JobTitle"].ToString();    contact.Contact_Department = dr["Contact_Department"].ToString();    contact.Contact_Rank = dr["Contact_Rank"].ToString();    contacts.Add(contact);   }   contactData.DataSource= contacts;   contactData.DataBind();  } 借助反射 private void BindByReflection(SqlDataReader dr)  {   ContactCollection contacts = new ContactCollection();   Type objType = typeof(Contact);   PropertyInfo[] properties = objType.GetProperties(BindingFlags.Public | BindingFlags.Instance);   Type fieldAttributeType  = typeof(FieldAttribute);    while(dr.Read())   {    //Contact contact = new Contact();    //foreach (PropertyInfo property in properties)    //{    // FieldAttribute fieldAttribute = (FieldAttribute) Attribute.GetCustomAttribute(    //  property, fieldAttributeType);     // property.SetValue(contact,dr[fieldAttribute.ColumnName].ToString(),null);    //}    object instance = Activator.CreateInstance(objType, true);    for (int i=0; i < properties.Length; i++)    {     FieldAttribute[] fieldAttributes = (FieldAttribute[])properties[i].GetCustomAttributes(fieldAttributeType, true);     if (fieldAttributes.Length > 0)     {      properties[i].SetValue(instance, dr[fieldAttributes[0].ColumnName].ToString(), null);     }    }    contacts.Add(instance );   }   contactData.DataSource= contacts;   contactData.DataBind();  } } public class  Contact{  string contact_ID;  [FieldAttribute("Contact_ID")]  public string Contact_ID  {   get {return contact_ID;}   set {contact_ID = value;}  }}...[阅读全文]

posted @ | Feedback (10) | Filed Under [ Dot Net ]

摘要:博客园组织了一个开源项目 Aop.NET , 看起来很不错,干实事的人呀。 他们在http://www.gotdotnet.com还有一个AOP的项目: DotNetAOP 大名鼎鼎的spring也有人开始开发.Net版本的了:http://www.springframework.org/net/。 不可否认,.Net的社区还是不如Java的社区活跃呀。  ...[阅读全文]

posted @ | Feedback (13) |

摘要:近来网络一直不好,只能流畅的访问中网新空气等几个中文站点,这几天访问国外网站的速度恢复正常了,访问一般的中文网站还是很慢。 本想在博客动力申请一个账号作为补充的,但申请之后就没能正常的访问过博客动力。 以前的流言社也很久没更新了。 接下来的时间先总结一下基本的代码标准,然后载总结一下基本的重构方法。 期间蒙JGTM多次指点,深表感谢。  ...[阅读全文]

posted @ | Feedback (5) | Filed Under [ Dot Net ]