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

QUERY : Build and Fetch Pattern

BasicCRUD Insert Query with Bridge

PreviousQUERY : Output API ModelNextQUERY : Demo In Action

Last updated 4 years ago

Now, let us understand the build and fetch pattern using which we execute all our queries.

The query gets executed in the process or end point where pre-bus runs.

So before that, let us have a look at our controller action GetCustomerById.

public partial class CustomerController : FlexControllerBridge
    {
        [HttpGet()]
        [Route("GetCustomerById/{id}")]
        public GetCustomerByIdOutputAPIModel GetCustomerById(string id)
        {
            GetCustomerByIdParams parameters = new GetCustomerByIdParams();

            FlexHostContextInfoBridge hostContextInfo = new FlexHostContextInfoBridge();
            hostContextInfo.Populate<IFlexHostHttpContextAccesorBridge>(_appHttpContextAccessor);
            parameters.SetHostContextInfo(hostContextInfo);

            parameters.Id = id;

            return ProcessCustomerService.GetCustomerById(parameters);
        }
    }

If you notice here, we have the GetCustomerById and passing the Id in an instance of GetCustomerbyIdParams. Let's see what is the definition of this?

So it is located in our Query projects.

public class GetCustomerByIdParams : FlexiQueryParameterBridge
    {
        public string Id { get; set; }
    }

It is HostContext is being accessed here and we are passing the parameter here. So even if you need the tenant information or user information in the queries you have easily accessible there. Now let us understand the query.

public class GetCustomerById : FlexiQueryBridge<Customer, GetCustomerByIdOutputAPIModel>
    {
        private string _Id;
        private IFlexQueryRepositoryBridge _repoFlex;
        readonly ILogger<GetCustomerById> _logger;
        private GetCustomerByIdParams _params;
        IMapper _mapper;
        IReadDbConnectionProviderBridge _connectionProvider;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="repoFlex"></param>
        /// <param name="logger"></param>
        public GetCustomerById(IFlexQueryRepositoryBridge repoFlex, ILogger<GetCustomerById> logger, IMapper mapper, IReadDbConnectionProviderBridge connectionProvider)
        {
            _repoFlex = repoFlex;
            _logger = logger;
            _mapper = mapper;
            _connectionProvider = connectionProvider;
        }

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

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

            var result = Build<Customer>().ProjectTo<GetCustomerByIdOutputAPIModel>(_mapper.ConfigurationProvider).FirstOrDefault();

            Trace.WriteLine($"Query GetCustomerById 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>().Where(t => t.Id == _params.Id);
            return query;
        }
    }

    public class GetCustomerByIdParams : FlexiQueryParameterBridge
    {
        public string Id { get; set; }
    }

We will use the IFlexQueryRepositoryBridge and the IReadDbConnectionProviderBridge instead of the IWriteDbConnectionProviderBridge. We are using a ProjectTo is a very handy auto-mapper method that maps the data and queries only those fields mentioned in the output model, which is a happy scenario for the DBA. So, whatever you define in the output API model, it will fetch only those fields from the database. And a lot of time saved in writing long select statements even in LINQ.

So let us see that in action in the next section.