【问题标题】:Overriding LLBLGen Save() with custom columns用自定义列覆盖 LLBLGen Save()
【发布时间】:2012-11-30 16:04:09
【问题描述】:

在我们的计划中,LLBLGEN 中有大约 50 多个实体。它们都有 2 列,分别称为 ModifyDatetimeModifyUserID。它们也是 SQL 数据库中每个表的最后 2 列。

我们想修改 .Save() 的 LLBLGEN 函数,以便在程序员执行 .Save()

我得到它来保存会话用户 ID 和日期时间,但我在某些情况下遇到错误,我认为这与在渲染或编译时更改的列的位置有关。

我从 ___Entity.cs 之一(在本例中为 CityEntity.cs)复制了 2 个 public virtual 函数,它甚至在保存另一种实体类型 (ServicesEntity.cs) 时也有效。所以,有一段时间,我认为 CityFieldIndex.* 只是一个随机整数。两个类都有不同数量的列。或者也许我不完全理解这个 public virtual 以及如何覆盖它。

我也尝试只发送 Rows.Count - 1 和 Rows.Count - 2 而不是 CityFieldIndex.*

在 set ModifyUserId 上也出现一个奇怪的错误:值 33 的类型为“System.Int32”,而该字段的类型为“System.Nullable`1[System.DateTime]” (33 是用户 ID)

CommonEntityBase.cs上的自定义代码是:

public override bool Save(IPredicate updateRestriction, bool recurse)
{
      System.Diagnostics.Debug.WriteLine("i'm overriding SAVE yeii");
      ModifyDatetime = DateTime.Now;
      ModifyUserId = 33;

      return base.Save(updateRestriction, recurse);
}

public virtual Nullable<System.DateTime> ModifyDatetime
{
    get { return (Nullable<System.DateTime>)GetValue((int)CityFieldIndex.ModifyDatetime, false); }
    set { SetValue((int)CityFieldIndex.ModifyDatetime, value, true); }
}    

public virtual Nullable<System.Int32> ModifyUserId
{
    get { return (Nullable<System.Int32>)GetValue((int)CityFieldIndex.ModifyUserId, false); }
    set { SetValue((int)CityFieldIndex.ModifyUserId, value, true); }
}

【问题讨论】:

  • 没有时间制定完整的答案,所以以 cmets 的身份发布。您可能想查看 LLBLGen 本身内置的 Auditing 功能。
  • 您也可能需要重新考虑直接从 Save 中引用 HttpContext。这将它与 Web 应用程序紧密联系在一起,如果您有一个需要保存数据的控制台实用程序(例如),将会给您带来问题。让支持这些属性的所有实体实现相同的接口可能会更好。然后,您可以在实体上使用 getter/setter,而不必处理字段索引关闭的事实。
  • 是的,我遇到了控制台工具的问题。我现在首先检查 HttpContext 是否为空。见答案。

标签: c# reflection polymorphism llblgenpro


【解决方案1】:

我解决了。
现在,每当其他人使用 Save() 时,它都会从会话中保存用户。 如果没有设置用户,它只是放置 UserId = 0。

当然,我们关闭了我们的应用程序来验证登录会话,只有系统服务使用 0 保存:)

 public override bool Save(IPredicate updateRestriction, bool recurse)
    {
        ModifyDatetime = DateTime.Now;
        int userID = 0;

        // Verifies the UserId that called the Save(). Uses 0 (as system user) when none is found.
        try
        {
            if (HttpContext.Current.Session["UserID"] != null)
            { userID = Convert.ToInt32(HttpContext.Current.Session["UserID"]); }

        }
        catch (NullReferenceException nullE)
        {
            userID = 0;
        }
        ModifyUserObjectId = userID;

        return base.Save(updateRestriction, recurse);
    }

    /// <summary> The ModifyDatetime property of all Entities<br/><br/></summary>
    /// <remarks>All tables must have the field: *table*."modifyDatetime"<br/>
    public virtual Nullable<System.DateTime> ModifyDatetime
    {
    //    get { return (Nullable<System.DateTime>)GetValue((int)base.Fields["ModifyDatetime"].FieldIndex, false); }  // uncomment for GET
        set { SetValue((int)base.Fields["ModifyDatetime"].FieldIndex, value, true); }
    }

    /// <summary> The ModifyUserObjectId property of all Entities<br/><br/></summary>
    /// <remarks>All tables must have the field: *table*."modifyUser_objectID"<br/>
    public virtual Nullable<System.Int32> ModifyUserObjectId
    {
    //    get { return (Nullable<System.Int32>)GetValue((int)base.Fields["ModifyUserObjectId"].FieldIndex, false); }  // uncomment for GET
        set { SetValue((int)base.Fields["ModifyUserObjectId"].FieldIndex, value, true); }
    }

仍然不知道它是否适用于集合。但它应该是相同的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2021-07-22
    相关资源
    最近更新 更多