我需要在我的所有其他实体(Fluent Nhibernate)继承的基本实体上实现AdditionalBy/ChangedBy类型字段.
I need to implement AddedBy/ChangedBy type fields on my Base Entity that all other entities inherit from ( Fluent Nhibernate ).
从我的存储库/数据层访问HttpContext.User.Identity可能不是一个好主意. 抓住我的用户(当前身份)信息以记录添加或更改记录的人的最佳方法是什么? 将整个应用程序重构为将用户信息包括在存储库调用中将是很愚蠢的.我敢肯定有一种更好,更通用的方法.
Accessing HttpContext.User.Identity from within my Repository/Data layer is probably not a good idea.. or is it ? What's the best way to grab my user ( current identity ) information to record who the records were added or changed by ? Re-factoring the entire application to include user information in repository calls would be silly. I'm sure there is a better, more generic way.
推荐答案从数据层访问HttpContext会使工作变得更加困难,特别是在使用单元测试的情况下.解决方案是创建一个服务,以提供应用程序范围的用户信息,例如:
Access the HttpContext from the Data Layer makes the life harder, specially if you use Unit Tests. The solution is to create a service to provide application wide user information, something like:
public interface ICurrentUserService { string UserName {get;} string UserId {get;} string HostIP {get;} // etc. }然后,您可以实施具体的服务并使用您的 首选的IoC容器.
Then you can implement the concrete service and inject it using your preferred IoC container.
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) { ... } }更多推荐
从数据层访问HttpContext和用户身份
发布评论