SHASHANGKA SHEKHAR'S BLOG

Author, TechWriter

Custom Code-Generator Using ASP.Net Core

Hits: 1279

In this post we will get overview and generate a basic label code using our database table column that help in development.

The main thought behind the sample was to generate the common types of code easily.

The Single page application is built with ASP.Net Core & AngularJS, here’s the execution environment & API service is in .Net Core to generate code. In front-end have used AngularJS.

Content Focused:

  • Connect to database server
  • Get all database list then
  • Get all Table Column by selecting a particular table
  • Choose column then
  • Generate code, that’s it.

Let’s dive into the code, first let’s get started with back-end work. We have connect our database server using connection string.

Let’s Work with Back-End, Connect to Database:

Lets’ create an API Controller to get all our operation done. In our controller class let’s put a connection string to get connected with our databases.

Get All Databases:

Let’s create a method to get all database list from database server. Below SQL query to get all databases.

Here’s is the Get method to list all our databases in server.

Get all Table Column: Here’s we are getting all tables from selected database.

Choose Column: Below SQL query to get all table columns.

Here’s is the Get method to list all selected table column in server.

Now let’s overview the server code that is producing the generated code based on our database column.

In our code-generation mechanism the .txt template use to representing the real structure. Below folder we have all the formatted template to present the real structure in generated code.

Let’s expand the generator. Here’s the list that we are  generated by this app

  1. API Controller
  2. ViewModel
  3. Stored Procedure
  4. AngularJS Controller
  5. HTML-Form

API-Generator: Below code method will generate the API Controller using a formatted text file.

API Controller Format:

In this portion we have read the text as it is till the end line then replace those #tag property using c# dynamically. This is the main trick that used in others code generation.

ViewModel-Generator:  For specific data model we need to generate some view model, below method will generate those data models.

Stored Procedure-Generator

Insert-SP: This is a minimum label  sp generate query template for insert operation, in application we need to modify those with our own additional logic.

Get-SP: This is a minimum label  sp generate query template for get operation, in application we need to modify those with our own additional logic.

Update-SP: This is a minimum label  sp generate query template for update operation, in application we need to modify those with our own additional logic.

Delete-SP: This is a minimum label  sp generate query template for delete operation, in application we need to modify those with our own additional logic.

Let’s Work with Fron-End, AngularJS Controller-Generator: In our frontend we also used the same mechanism that is used to generate API Controller.

HTML-Generator (Form): Using below method we have generated Html form that embedded with angular tag.

Generated Code: We have used multiple tab to represent all the generated code. Below is our HTML section that shows how we are presenting generated code.

We have use pre-formated & HTML Example Element tag in html to present the generated code.

Here’s the code generation frontend function that is interacting with API’s, then getting the generated content which is representing by writing the DOM in Html page.

OutPut: As you can see the final out put in the below screen.

Source Code: I’ve uploaded the full source code to download/clone @github, Hope this will help 🙂

Post a comment