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

INSERT : Plugin Validation

BasicCRUD Insert Query with Bridge

PreviousINSERT : Attribute ValidationNextINSERT : Mapper

Last updated 4 years ago

Now let us look at another type of validation. That is the plug-in validation.

The plug-in validation happens in the ProcessServices. You see a ProcessCustomerServices, it creates a sequence with the data packet and the data packet contains the data that was passed as parameter from the controller. In the controller, we are passing the parameter that contains the ContextInfo and the InputAPIModel. Now the first validation that we will look at is the validation which are more complex in scenarios. We can have more database interactions or we have something we want to validate something like the aadhar number. So this is one validation that is not started with number, if you see it cause a packets method do not start with number.

public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
    {
        #region "Validations"
        //Validations goes here
        /// <summary>
        /// 
        /// </summary>
        public async Task DoesNotStartWithNumber()
        {

            //If any validation fails, uncomment and use the below line of code to add error to the packet

            if (Regex.IsMatch(this.InputParams.Model.Name, @"^\d+"))
                this.AddError("Name", $"Name {this.InputParams.Model.Name} cannot start with numbers");


            await Task.CompletedTask; //If you have any await in the validation, remove this line
        }

        #endregion

    }

Next validation, I am checking if the name that was provided by the model is unique, if any duplicate name is found. We are adding the message in the data packets error.

public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
    {
        #region "Validations"
        //Validations goes here
        /// <summary>
        /// 
        /// </summary>
        public async Task IsUnique()
        {

            //If any validation fails, uncomment and use the below line of code to add error to the packet
            //this.AddError("key", "ErrorMessage");

            _connectionProvider.ConfigureDbConnectionString(this.InputParams.HostContextInfo);
            _repo.InitializeConnection(_connectionProvider);

            var custId = _repo.FindAll<Customer>().Where(c => c.Name == this.InputParams.Model.Name).Select(s => s.Id).FirstOrDefault();
            if (custId != null)
                this.AddError("Name", $"Duplicate {this.InputParams.Model.Name} Customer found");

            await Task.CompletedTask; //If you have any await in the validation, remove this line
        }

        #endregion

    }

And refer to the repo instance in the constructor of your data packet as shown below:

public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
    {

        readonly ILogger<AddCustomerDataPacket> _logger;
        IFlexRepositoryBridge _repo;
        IWriteDbConnectionProviderBridge _connectionProvider;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public AddCustomerDataPacket(ILogger<AddCustomerDataPacket> logger, IFlexRepositoryBridge repo, IWriteDbConnectionProviderBridge connectionProvider)
        {
            _logger = logger;
            _repo = repo;
            _connectionProvider = connectionProvider;
        }

        #region "Properties
        //Models and other properties goes here

        public AddCustomerParams InputParams { get; set; }

        #endregion
    }