【问题标题】:nhibernate session with custom data and event listeners具有自定义数据和事件侦听器的休眠会话
【发布时间】:2013-01-08 05:15:28
【问题描述】:
对于我正在处理的 WCF 项目,我需要为我们保留的实体实施某种审核日志。基本上,审计跟踪将包含 4 个必填字段
- 创建日期时间
- CreatedUserID
- 更新日期时间
- 更新用户 ID
我正在尝试通过我的 DataAccess 层中的 nHibernate 事件侦听器来实现这一点,因为我认为这不应该在域层中。到目前为止,我已经让 DateTime 的东西按预期工作,但还没有弄清楚如何在事件侦听器中检索用户 ID。理想情况下,我希望将用户 ID 作为某种自定义数据附加到 nHibernate 会话对象。
非常感谢任何建议。
【问题讨论】:
标签:
wcf
nhibernate
auditing
audit-trail
【解决方案2】:
这是我的做法,但我没有使用 WCF 执行此操作的经验。请注意,这需要引用 System.Web。
/// <summary>
/// Returns the user name of the current user. Gets user name from HttpContext if running as a web app, else WindowsIdentity.
/// </summary>
private static string GetUserName()
{
var identity = HttpContext.Current == null ? WindowsIdentity.GetCurrent() : HttpContext.Current.User.Identity;
if (identity == null)
{
throw new Exception("Identity could not be determined.");
}
// Remove domain name if present
var s = identity.Name;
var stop = s.IndexOf("\\", StringComparison.InvariantCultureIgnoreCase);
return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1).ToUpper() : s;
}