访问MVC存储库中的Identity对象 [英] Accessing the Identity object in a MVC repository

查看:103
本文介绍了访问MVC存储库中的Identity对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个漂亮的通用存储库,为我的许多业务实体做基本的CRUD。
实体承载形成一个通用对象,它具有我为所有对象维护的几个字段。
例如ModifiedBy,CreatedBy,CreatedDate,ModifiedDate。



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



我的问题是:
有没有办法从我的存储库中的MVC Web应用程序访问Identity对象?
我希望在一次镜头中将修改设置为身份用户的任何更新?



Best Regard,
Rod

解决方案

嗯,你可以直接在你的存储库中访问 HttpContext.Current.User.Identity ,但我不会推荐它,因为它将使您的存储库与HttpContext相关。



替代方法:




  • 在创建/更新方法中添加一个显式的 IIdentity 参数,例如 void Update(T entity,IIdentity user)

  • 解除获取当前用户的逻辑,例如: / p>

      class GenericRepository< T>:IRepository< T> {
    private readonly ICurrentUserFetcher currentUserFetcher;
    public GenericRepository< T>(ICurrentUserFetcher currentUserFetcher){
    this.currentUserFetcher = currentUserFetcher;
    }
    public void Update(T entity){
    var currentUser = currentUserFetcher.Get();
    ...
    }
    }
    接口ICurrentUserFetcher {
    IIdentity Get();
    }
    class WebCurrentUserFetcher:ICurrentUserFetcher {
    public IIdentity Get(){return HttpContext.Current.User.Identity;}
    }

    甚至更简单:

      class GenericRepository&T ;:存储库< 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);

    使用IoC容器接线。



I have a pretty generic repository that does basic CRUD for many of my business entities. The entities enherit form a generic object that has a few fields I maintain for all objects. eg. ModifiedBy, CreatedBy, CreatedDate, ModifiedDate.

These fields ModifiedBy and CreatedBy will always be set before any update/save.

My questions is: Is there any way to gain access to the Identity object from my MVC web application in my repositories? I was hoping to set the modifiedby to the identity user for any update in one shot??

Best Regard, Rod

解决方案

Well, you could access HttpContext.Current.User.Identity directly in your repositories, but I wouldn't recommend it as it will make your repositories HttpContext-dependent.

Alternatives:

  • Add an explicit IIdentity parameter in your Create/Update methods, e.g. void Update(T entity, IIdentity user)
  • Decouple the logic of "getting the current user", e.g.:

    class GenericRepository<T>: IRepository<T> {
        private readonly ICurrentUserFetcher currentUserFetcher;
        public GenericRepository<T>(ICurrentUserFetcher currentUserFetcher) {
            this.currentUserFetcher = currentUserFetcher;
        }
        public void Update(T entity) {
            var currentUser = currentUserFetcher.Get();
            ...
        }
    }
    interface ICurrentUserFetcher {
        IIdentity Get();
    }
    class WebCurrentUserFetcher: ICurrentUserFetcher {
        public IIdentity Get() {return HttpContext.Current.User.Identity;}
    }
    

    Or even simpler:

    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);
    

    Use an IoC container to wire things up.

这篇关于访问MVC存储库中的Identity对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆