業務代碼
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,請大家關注并使用。
關注我獲得
更多精彩