ABP框架—后台:应用服务ApplicationServices(9)
应用服务作用是将领域(业务)逻辑暴露给外部(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;
}
}
}