C# Code Generator - Zontroy

C# Code Generator

Generate Code in C#

Csharp is the favorite programming language of Microsoft. There are many areas to use C# such as Web Application Development, Console Applications and Mobile Application Development via Xamarin. C# programming is very enjoying. But did you know about generating some parts of code using a C# Code Generator? Zontroy is a C# Code Generator, and it speeds up the coding process by generating code in a very short time. Let’s look at a basic example. We are going to use the data model provided below, but you can use your own data model if you like.

HR Database Model

First of all, download Zontroy Generator (Free) and install it.

Run Zontroy and create an Empty Zontroy Project.

Enter the information in the form regarding the database and the project. Our data source is local MSSQL and database is a HR database (model is provided above) for this sample. We want to create a CSharp Code Generator project, so select C# as the programming language.

After clicking Create button, the project will be created and ready to work with. At the left side, there is the Entity Window that shows our entities (tables) based on the database. At the right side, there is the Project Window with the project file inside of it.

Next, we are adding a simple template file to generate code. There are three Zontroy template types:

1 – Zontroy Repeating File (zref) creates a code file with code implementation for each entity (table or entity class).

2- Zontroy Single File (zsif) creates only one code file with code implementation for all entities.

3 – Zontroy Inner Repeating File (ziref) creates a folder and a code file with code implementation for each entity (table or entity class).

For our example, we are going to add a zref template to generate a class for each entity in our data model. Right click csharp directory, and click Add File.

Select Zontroy Repeating File (zref) and enter the File Name as below.

With Zontroy, you can write code even in file and folder name areas. This means that Zontroy will take each entity name and tie Controller text with it. File name is going to be like countriesController.cs. For a full list of built-in functions go to docs.

Double click the newly added zref file and paste the code below:

using Microsoft.AspNetCore.Mvc;
using Oprazi.Bll.Services.IServices;
using Oprazi.Dto.DTOs;
using System;
using System.Threading.Tasks;

namespace Oprazi.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class [[[zg-entity...zg-name]]]Controller : ControllerBase
    {
        private readonly I[[[zg-entity...zg-name]]]Service _[[[zg-entity...zg-name]]]Service;

        public [[[zg-entity...zg-name]]]Controller(I[[[zg-entity...zg-name]]]Service [[[zg-entity...zg-name]]]Service)
        {
            _[[[zg-entity...zg-name]]]Service = [[[zg-entity...zg-name]]]Service;
        }

        //[BasicAuthorization]
        [HttpGet("get[[[zg-entity...zg-name]]]")]
        public async Task<IActionResult> Get[[[zg-entity...zg-name]]]()
        {
            try
            {
                var [[[zg-entity...zg-name]]] = await _[[[zg-entity...zg-name]]]Service.Get[[[zg-entity...zg-name]]]();
                if ([[[zg-entity...zg-name]]] == null) 
                    return NotFound();
                else
                    return Ok([[[zg-entity...zg-name]]]);

            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message.ToString());
            }
        }
    }
}

[[[zg-entity…zg-name]]] will be replaced with the name of entity by Zontroy. Code generation is not only about replacing the text, this is just a simple part of source code generation. By using Zontroy, you can do more such as setting the data type of a variable, determining the type of an input (button, text, checkbox, date or any other type) or applying other decision-making mechanisms automatically.

Click Generate button.

That’s all. You can see the generated files in the Project Window.

We have automatically created 7 code file and code lines within them. You can generate much more than this with Zontroy. Hundreds of files and millions of lines…

As the output, the generated countries code file given below:

using Microsoft.AspNetCore.Mvc;
using Oprazi.Bll.Services.IServices;
using Oprazi.Dto.DTOs;
using System;
using System.Threading.Tasks;

namespace Oprazi.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class countriesController : ControllerBase
    {
        private readonly IcountriesService _countriesService;

        public countriesController(IcountriesService countriesService)
        {
            _countriesService = countriesService;
        }

        //[BasicAuthorization]
        [HttpGet("getcountries")]
        public async Task<IActionResult> Getcountries()
        {
            try
            {
                var countries = await _countriesService.Getcountries();
                if (countries == null) 
                    return NotFound();
                else
                    return Ok(countries);

            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message.ToString());
            }
        }
    }
}

Zontroy is a very flexible and effective tool. You can generate all tiers in a multi-tier application, even frontend.

Post a Comment