Flexbase
  • Flexbase
  • Introduction
  • #JoyofCoding with Flexbase
  • Core Concepts
    • Flexbase
    • Flexstudio
      • Commands & Events
  • Thinking in Flexbase
    • Features and Modules
    • Designing APIs using Flexbase Control Flow
  • Sample Application
  • Solution Structure
    • Getting Started
      • Application
        • Introduction to Solution
        • Demo - Solution Generation
        • Solution Structure Overview
        • Architecture Overview
        • Clean architecture layer
        • Common Configurations
      • Features
        • Understanding Feature
        • Use Case
        • First Feature - Add Customer
        • Second Feature - Update Customer
        • Third Feature - Delete Customer
        • Query Features
  • Input Output Models
  • Messages
  • Domain
  • Pre Bus
  • Post Bus
  • Subscribers
  • Bus Gamma
  • Persistence
  • Hosting
  • Startup Code - WebAPI
  • Startup Code - Background Job
  • Endpoint - AppSettings
  • Bridge
  • Basic CRUD - Insert & Update
  • Introduction
  • View Generated API Definition in Swagger
  • Walkthrough Generated Code Insert
  • Walkthrough Generated Code Query GetById
  • Domain Model
  • Migration
  • AppSettings
  • Bus : How Basic Routing Config Works
  • INSERT : Input API Model
  • INSERT : Attribute Validation
  • INSERT : Plugin Validation
  • INSERT : Mapper
  • INSERT : Controller
  • INSERT : Services
  • INSERT : PostBus Handler
  • INSERT : PostBus Subscriber
  • INSERT : Demo In Action
  • QUERY : Output API Model
  • QUERY : Build and Fetch Pattern
  • QUERY : Demo In Action
  • Basic CRUD 2
    • Introduction
    • View Generated API Definition In Swagger
  • Walkthrough Generated Code Update
  • Walkthrough Generated Code Delete
  • Walkthrough Generated Code GetList
  • Walkthrough Generated Code Get Paged List
  • Update In Action
  • Delete In Action
  • GetList In Action
  • GetPagedList In Action
  • Konarch
Powered by GitBook
On this page

Walkthrough Generated Code GetList

BasicCRUD Part 2

PreviousWalkthrough Generated Code DeleteNextWalkthrough Generated Code Get Paged List

Last updated 4 years ago

Now let us walk through the generated code for the GetList. Let start with the generated output api model.

public class GetCustomersForLookupOutputAPIModel : IFlexOutputAPIModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
    }

So if you see here in web controller we have the GetCustomersForLookup is the GetList.

public partial class CustomerController : FlexControllerBridge
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet()]
        [Route("GetCustomersForLookup")]
        [ProducesResponseType(typeof(IEnumerable<GetCustomersForLookupOutputAPIModel>), 200)]
        public IActionResult GetCustomersForLookup([FromQuery]GetCustomersForLookupParams parameters)
        {
            FlexHostContextInfoBridge hostContextInfo = new FlexHostContextInfoBridge();
            hostContextInfo.Populate<IFlexHostHttpContextAccesorBridge>(_appHttpContextAccessor);
            parameters.SetHostContextInfo(hostContextInfo);

            return Ok(ProcessCustomerService.GetCustomersForLookup(parameters));
        }
    }

Since the parameter get customer by lookup uses the same ContextInfo, we cannot expose those parameter to the API. So its implemented as protected. That is the ideal way that works like that. So to avoid exposing the parameter to the API. This particular parameter is made protected.

Lets look at the Services layer.

public partial class ProcessCustomerService : IProcessCustomerService
    {
        /// <summary>
        /// YourRemarksForMethod
        /// </summary>
        /// <param name="params"></param>
        /// <returns></returns>
        public IEnumerable<GetCustomersForLookupOutputAPIModel> GetCustomersForLookup(GetCustomersForLookupParams @params)
        {
            return _flexHost.GetFlexiQuery<GetCustomersForLookup>().AssignParameters(@params).Fetch();
        }
    }

Lets look at the Query:

public class GetCustomersForLookup : FlexiQueryEnumerableBridge<Customer, GetCustomersForLookupOutputAPIModel>
    {
        private IFlexQueryRepositoryBridge _repoFlex;
        readonly ILogger<GetCustomersForLookup> _logger;
        readonly IReadDbConnectionProviderBridge _connectionProvider;
        private GetCustomersForLookupParams _params;
        IMapper _mapper;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="repoFlex"></param>
        /// <param name="logger"></param>
        public GetCustomersForLookup(IFlexQueryRepositoryBridge repoFlex, ILogger<GetCustomersForLookup> logger, IMapper mapper, IReadDbConnectionProviderBridge connectionProvider)
        {
            _repoFlex = repoFlex;
            _logger = logger;
            _mapper = mapper;
            _connectionProvider = connectionProvider;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="params"></param>
        /// <returns></returns>
        public GetCustomersForLookup AssignParameters(GetCustomersForLookupParams @params)
        {
            _params = @params;
            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<GetCustomersForLookupOutputAPIModel> Fetch()
        {
            var stopwatch = Stopwatch.StartNew();

            var result = Build<Customer>().ProjectTo<GetCustomersForLookupOutputAPIModel>(_mapper.ConfigurationProvider).ToList();

            Trace.WriteLine($"Query GetCustomersForLookup execution time: {stopwatch.ElapsedMilliseconds} ms");

            return result;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected override IQueryable<T> Build<T>()
        {
            _connectionProvider.ConfigureDbConnectionString(_params.GetHostContextInfo());
            _repoFlex.InitializeConnection(_connectionProvider);

            IQueryable<T> query = _repoFlex.FindAll<T>();
            
            if (!String.IsNullOrEmpty(_params.Name))
                query = query.Where(c => c.Name == _params.Name);

            //Build Your Query Here

            return query;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class GetCustomersForLookupParams : FlexiQueryParameterBridge
    {
        public string Name { get; set; }
    }

GetList implements the FlexiQueryEnumerableBridge. The same way we have the Fetch and we have the ProjectTo so do the mapping for the customer model to this output API model. So once the query is executed the control flows back to the services and the controller gives the result back to the consumer of the API.