如何使用 Castle ActiveRecord 实现 CreatedAt 属性

发布于 07-29 05:57 字数 406 浏览 4 评论 0原文

假设我想创建一个名为 EntityWithCreatedAt 的类:

class Entity<T> : ActiveRecordBase<T>
{
  [PrimaryKey]
  public int Id { get; set; }
}

class EntityWithCreatedAt<T> : Entity<T>
{
  [Property]
  public DateTime CreatedAt { get; set; }
}

填充 CreatedAt 字段的最佳方法是什么? 我只是在 EntityWithCreatedAt 的构造函数中执行此操作,还是还有其他方法?

那么 UpdatedAt 属性又如何呢?

谢谢,

Say I want to create a class called EntityWithCreatedAt:

class Entity<T> : ActiveRecordBase<T>
{
  [PrimaryKey]
  public int Id { get; set; }
}

class EntityWithCreatedAt<T> : Entity<T>
{
  [Property]
  public DateTime CreatedAt { get; set; }
}

What is the best way to populate the CreatedAt field? Do I just do it in EntityWithCreatedAt's constructors, or is there some other way?

What about an UpdatedAt property?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担2024-08-05 05:57:27

在 Rails 的 AR 实现中,CreatedAt 和 UpdatedAt 是在迁移中自动创建的时间戳(尽管您也可以手动创建它们)。 假设您想要相同的行为,您需要重写 Create 和 Update。

   public override void Update() 
   {
         UpdatedAt = DateTime.Now;                      
         base.Update(); 
   }

   public override void Create()
   {
         CreatedAt = DateTime.Now;
         base.Create();
   }

如果您没有使用分配的主键,并且数据库正在为您生成它(例如,使用自动增量),那么您可能会使用 Save 方法来决定是调用 Create 还是 Update。 使用 Save 仍然可以完美地为您工作,因为调用基类的 Save() 方法将触发对 Create(如果尚未设置 ID)或 Update(如果 ID 已设置并且记录之前已保存)。

此方法的唯一缺点是您的 CreatedAt 和 UpdatedAt 属性应始终反映数据库中保存的内容,但在您的情况下,您是在知道数据库提交成功之前设置属性。 这是不可避免的,但是通过更新覆盖中的一些 try/catch 代码,您应该能够记录以前的值并在出现任何问题时将其分配回来:

   public override void Update() 
   {
         DateTime originalUpdatedAt = UpdatedAt;
         try 
         {
            UpdatedAt = DateTime.UtcNow;  // Assuming you're using UTC timestamps (which I'd recommend)                     
            base.Update(); 
         }
         catch  // Don't worry, you're rethrowing so nothing will get swallowed here.
         {
            UpdatedAt = originalUpdatedAt;
            throw;
         }
   }

In Rails' implementation of AR, CreatedAt and UpdatedAt are automatic timestamps created in migrations (although you can created them manually, too). Assuming you want the same behaviour, you'll need to override Create and Update.

   public override void Update() 
   {
         UpdatedAt = DateTime.Now;                      
         base.Update(); 
   }

   public override void Create()
   {
         CreatedAt = DateTime.Now;
         base.Create();
   }

If you're not using an assigned primary key, and the DB is generating it for you (with auto increment, for example), then you may be using the Save method to decide whether to call Create or Update. Using Save will still work perfectly for you, as a call to the base class' Save() method will trigger a call to your Create (if the ID hasn't been set) or your Update (if the ID has been set and the record has previously been saved).

The only downside to this method is that your CreatedAt and UpdatedAt properties should always reflect what's saved in the DB, but in your case you're setting the properties before you know the commit to the DB succeeded. This is unavoidable, but with some try/catch code in your Update override, you should be able to record the previous value and assign it back should anything go wrong:

   public override void Update() 
   {
         DateTime originalUpdatedAt = UpdatedAt;
         try 
         {
            UpdatedAt = DateTime.UtcNow;  // Assuming you're using UTC timestamps (which I'd recommend)                     
            base.Update(); 
         }
         catch  // Don't worry, you're rethrowing so nothing will get swallowed here.
         {
            UpdatedAt = originalUpdatedAt;
            throw;
         }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文