AbpVnext 分布式事件总线

打印 上一主题 下一主题

主题 898|帖子 898|积分 2694

AbpVnext 本地事件总线

补充知识

发布订阅

概念

应用场景

本地事件总线允许服务发布和订阅进程内事件,这意味着如果两个服务>(发布者和订阅者)在同一个进程中运行,那么它是合适的
完整示例
DDD开发规范: 先定义好接口层、后实现层、暴露接口
对于要更新的实体
  1. //领域层的实体
  2. public class Book : FullAuditedEntity<Guid>, IHasExtraProperties, IMultiTenant
  3. {
  4.    public string Name{get;set;}
  5.    public string Author {get;set;}
  6.    public long Price {get;set}
  7.    
  8.    public string Title {get;set;}
  9.    public string Classify{get;set;}
  10.    //条形码
  11.    public string BarCode {get;set;}
  12.    // 出版商
  13.    public string Issue{get;set;}
  14.    // 页数
  15.    public string PageTotal{get;set;}
  16.   
  17.    public string BuyerId{get;set;}
  18.    public DateTime? ModifyTime{get;set}
  19. }
复制代码
  1. //这个实体是必须有的
  2. public class BookRecordEto{
  3.     public string Name{get;set;}
  4.     public string Author {get;set;}
  5.     public long Price {get;set}
  6.     public DateTime? ModifyTime{get;set}
  7. }
复制代码
  1. //发布事件
  2. public class BookService: ITransientDependency
  3. {
  4.     //Abp使用了字典来保存对实例的引用
  5.     public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
  6.     //按需注入
  7.     protected ILocalEventBus  LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus >();
  8.     //仓储--对应Book的数据库表
  9.     protected IBookRepository BookRepository => LazyGetRequiredService<IBookRepository>();
  10.     public async Task UpdateData(Guid id)
  11.     {
  12.         var book =  await BookRepository.FindAsync(x=>x.Id = id);
  13.         if (book == null){//不存在实体}
  14.         var updateEntity = await BookRepository.UpdateAsync(book);
  15.         if (updateEntity == null){//更新失败}
  16.         
  17.         //实体数据发生变更,发布事件
  18.         await LocalEventBus.PublishAsync(new BookEto(){
  19.             Name = book.Name;
  20.             Author = book.Author;
  21.             Price = book.Price;
  22.             ModifyTime = DateTime.Now;
  23.         });
  24.     }
  25. }
复制代码
  1. //订阅事件
  2. using Volo.Abp.DependencyInjection;
  3. using Volo.Abp.EventBus;
  4. public class EventBusHandler: ApplicationService,
  5.       , ILocalEventHandler<BookRecordEto>
  6.       , ITransientDependency
  7. {
  8.     // 进行业务数据更新,比如要更新购买这边书读者记录
  9.     public IAbpLazyServiceProvider LazyServiceProvider{get;set;}
  10.     protected IBookBoughtRecordRepository BoughtRecordRepository => LazyServiceProvider.LazyGetRequiredService<IBookBoughtRecordRepository>();
  11.     [UnitOfWork]//这个特性以及方法头里面的virtual很重要
  12.     public async virtual Task HandleEventAsync(BookRecordEto eventData)
  13.     {
  14.         //获取购买记录
  15.         var record =  await  BoughtRecordRepository.GetQueryableAsync();
  16.         var recordList = await AsyncExecuter.ToListAsync(record);
  17.         
  18.         if(recordList.Count() != 0){
  19.             var boughtRecord  = ObjectMapper.Map(eventData, new BoughtRecord());
  20.             //插入一条数据
  21.             await BoughtRecordRepository.InsertAsync(boughtRecord);
  22.         }
  23.     }
  24. }
复制代码
总结

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

钜形不锈钢水箱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表