Walkthrough Generated Code Insert
BasicCRUD Insert Query with Bridge
Last updated
BasicCRUD Insert Query with Bridge
Last updated
Let us walk through the generated code for the insert operation.
The first thing that we will look at is the input API model. We have an AddCustomerInputAPIModel generated.
This is where the developer will write the model and structure the model depending on the input they want from the users. The InputAPIModel starting point is the controller action. This is the input from the applications which are consuming the APIs.
Here one thing to notice is that we have some AddCustomerParams. Will explain that later.
Here we have something called FlexHostContextInfoBridge. This carries the user information across the application.
Now, lets look at the controller Constructor.
We have something called IFlexHostHttpContextAccessorBridge. What it does is, if you go to the endpoint you can find the ContextAccessorBridge. This implements FlexHostHttpContextAcessorBridge. This is a class that is used whenever the HTTP context is loaded. With this, you can write your logic to find the UserId, HostId, ContextId..etc and any other information which you need to get through the HTTP context. It can be other implementation in case of some gRPC implementation or any other source other than asp.net core http end point when its used as an input, so that's why it is made highly decoupled. In this particular case, we will be using the IHttpContextAcessor in that we can fetch any of the context information available to us.
As you can see, we can have the hostname, user ID, username. It can be extended in the Bridge, to add any more fields that are needed. It can be some other header information or it can be anything that comes from the context. So that details are all fetched from The Constructor automatically when you have configured in your DI and then these are passed to a context info. Similarly, we have the context info also. As you will see, we have given four fields. Any more required fields can be added.
The field names should be the same at both the places so that mapping happens automatically and if there is any difference, you can always configure this mapper for any changes. This is already given by default, and if you only need by different names and fields in the context and info then we will need this.
We will dedicate a full section on context info and context accessor, which we will see later. Here, it is being accessed in the context info and it is passed to the AddParams. As we can see the AddCustomerParams, it is a part of our IProcessCustomerService, where it contains the AddCustomerInputAPIModel. So the model is being accessed, that already contains the context info for you inside the Context Params. If we go to the ProcessService, it defines the ContextInfo for us. The context accessories is mapped to the context info and it is passed to the ProcessCustomerService.AddCustomer.
Let's see what the 'Add Customer' does. In the 'AddCustomer', it takes the Param, creates a data packet which fills the data packet with the Param and executes the Flexi flow. It goes through each plugin one by one as defined in the sequence and executes the validation. We will understand more about validation later.
As we saw, it validates all the plugins. If any error is there, it will return the customer with an error. So the controller receives the command result with the error. If all are not successful, then it creates a command and we generate the primary key.
As you can see in the video, the primary key generator is configured in the DI. so usually do that thing like we have that no integer ID from the database.
We don't recommend Integer ID from database as people usually do. Instead, a sequencer ID, which is good as In 64, and that helps microservices and distributed database without compromising on the performance.
That one gives the generation. If you need any ID or some other logic, you can always customize this. We will get back to that in a separate section for customizing the primary key.
We create the ID there itself while giving the command so that the message can be traced throughout the bus and whether it's being processed not processed. It is possible when we generate it as shown. It can be done at a later stage also but is it preferred in this manner. Then the model of the command needs to be filled with the Param's model and the host context info with the Param's context info, This Context Info will be carried across the application, So wherever you need a user ID to be fetched like which is the current logged in user or any other host info, it will be available throughout the application. This is very useful when you are doing a multi-tenant application so you can switch from a single tenant to a multi-tenant and all from a multi-tenant to a single tenant without changing any code with just by configuration changes.
The command result is then given back control of the Controller and that is being passed back to the user or the application that is calling the API.
So once the process has happened here at customer command. We are talking about creating the process command. We go back to the process command. We are doing a 'bus.Send'.
If you are doing a non-eventually and you don't want the eventual consistency to happen through the bus, we have another option for non-eventual consistency. It can be chosen in the feature diagram. It will generate the code to persist the data in the database. we will see that code in a while.
Moving on, this message command is passed on the bus. That is being handled by the Command Handler. The Command Handler receives the message command in which all the data is filled by the process services. Also, do remember this 'IMessageHandlerContext', which is specific to the Nservice Bus.
After that, any processing happens, we need this context so that it is maintained transactional and you can utilize the NService Bus and other diagrams. Well, again here we are creating a packet and running the sequence of the plug-in. Here, there is only one plug-in. The reason we have created a data packet in the plugin principle is that your bus is decoupled. It can be Bus Omega or any other bus also whenever we Implement.
This is a very standard code we can easily replace that with any other bus code and this code is specific to Nservice Bus. There are few parameters we have to pass it to the Nservice Bus. The method signature will change from bus to bus, but your business validations or the business logic do not change of what is written in the plug-in.
What happens is, it calls the plugin and if the bus is changed, the bus specific code will be written and it will call the same plug-in. So whatever you've written for your business won't go away.
Now let's look into the plug-in. The command is passed to the plug-in through the data packet. Here we are maintaining a connection provider. We will have a detailed section on connection providers later.
Let's just assume we need to create an instance of a connection provider for the repository to run. The model is fetched from the 'DomainModel.AddCustomer' and the 'repo.InsertOrUpdate' happens for the model and the 'SaveAsync'.
Now when we see 'GetDomainModel', the 'FlexHost.GetDomainModel' is essential because we have some client and the industry implementation.
It will automatically get your client implementation for the domain model.
Let us see the 'Add Customer'. The 'AddCustomer' receives the 'AddCustomerCommand', and that command carries the model and that is mapped to The Domain model.
We will discuss the 'AddCustomerDomainModel' when we see that in action and 'created by' and 'last Modified by' is accessed from the context info user ID.
So this is all about the insert operation flow that is happening in the code. In the next video we will look at the query flow.