代码之家  ›  专栏  ›  技术社区  ›  Thomas

如何在castle activerecord保存或更新期间自动填充字段

  •  4
  • Thomas  · 技术社区  · 15 年前

    问题: 数据库中的所有表都有createdDate、createdBy、changedDate和changedBy字段,我希望在保存/更新activerecord实体时自动设置这些字段。

    我的第一个尝试是重写save()和update()方法。但这些方法只有在我对实体执行直接save()或update()时才会被调用。它们不是在主详细场景中调用的,我只在主场景中调用save()。

    下一个尝试是onSave()和onUpdate()方法,但此处字段中的更改没有持久化到数据库中。

    最后我尝试了beforeSave()方法。但更新时不调用此方法。

    问题是: 如何在save()或update()期间自动设置这些createdDate、createdBy、changedDate、changedBy字段?

    4 回复  |  直到 14 年前
        1
  •  2
  •   Nelson Miranda    15 年前

    要按需修改数据,必须重写beforesave方法,如下所示:

        protected override bool BeforeSave(IDictionary state)
        {
            bool retval = base.BeforeSave(state);
            state["Password"] = Global.Encrypt("password");
            return retval;
        }
    

    最后保存实例:

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            qfh.User user = null;
    
            user = new qfh.User();
    
            user.UserName = txtUserName.Text;
            user.Name = txtName.Text;
            user.IsAdministrator = cboIsAdministrador.SelectedValue == "Yes";
            user.IsActive = cboIsActive.SelectedValue == "Yes";
    
            user.SaveCopy();
        }
        catch (Exception ex)
        {
            ex = Utilities.GetInnerException(ex);
            JSLiteral.Text = Utilities.GetFormattedExceptionMessage(ex);
        }
    }
    

    我通常使用savecopy()来使用重写方法finddirty(object id,idictionary previousstate,idictionary currentstate,nhibernate.type.itype[]类型)来获取类的前一个值。

    希望有帮助。

        2
  •  1
  •   Episodex    14 年前

    使用beforeSave()保存,使用onflushDirty()更新。

        3
  •  0
  •   Nesizer    15 年前

    你可以这样做

    [ActiveRecord("PostTable")]
    public class Post : ActiveRecordBase<Post>
    {
         private int _id;
         private DateTime _created;
    
         [PrimaryKey]
         public int Id
         {
            get { return _id; }
            set { _id = value; }
         }
    
         [Property("created")]
         public DateTime Created
         {
            get { return _created; }
            set { _created = value; }
         }
    
         private void BeforeUpdate()
         {
            // code that run before update
            Created = DateTime.Now;
         }
    
        public override void Update()
        {
            BeforeUpdate();
            base.Update();            
        }
    
        4
  •  0
  •   Afshar Mohebi    15 年前

    我也有同样的问题,就这样解决了:

    我用 OnUpdate() OnSave() . 如您所述,此解决方案不适用于主详细信息方案。为此,我显式地设置每个子对象的父对象。注意以下代码:

    [ActiveRecord(Lazy = true)]
    public class Lookup : ActiveRecordBase<Lookup>
    {
        [HasMany(typeof(LookupItem), Cascade = ManyRelationCascadeEnum.All)]
        public virtual IList Items { set; get; }
    
        //other properties...
    }
    
    
    [ActiveRecord(Lazy = true)]
    public class LookupItem : ActiveRecordBase<LookupItem>
    {
        [BelongsTo("Lookup_id")]
        public virtual Lookup ContainerLookup { set; get; }
    
        //other properties...
    }
    
    void SaveLookup()
    {
        Lookup lookup = GetLookup();
        LookupItem lookupItem = new LookupItem()
        {
            Title = LookupItemName,
            ContainerLookup = lookup
        };
        lookup.Items.Add(lookupItem);
        lookup.Save();
    }
    
    推荐文章