ABP vNext微服务架构详细教程(补充篇)——单层模板(下)

业务代码

2 聚合服务

聚合服务层和基础服务层相同的道理,在Demo.Core.Contracts增加Services文件夹,并添加Notifications子文件夹,在其中添加Dtos文件夹并添加两个DTO与基础服务对应:.

using Volo.Abp.Application.Dtos;
namespace Demo.Core.Contracts.Services.Notifications.Dtos;
public class CoreNotificationCreateDto:EntityDto<Guid>{    /// <summary>    /// 接受着用户ID    /// </summary>    public Guid ReceiverId { get; set; }
    /// <summary>    /// 标题    /// </summary>    public string Title { get; set; }
    /// <summary>    /// 内容    /// </summary>    public string Content { get; set; }
    /// <summary>    /// 是否已读    /// </summary>    public bool IsRead { get; set; }}
using Volo.Abp.Auditing;
namespace Demo.Core.Contracts.Services.Notifications.Dtos;
public class CoreNotificationDto : CoreNotificationCreateDto,ICreationAuditedObject{    /// <summary>    /// 创建时间    /// </summary>    public DateTime CreationTime { get; set; }        /// <summary>    /// 创建人    /// </summary>    public Guid? CreatorId { get; set; }}
在Demo.Core.Contracts项目中Services/Notifications下添加接口ICoreNotificationAppService如下:
using Demo.Core.Contracts.Services.Notifications.Dtos;using Volo.Abp.Application.Dtos;using Volo.Abp.Application.Services;
namespace Demo.Core.Contracts.Services.Notifications;
public interface ICoreNotificationAppService:IApplicationService{    Task<PagedResultDto<CoreNotificationDto>> GetAllAsync(PagedAndSortedResultRequestDto request);
    Task<bool> CreatAsync(CoreNotificationCreateDto request);}
在Demo.Core项目中的Services下创建Notifications文件夹并添加实现类CoreNotificationAppService:
using Demo.Core.Contracts.Services.Notifications;using Demo.Core.Contracts.Services.Notifications.Dtos;using Demo.NotificationManager.Contracts.Services.Notifications;using Demo.NotificationManager.Contracts.Services.Notifications.Dtos;using Mapster;using Volo.Abp.Application.Dtos;using Volo.Abp.Application.Services;
namespace Demo.Core.Services.Notifications;
public class CoreNotificationAppService:ApplicationService,ICoreNotificationAppService{    private readonly INotificationAppService _notificationAppService;
    public CoreNotificationAppService(INotificationAppService notificationAppService)    {        _notificationAppService = notificationAppService;    }
    public async Task<PagedResultDto<CoreNotificationDto>> GetAllAsync(PagedAndSortedResultRequestDto request)    {        var ret = await _notificationAppService.GetListAsync(request);        return new PagedResultDto<CoreNotificationDto>(ret.TotalCount, ret.Items.Adapt<List<CoreNotificationDto>>());    }
    public async Task<bool> CreatAsync(CoreNotificationCreateDto request)    {        var notification = request.Adapt<NotificationDto>();        await _notificationAppService.CreateAsync(notification);        return true;    }}
修改Demo.Core项目appsettings.json文件如下:
{  "urls": "http://*:6003",  "RemoteServices": {    "NitificationManager": {      "BaseUrl": "http://localhost:5003/"    }  }}
到这里,我们最基础的一个聚合层服务和基础层服务都创建完毕。

补充说明

如果我们使用的是单一服务或者没有使用聚合层,可以只使用基础服务的用法。

如果想继续使用AutoMapper框架作为实体映射,可在创建项目时保留AutoMapper相关内容。

这篇文章中我们完成了删减的过程,依据我们的需要,我们再增加所需要的缓存、鉴权、当前用户传递等代码,具体可参见《ABP vNext微服务架构详细教程》系列完整文章。

为大家创建单层应用方便,我将编写一个单层模板项目代码生成器,上传至https://gitee.com/lightnehum/abp-single-layer-builder,请大家关注并使用。