Nhibernate 访问MVC存储库中的标识对象

Nhibernate 访问MVC存储库中的标识对象,nhibernate,model-view-controller,repository,design-patterns,Nhibernate,Model View Controller,Repository,Design Patterns,我有一个相当通用的存储库,它为我的许多业务实体提供基本的CRUD。 entities enherit形成了一个通用对象,它有几个我为所有对象维护的字段。 例如,ModifiedBy,CreatedBy,CreatedDate,ModifiedDate 这些字段ModifiedBy和CreatedBy将始终在任何更新/保存之前设置 我的问题是: 是否有任何方法可以从存储库中的MVC web应用程序访问Identity对象? 我希望在一次更新中将modifiedby设置为identity user

我有一个相当通用的存储库,它为我的许多业务实体提供基本的CRUD。 entities enherit形成了一个通用对象,它有几个我为所有对象维护的字段。 例如,ModifiedBy,CreatedBy,CreatedDate,ModifiedDate

这些字段ModifiedBy和CreatedBy将始终在任何更新/保存之前设置

我的问题是: 是否有任何方法可以从存储库中的MVC web应用程序访问Identity对象? 我希望在一次更新中将modifiedby设置为identity user

致以最崇高的敬意,
Rod

好吧,您可以直接在存储库中访问HttpContext.Current.User.Identity,但我不推荐这样做,因为这样会使存储库依赖于HttpContext

备选方案:

  • 在创建/更新方法中添加显式的
    IIdentity
    参数,例如
    void Update(T实体,IIdentity用户)
  • 解耦“获取当前用户”的逻辑,例如:

    class GenericRepository:IRepository{
    私有只读ICurrentUserFetcher currentUserFetcher;
    公共GenericRepository(ICurrentUserFetcher currentUserFetcher){
    this.currentUserFetcher=currentUserFetcher;
    }
    公共无效更新(T实体){
    var currentUser=currentUserFetcher.Get();
    ...
    }
    }
    接口ICurrentUserFetcher{
    身份获取();
    }
    类WebCurrentUserFetcher:ICurrentUserFetcher{
    public IIdentity Get(){return HttpContext.Current.User.Identity;}
    }
    
    或者更简单:

    class GenericRepository<T>: IRepository<T> {
        private readonly Func<IIdentity> currentUserFetcher;
        public GenericRepository<T>(Func<IIdentity> currentUserFetcher) {
            this.currentUserFetcher = currentUserFetcher;
        }
        public void Update(T entity) {
            var currentUser = currentUserFetcher();
            ...
        }
    }
    var repo = new GenericRepository<Person>(() => HttpContext.Current.User.Identity);
    
    class GenericRepository:IRepository{
    私有只读函数currentUserFetcher;
    公共GenericRepository(Func currentUserFetcher){
    this.currentUserFetcher=currentUserFetcher;
    }
    公共无效更新(T实体){
    var currentUser=currentUserFetcher();
    ...
    }
    }
    var repo=newgenericrepository(()=>HttpContext.Current.User.Identity);
    
    用一个IoC容器把东西连接起来


嗯,您可以直接在存储库中访问HttpContext.Current.User.Identity,但我不推荐这样做,因为这样会使存储库依赖于HttpContext

备选方案:

  • 在创建/更新方法中添加显式的
    IIdentity
    参数,例如
    void Update(T实体,IIdentity用户)
  • 解耦“获取当前用户”的逻辑,例如:

    class GenericRepository:IRepository{
    私有只读ICurrentUserFetcher currentUserFetcher;
    公共GenericRepository(ICurrentUserFetcher currentUserFetcher){
    this.currentUserFetcher=currentUserFetcher;
    }
    公共无效更新(T实体){
    var currentUser=currentUserFetcher.Get();
    ...
    }
    }
    接口ICurrentUserFetcher{
    身份获取();
    }
    类WebCurrentUserFetcher:ICurrentUserFetcher{
    public IIdentity Get(){return HttpContext.Current.User.Identity;}
    }
    
    或者更简单:

    class GenericRepository<T>: IRepository<T> {
        private readonly Func<IIdentity> currentUserFetcher;
        public GenericRepository<T>(Func<IIdentity> currentUserFetcher) {
            this.currentUserFetcher = currentUserFetcher;
        }
        public void Update(T entity) {
            var currentUser = currentUserFetcher();
            ...
        }
    }
    var repo = new GenericRepository<Person>(() => HttpContext.Current.User.Identity);
    
    class GenericRepository:IRepository{
    私有只读函数currentUserFetcher;
    公共GenericRepository(Func currentUserFetcher){
    this.currentUserFetcher=currentUserFetcher;
    }
    公共无效更新(T实体){
    var currentUser=currentUserFetcher();
    ...
    }
    }
    var repo=newgenericrepository(()=>HttpContext.Current.User.Identity);
    
    用一个IoC容器把东西连接起来


谢谢毛里西奥,你给了我一些很好的选择。我试图避免与http上下文耦合,因此我想我将尝试第二种选择。我对本文中的人如何实现第二个示例中的ICurrentUserFetcher感到有点困惑。我假设它将提供IIdentity无法提供的额外属性。有没有人可以提供这个代码的清晰性,因为它会在我必须做的事情上派上用场?老实说,我对Unity容器或任何IoC容器都是新手。如何将IIdentity注入存储库?@user1790300它是
Func
,而不是
IIdentity
。不知道Unity,试试文档或者创建一个新的更具体的问题。谢谢毛里西奥,你给了我一些很好的选择。我试图避免与http上下文耦合,因此我想我将尝试第二种选择。我对本文中的人如何实现第二个示例中的ICurrentUserFetcher感到有点困惑。我假设它将提供IIdentity无法提供的额外属性。有没有人可以提供这个代码的清晰性,因为它会在我必须做的事情上派上用场?老实说,我对Unity容器或任何IoC容器都是新手。如何将IIdentity注入存储库?@user1790300它是
Func
,而不是
IIdentity
。不知道Unity,试试文档或者创建一个新的更具体的问题。