我有几个简单的服务可以交换信息:
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;
}
// ...
}
相关:Should I be calling an AppService from another AppService?