首页 > 编程语言 >abp vnext blog模块用户信息同步 源码解析

abp vnext blog模块用户信息同步 源码解析

时间:2022-12-17 22:00:41浏览次数:50  
标签:vnext localUser userRepository return cancellationToken await bloguser blog 源码

先看一下Volo.Blogging.Domain引用的关于用户的项目只有Volo.Abp.Users.Domain,再看BlogUser定义:

    public class BlogUser : AggregateRoot<Guid>, IUser, IUpdateUserData
    {
        public virtual Guid? TenantId { get; protected set; }
...

先记下这个点:IUpdateUserData ,查看源码

public interface IUpdateUserData
{
    bool Update([NotNull] IUserData user);
}

查看文件BlogUserLookupService

    public class BlogUserLookupService : UserLookupService<BlogUser, IBlogUserRepository>, IBlogUserLookupService
    {
        public BlogUserLookupService(
            IBlogUserRepository userRepository,
            IUnitOfWorkManager unitOfWorkManager)
            : base(
                userRepository,
                unitOfWorkManager)
        {
            
        }

        protected override BlogUser CreateUser(IUserData externalUser)
        {
            return new BlogUser(externalUser);
        }
    }

啥也没干就继承了泛型类UserLookupService,重点看UserLookupService类,这个类有三个主要方法:FindByIdAsync,FindByUserNameAsync,SearchAsync,主要看下FindByIdAsync方法:

    public async Task<TUser> FindByIdAsync(Guid id, CancellationToken cancellationToken = default)
    {

        // _userRepository继承类的泛型,对应IBlogUserRepository,localUser 指blog模块中的用户信息
        var localUser = await _userRepository.FindAsync(id, cancellationToken: cancellationToken);
        // IExternalUserLookupServiceProvider搜索整个源码文件只用IdentityUserRepositoryExternalUserLookupServiceProvider和HttpClientExternalUserLookupServiceProvider实现这个接口,
        // 一般情况下这个应该不为空,如果ExternalUserLookupServiceProvider为空,即返回
        if (ExternalUserLookupServiceProvider == null)
        {
            return localUser;
        }

        // SkipExternalLookupIfLocalUserExists在UserLookupService默认为true,其他地方未修改
        // 本地bloguser不存在且SkipExternalLookupIfLocalUserExists返回bloguser
        if (SkipExternalLookupIfLocalUserExists && localUser != null)
        {
            return localUser;
        }
        IUserData externalUser;

        try
        {
            // 使用ExternalUserLookupServiceProvider方法获取User数据,这个ExternalUserLookupServiceProvider是IdentityUserRepositoryExternalUserLookupServiceProvider
            // 
            externalUser = await ExternalUserLookupServiceProvider.FindByIdAsync(id, cancellationToken);
            // IdentityUserRepositoryExternalUserLookupServiceProvider也不存在User信息
            if (externalUser == null)
            {
                // 删除bloguser
                if (localUser != null)
                {
                    //TODO: Instead of deleting, should be make it inactive or something like that?
                    await WithNewUowAsync(() => _userRepository.DeleteAsync(localUser, cancellationToken: cancellationToken));
                }

                return null;
            }
        }
        catch (Exception ex)
        {
            Logger.LogException(ex);
            return localUser;
        }
        // externalUser 不为空。bloguser为空,插入bloguser库
        if (localUser == null)
        {
            await WithNewUowAsync(() => _userRepository.InsertAsync(CreateUser(externalUser), cancellationToken: cancellationToken));
            return await _userRepository.FindAsync(id, cancellationToken: cancellationToken);
        }
        // bloguser不为空,实现IUpdateUserData接口且存在更新内容,则更新bloguser数据
        if (localUser is IUpdateUserData && ((IUpdateUserData)localUser).Update(externalUser))
        {
            await WithNewUowAsync(() => _userRepository.UpdateAsync(localUser, cancellationToken: cancellationToken));
        }
        else
        {
            return localUser;
        }

        return await _userRepository.FindAsync(id, cancellationToken: cancellationToken);
    }


到此位置,当在blog模块中调用UserLookupService时,获取到的用户信息是和Identity模块中用户信息是同步的,Identity有blog没的添加一个,Identity没blog有删除一个,Identity和blog不一样的更新到blog一个,然后再看看BlogUserSynchronizer类,这个类发布一个事件:

public async Task HandleEventAsync(EntityUpdatedEto<UserEto> eventData)
{
  // 本地库bloguser查找
   var user = await UserRepository.FindAsync(eventData.Entity.Id);
   if (user == null)
   {       
       // 本地查不到,看看Identity里有没,
       user = await UserLookupService.FindByIdAsync(eventData.Entity.Id);
       if (user == null)
       {
          return;
       }
   }
   // 不管哪有,最后更新到bloguser库里
   if (user.Update(eventData.Entity))
   {
       await UserRepository.UpdateAsync(user);
   }
}

至此,完成所有同步工作。

貌似还有点糊涂,欢迎大家指导!

 

标签:vnext,localUser,userRepository,return,cancellationToken,await,bloguser,blog,源码
From: https://www.cnblogs.com/indexlang/p/16978976.html

相关文章

  • Java超市系统超市自提超市多商家系统源码超市自提网站
    简介Ssm多商家超市自提系统。用户注册申请开店成为商家,普通注册用户下单时选择离自己较近的自提点次日取货。管理员进行店铺审核、用户、分类管理等。演示视频https://w......
  • Python+QT美颜工具源码
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • Gorm源码学习-创建行记录
    1.前言Gorm源码学习系列Gorm源码学习-数据库连接此文是Gorm源码学习系列的第二篇,主要梳理下通过Gorm创建表的流程。 2.创建行记录代码示例gorm提供了以下几个接......
  • 基于struts+hibernate的采购管理系统的分析与实现(论文+PPT+源码)
    基于struts+hibernate的采购管理系统的分析与实现摘要当今社会己进入信息社会时代,信息己经受到社会的广泛关注,被看作社会和科学技术发展的三大支柱(材料、能源、信息)之一。......
  • Java_JSP电子政务网(论文+源码)
    摘要当前阶段,伴随着社会信息技术的快速发展,使得电子政务能够成为我国政府职能部门进行办公管理的一个重要内容,其中就包括了提升办公效率完善服务质量以及实现信息共享等。......
  • 医院管理住院系统的研究与实现(论文+PPT+源码)
    分类号_______________密级________________UDC_______________学号____毕业设计(论文)论文题目医院管理住院系统的研究与实现ThesisTopicResearchandImplementationo......
  • 面向特定群体的健康管理平台(论文+PPT+源码)
    毕业设计说明书面向特定群体的健康管理平台设计与实现班级:学号:姓名:软件学院学院:软件工程专业:指导教师:2014年6月面向特定群体的健康管理平台设计与实现摘要随着网络技......
  • 网络考试系统的设计与实现(论文+PPT+源码)
    毕业设计(论文)任务书第1页毕业设计(论文)题目:网络考试系统的设计与实现毕业设计(论文)要求及原始数据(资料):了解国内外网络考试系统的发展现状,深入分析考试系统的工作流程;根据考试......
  • 户籍管理系统的设计与实现(论文+PPT+源码)
    户籍管理系统的设计与实现摘要当今社会人们生活质量越来越高,人们对生活品质的追求不断提升,对于孩子求学,变更住所等情况时有发生,因此对于户籍变动管理就显得十分重要,管理用......
  • 安卓APP课程设计源码和报告——个人日记本开发
    Android开发基础题目: 个人日记本开发题目个人日记本开发???关键技术开发过程中使用到的技术:利用SQLite存储数据主要体现类:DbHelper类继承自SQLiteOpenHelper类,重写其构造方法......