简体   繁体   English

在DataRow中将CreatedDate和ModifiedDate放入哪里的好地方?

[英]Where is a good place to tuck CreatedDate and ModifiedDate in a DataRow?

I have the following code 我有以下代码

public class SpecialItemRow : DataRow
    {
        public SpecialItemRow(DataRowBuilder builder)
            : base(builder)
        {
            CreatedDate = DateTime.Now.ToLongTimeString();
        }

        public string ItemName { get { return Convert.ToString(base["_ItemName"]); } set { base["_ItemName"] = value; _UpdateModifiedDate(); } }
        public string Price { get { return Convert.ToString(base["_Price"]); } set { base["_Price"] = value; _UpdateModifiedDate(); } }
        public string CreatedDate { get { return Convert.ToString(base["_CreatedDate"]); } set { base["_CreatedDate"] = value; _UpdateModifiedDate(); } }
        public string ModifiedDate { get { return Convert.ToString(base["_ModifiedDate"]); } set { base["_ModifiedDate"] = value; } }

        private void _UpdateModifiedDate()
        {
            ModifiedDate = DateTime.Now.ToLongTimeString();
        }
    }

which tries to set the modified and created dates automatically. 它将尝试自动设置修改和创建的日期。 but it turns out that those are not the right places to put. 但事实证明,这些不是放置的正确位置。 is there any other overridable methods to set these values? 还有其他可覆盖的方法来设置这些值吗? Thanks. 谢谢。

Do this in DataTable using OnRowChanged (or OnRowChanging). 使用OnRowChanged (或OnRowChanging)在DataTable执行此操作。 For example, 例如,

public class SpecialItemTable : DataTable
{

   protected override void OnRowChanged(DataRowChangeEventArgs e)
   {
      if (e.Action == DataRowAction.Add)
      {
         r.Row["CreatedDate"] = DateTime.Now;
      }
      else if (e.Action == DataRowAction.Change)
      {
         var time = DateTime.Now;
         // Check to prevent the cascaded row change events
         if ((time - (DateTime)e.Row["LastUpdated"]).TotalMilliseconds > 2)
         {
            e.Row["LastUpdated"] = time;
         }
      }
   }
}

(Disclaimer: Untested code - provided to just give an idea) (免责声明:未经测试的代码-仅用于提供想法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM