Search code examples
c#asp.net-core.net-coreaspnetboilerplateasp.net-boilerplate

Service call to service at Aspnet Zero Boilerplate


I have a couple of simple services that exchange information:

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;
        }

    }

OK, it works properly and I just have to send the session to work. But how can I do it simply when I have to get information from a more complex service? Like the default Boilerplate? For example, 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);
        }
    }

As you can see, the constructor is more complex, the constructor data comes directly defined by swagger (ASP.NET Boilerplate creates dynamic drivers and swagger, and it is these that carry this data that they use as a builder), but when making the call from another service I can't get them.

What is the best way to do that is edit the minimum the second?

In Service2, where I have to call EditionAppService.GetEditions I need something like:

EditionAppService _editionAppService = new EditionAppService();
_editionAppService.GetEditions().Result;

But wait for the builder I don't have


Solution

  • That design pattern is called Dependency Injection.

    Do this instead:

    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;
        }
    
        // ...
    }
    

    Related: Should I be calling an AppService from another AppService?