ABP框架事务总线(EventBus)实现领域事件

一、DTO

using System;
using System.Collections.Generic;
using System.Text;

namespace Leeway.Operation.Dto
{
   public class OperationLogDto
    {
        /// <summary>
        /// 编码
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 操作动作
        /// </summary>
        public string ActionName { get; set; }
        /// <summary>
        /// 单据编号
        /// </summary>
        public string BillNo { get; set; }
        /// <summary>
        /// 操作备注
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 执行动作的状态值
        /// </summary>
        public string Status { get; set; }
        /// <summary>
        /// 执行动作的状态值名称
        /// </summary>
        public string StatusName { get; set; }
        /// <summary>
        /// 操作人userid
        /// </summary>
        public Guid EmployeeUserId { get; set; }
        /// <summary>
        /// 操作人工号
        /// </summary>
        public string EmployeeNo { get; set; }

        public DateTime CreationTime { get; set; }
        /// <summary>
        /// 操作人姓名
        /// </summary>
        public string EmployeeName
        {
            get; set;
        }

    }
}

二、新建事务总线处理器

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;

namespace EventHandlers
{
    public class LeewayEventHandler : ILocalEventHandler<OperationLogDto>, ITransientDependency
    {
        ILeewayService _leewayService;
        public StatementEventHandler(ILeewayService leewayService)
        {
            _leewayService= leewayService;
        }
        public async Task HandleEventAsync(OperationLogDto eventData)
        {
            await _leewayService.AddOperationHistoryAsync(eventData);
        }
    }
}

三、ILeewayService

public iterface ILeewayService{
    Task<bool> AddOperationHistoryAsync(OperationLogDto dto);
}

四、LeewayService

public class LeewayService : ApplicationService, ILeewayService
{
    private readonly ILocalEventBus _localEventBus;
    
    public LeewayService(ILocalEventBus localEventBus)
    {
        _localEventBus = localEventBus;
    }


    public async Task<bool> AddOperationHistoryAsync(OperationLogDto dto)
        {
            //这里用到AutoMapper,需在相关类里添加,例如新建类ApplicationAutoMapperProfile
            var model = ObjectMapper.Map<OperationLogDto, OperationLog>(dto);
              
            var result = await _operationLogRepository.InsertAsync(model); ;
            return result != null;
        }    

    public async Task<bool> test(){
        var user = await _userRepository.FindAsync(CurrentUser.GetId());
        await _localEventBus.PublishAsync(new OperationLogDto
                                    {
                                        Code = "Leeway",
                                        ActionName = "操作日志",
                                        BillNo = "Test111111",
                                        Remark = "",
                                        Status = "1",
                                        EmployeeUserId = new Guid(CurrentUser.Id.ToString()),
                                        EmployeeNo = CurrentUser.UserName,
                                        EmployeeName = user.Surname + user.Name
                                    });
    return true;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值