Aspnet零锅炉维修服务电话

问题描述 投票:1回答:1

我有几个简单的服务可以交换信息:

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;

但是等待我没有的建造者

c# asp.net-core .net-core aspnetboilerplate asp.net-boilerplate
1个回答
2
投票

该设计模式称为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?

© www.soinside.com 2019 - 2024. All rights reserved.