我需要在所有其他实体继承自的基础实体上实现AddedBy/ChangedBy 类型字段(Fluent Nhibernate)。
访问HttpContext.User.Identity
从我的存储库/数据层中可能不是一个好主意..或者是吗?
获取我的用户(当前身份)信息以记录记录添加或更改者的最佳方式是什么?
重构整个应用程序以在存储库调用中包含用户信息是愚蠢的。我确信有更好、更通用的方法。
从数据层访问 HttpContext 会让事情变得更加困难,特别是如果您使用单元测试。解决方案是创建一项服务来提供应用程序范围内的用户信息,例如:
public interface ICurrentUserService {
string UserName {get;}
string UserId {get;}
string HostIP {get;}
// etc.
}
然后你可以实现具体的服务并使用你的注入它
首选 IoC 容器。
public class CurrentWebUserService : ICurrentUserService {
// implement interface members
public CurrentWebUserService(HttpContext context) { ... }
public string UserName { get { ... } }
// etc.
}
// maybe you want a stub service to inject while unit testing.
public class CurrentUserServiceStub : ICurrentUserService {
}
// data layer
public class MyDaoService {
public DaoService(ICurrentUserService currentUser) { ... }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)