Aspnet 零样板中的服务调用
Service call to service at Aspnet Zero Boilerplate
我有几个交换信息的简单服务:
public class Service2: PPlusAppServiceBase
{
private readonly IAbpSession _session;
public Service2(IAbpSession session)
{
_session = session;
}
public Entity getEntity()
{
Entity et = new Entity();
Service1 _service1 = new Service1(_session);
[...]
_service1.getEntity();
[...]
return et;
}
}
public class Service1: PPlusAppServiceBase
{
private readonly IAbpSession _session;
public Service1(IAbpSession session)
{
_session = session;
}
public Entity getEntity()
{
_session.[...]
return et;
}
}
好的,它工作正常,我只需要发送会话即可。但是,当我必须从更复杂的服务中获取信息时,我该如何简单地做到这一点呢?喜欢默认的样板文件吗?例如,EditionAppService
:
public class EditionAppService : PPlusAppServiceBase, IEditionAppService
{
private readonly EditionManager _editionManager;
private readonly IRepository<SubscribableEdition> _editionRepository;
private readonly IRepository<Tenant> _tenantRepository;
private readonly IBackgroundJobManager _backgroundJobManager;
public EditionAppService(
EditionManager editionManager,
IRepository<SubscribableEdition> editionRepository,
IRepository<Tenant> tenantRepository,
IBackgroundJobManager backgroundJobManager)
{
_editionManager = editionManager;
_editionRepository = editionRepository;
_tenantRepository = tenantRepository;
_backgroundJobManager = backgroundJobManager;
}
[AbpAuthorize(AppPermissions.Pages_Editions)]
public async Task<ListResultDto<EditionListDto>> GetEditions()
{
var editions = await (from edition in _editionRepository.GetAll()
join expiringEdition in _editionRepository.GetAll() on edition.ExpiringEditionId equals expiringEdition.Id into expiringEditionJoined
from expiringEdition in expiringEditionJoined.DefaultIfEmpty()
select new
{
Edition = edition,
expiringEditionDisplayName = expiringEdition.DisplayName
}).ToListAsync();
var result = new List<EditionListDto>();
foreach (var edition in editions)
{
var resultEdition = ObjectMapper.Map<EditionListDto>(edition.Edition);
resultEdition.ExpiringEditionDisplayName = edition.expiringEditionDisplayName;
result.Add(resultEdition);
}
return new ListResultDto<EditionListDto>(result);
}
}
可以看到,构造器比较复杂,构造器数据直接由swagger
定义而来(ASP.NET Boilerplate
创建动态驱动和swagger
,携带的就是这些他们用作构建器的这些数据),但是当从另一个服务发出调用时我无法获取它们。
最好的方法是第二个编辑最小值?
在 Service2
中,我必须调用 EditionAppService.GetEditions
我需要这样的东西:
EditionAppService _editionAppService = new EditionAppService();
_editionAppService.GetEditions().Result;
但是等待我没有的构建器
那个设计模式叫做Dependency Injection。
改为这样做:
public class Service2: PPlusAppServiceBase
{
private readonly EditionAppService _editionAppService; // Add this
private readonly Service1 _service1; // Add this
private readonly IAbpSession _session;
public Service2(
EditionAppService editionAppService, // Add this
Service1 service1, // Add this
IAbpSession session)
{
_editionAppService = editionAppService; // Add this
_service1 = service1; // Add this
_session = session;
}
public Entity getEntity()
{
Entity et = new Entity();
// Service1 _service1 = new Service1(_session); // Remove this
// ...
_service1.getEntity();
// ...
return et;
}
// ...
}
相关:
我有几个交换信息的简单服务:
public class Service2: PPlusAppServiceBase
{
private readonly IAbpSession _session;
public Service2(IAbpSession session)
{
_session = session;
}
public Entity getEntity()
{
Entity et = new Entity();
Service1 _service1 = new Service1(_session);
[...]
_service1.getEntity();
[...]
return et;
}
}
public class Service1: PPlusAppServiceBase
{
private readonly IAbpSession _session;
public Service1(IAbpSession session)
{
_session = session;
}
public Entity getEntity()
{
_session.[...]
return et;
}
}
好的,它工作正常,我只需要发送会话即可。但是,当我必须从更复杂的服务中获取信息时,我该如何简单地做到这一点呢?喜欢默认的样板文件吗?例如,EditionAppService
:
public class EditionAppService : PPlusAppServiceBase, IEditionAppService
{
private readonly EditionManager _editionManager;
private readonly IRepository<SubscribableEdition> _editionRepository;
private readonly IRepository<Tenant> _tenantRepository;
private readonly IBackgroundJobManager _backgroundJobManager;
public EditionAppService(
EditionManager editionManager,
IRepository<SubscribableEdition> editionRepository,
IRepository<Tenant> tenantRepository,
IBackgroundJobManager backgroundJobManager)
{
_editionManager = editionManager;
_editionRepository = editionRepository;
_tenantRepository = tenantRepository;
_backgroundJobManager = backgroundJobManager;
}
[AbpAuthorize(AppPermissions.Pages_Editions)]
public async Task<ListResultDto<EditionListDto>> GetEditions()
{
var editions = await (from edition in _editionRepository.GetAll()
join expiringEdition in _editionRepository.GetAll() on edition.ExpiringEditionId equals expiringEdition.Id into expiringEditionJoined
from expiringEdition in expiringEditionJoined.DefaultIfEmpty()
select new
{
Edition = edition,
expiringEditionDisplayName = expiringEdition.DisplayName
}).ToListAsync();
var result = new List<EditionListDto>();
foreach (var edition in editions)
{
var resultEdition = ObjectMapper.Map<EditionListDto>(edition.Edition);
resultEdition.ExpiringEditionDisplayName = edition.expiringEditionDisplayName;
result.Add(resultEdition);
}
return new ListResultDto<EditionListDto>(result);
}
}
可以看到,构造器比较复杂,构造器数据直接由swagger
定义而来(ASP.NET Boilerplate
创建动态驱动和swagger
,携带的就是这些他们用作构建器的这些数据),但是当从另一个服务发出调用时我无法获取它们。
最好的方法是第二个编辑最小值?
在 Service2
中,我必须调用 EditionAppService.GetEditions
我需要这样的东西:
EditionAppService _editionAppService = new EditionAppService();
_editionAppService.GetEditions().Result;
但是等待我没有的构建器
那个设计模式叫做Dependency Injection。
改为这样做:
public class Service2: PPlusAppServiceBase
{
private readonly EditionAppService _editionAppService; // Add this
private readonly Service1 _service1; // Add this
private readonly IAbpSession _session;
public Service2(
EditionAppService editionAppService, // Add this
Service1 service1, // Add this
IAbpSession session)
{
_editionAppService = editionAppService; // Add this
_service1 = service1; // Add this
_session = session;
}
public Entity getEntity()
{
Entity et = new Entity();
// Service1 _service1 = new Service1(_session); // Remove this
// ...
_service1.getEntity();
// ...
return et;
}
// ...
}
相关: