Ins*_*dBy 7 c# asp.net-mvc httpcontext asp.net-identity
我需要在我的基本实体上实现AddedBy/ChangedBy类型字段,所有其他实体都继承自(Fluent Nhibernate).
HttpContext.User.Identity从我的存储库/数据层访问可能不是一个好主意......或者是它?获取用户(当前身份)信息以记录添加或更改记录的人的最佳方法是什么?重新分解整个应用程序以在存储库调用中包含用户信息将是愚蠢的.我确信有更好,更通用的方式.
Mar*_*Zen 10
访问DataLayer中的HttpContext会使生活更加艰难,特别是如果您使用单元测试.解决方案是提供服务以提供应用程序范围的用户信息,例如:
public interface ICurrentUserService {
string UserName {get;}
string UserId {get;}
string HostIP {get;}
// etc.
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以实现具体服务并使用您首选的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
public class MyDaoService {
public DaoService(ICurrentUserService currentUser) { ... }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5752 次 |
| 最近记录: |