應用服務作用是將領域(業務)邏輯暴露給外部(vue前臺等)。外部(vue前臺等)通過傳入DTO(數據傳輸對象)參數來調用應用服務,而應用服務通過領域對象來執行相應的業務邏輯並且將DTO返回。因此,外部(vue前臺等)和領域層將被完全隔離開來。在一個理想的層級項目中,外部(vue前臺等)應該從不直接訪問領域對象。
此部分內容未使用DTO,後續文章會繼續講解
此應用服務層在ABP框架中會生成swagger中接口,供外部(vue前臺)調用,這樣就可以將項目中前後端分離
一、應用服務調用默認倉儲
當ABP框架內默認的倉儲能夠滿足需要時,則直接調用即可
示例:https://blog.csdn.net/sinat_16998945/article/details/97374091 查看內容(一、默認倉儲)
應用服務調用默認倉儲時,只需要編寫服務接口實現類就行, 直接繼承應用服務基類PDAppServiceBase、直接實現默認應用服務接口類IApplicationService
using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Application.Services;
using Abp.Domain.Repositories;
namespace PD.Menu
{
public class MenuAppService : PDAppServiceBase,IApplicationService
{
private readonly IRepository<Sys_Menu> _Sys_MenuRepository;
public MenuAppService(IRepository<Sys_Menu> Sys_MenuRepository)
{
_Sys_MenuRepository = Sys_MenuRepository;
}
public async Task<List<Sys_Menu>> Get()
{
//GetAllListAsync 是自動默認倉儲中方法
return await _Sys_MenuRepository.GetAllListAsync();
}
}
}
二、調用自定義倉儲
當ABP框架內默認的倉儲不能夠滿足需要時,則需要在默認倉儲上進行擴展方法
示例:https://blog.csdn.net/sinat_16998945/article/details/97374091 查看內容(二、自定義倉儲)
應用服務調用自定義倉儲時,需要編寫服務接口和服務接口實現類,如圖
1.服務接口類需要繼承IApplicationService
using Abp.Application.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PD.Menu
{
public interface IMenuAppService : IApplicationService
{
Task<List<Sys_Menu>> GetMenu();
}
}
2.服務接口實現類需要繼承PDAppServiceBase、且實現上面的服務接口類
using System.Collections.Generic;
using System.Threading.Tasks;
using PD.Menu.Repository;
namespace PD.Menu
{
public class MenuAppService : PDAppServiceBase, IMenuAppService
{
private readonly IMenuRepository _menuManager;
public MenuAppService(IMenuRepository menuManager)
{
_menuManager = menuManager;
}
public async Task<List<Sys_Menu>> GetMenu()
{
//GetSys_MenuList是自定義倉儲的方法
var query = await _menuManager.GetSys_MenuList();
//TODO 其他相關操作
return query;
}
}
}
三、調用多個倉儲
當實際需求中,單個倉儲不滿足,也可以調用多個倉儲
using System.Collections.Generic;
using System.Threading.Tasks;
using PD.Menu.Repository;
using PD.Roles;
namespace PD.Menu
{
public class MenuAppService : PDAppServiceBase, IMenuAppService
{
private readonly IMenuRepository _menuManager;
private readonly IRoleAppService _roleAppService;
public MenuAppService(IMenuRepository menuManager,IRoleAppService roleAppService)
{
_menuManager = menuManager;
_roleAppService = roleAppService;
}
public async Task<List<Sys_Menu>> GetMenu()
{
//GetSys_MenuList是自定義倉儲的方法
var query = await _menuManager.GetSys_MenuList();
var query1 = await _roleAppService.GetAllPermissions();
//TODO 其他相關操作
return query;
}
}
}