Creating and Registering of Service Classes (Chapter 6)

    A service that handles user input and returns an instantiated data model class, then controller will output to frontend to rendered the view. This topics will discuss how we create and register service to the Framework.

    To create service, we needed to add a class and an interface class to the folder ‘/Services’

    Create a Service Class

    The interface class is worked as a header file, which contains the declaration of the public functions of the service class. For each service class, we will normally written with four regions. Here is the example of inside the framework:

    public class ClientParcelsService : IClientParcelsService
            #region Properties
            private readonly ApiDataContext _context;
            private readonly IMapper _mapper;
            private readonly AppSettings _appSettings;
            private readonly IGuidRepository<ClientParcel> _clientParcelRepository;
            #region Ctor
            public ClientParcelsService(
                ApiDataContext context,
                IMapper mapper,
                IOptions<AppSettings> appSettings,
                IGuidRepository<ClientParcel> clientParcelRepository)
                _context = context;
                _mapper = mapper;
                _appSettings = appSettings.Value;
                _clientParcelRepository = clientParcelRepository;
            #region Utilities
            #region Methods
            public virtual async Task<IPagedList<ClientParcel>> GetClientParcelList(ClientParcelSearchCommand command)
                var query = from p in _context.ClientParcels
                            select p;
                if (command.ClientGuid != Guid.Empty)
                    query = query.Where(g => g.ClientGuid == command.ClientGuid);
                if (string.IsNullOrWhiteSpace(command.ShipmentCode))
                    query = query.Where(s => s.SourceShipmentCode.Contains(command.ShipmentCode));

    #region Properties / #region Fields

    “Properties” regions place all definitions variable declaration using by the service, which include properties, repositories, services, context and any properties which required to access within the service.

    #region Ctor

    “Ctor” is the constructor function, which initial all the “Properties” and assign the correct initial values or service assignment.

    #region Utilities

    “Utilities” regions always place the private / protection helpers functions with commonly used within the service.

    #region Methods

    “Methods” regions contains the public functions which the service will provide to controller or other services.

    This is not a hard rule but it is a good habit to organize the code for future troubleshooting.

    Create the Interface

    To create the interface, just easy to add a Interface file and write like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using CCOMS.Api.Domain.ConsolidatedOrder;
    using CCOMS.Api.Models.ConsolidatedOrder;
    using BlueSky.Core;
    namespace CCOMS.Api.Services
        public interface IClientParcelsService
            Task<IPagedList<ClientParcel>> GetClientParcelList(ClientParcelSearchCommand command);        

    Get all the public functions inside the service and write the declaration into the interface. Remember to inherit the interface to the service class.

     public class ClientParcelsService : IClientParcelsService

    Registering Service to framework

    After the creation of service and interface, we needed to register the service to the framework so that we can call the service across the projects. To add registration, go and open ‘/infrastructure/DependencyRegistrar.cs’

    using CCOMS.Api.Services;
    using CCOMS.Api.Factories;
    namespace CCOMS.Api.Infrastructure
        public class DependencyRegistrar 
            public void Register(IServiceCollection services)
                services.AddScoped<IClientParcelsService, ClientParcelsService>();
                services.AddScoped<IClientParcelsFactory, ClientParcelsFactory>();

    Overview : Developer Guide for BlueSky .NETCORE API Framework

    Previous : Creating and Registering of Service Classes (Ch.5)

    Next : Creating and Registering Factory, using of AutoMapper Services (Ch.7)

    (c) 2022, BlueSky Information Technology (Int’l) Co. Ltd, All Rights Reserved.

    Recent Articles


    Related Stories

    Stay on op - Ge the daily news in your inbox