Cybtans Code Generator

Download (Windows) Buy License

General Information

Cybtans Code Generator can really be a time savior to you, it is a code generation tool for WebAPI and Entity Framework. It reads your tables and views from your database in SQL Server or SQlite and generates the code for a complete WebAPI project. The generated code follows a layered S.O.L.I.D architecture composed of data models, services, controllers and controllers DTO models. Even more it allows you to customize the generation templates so you can get the code the way you want it. You can also apply a template when a condition on tables or columns is meet or to remove it from the generation process. The tool not only will generates WebAPI projects but also Android and Angular 2 generation modules are in development.

Requirements

In order to run this tool you must have installed .NET Framework 4.5 at least. The application is redistributed in a .zip file so no installment is required just run the CodeGenerator.exe file.

Quick Start

Welcome Screen

Working with Cybtans Code Generator is very easy, just follow the following steps.

1- Lunch the application and creates a new application as shown bellow

Welcome Screen

Fill in the information as the application's name , output directory and database connection settings. The Output Directory is only required if you don't specify the generation module Output Directory as that will be explained later.

2- Load your models from your database by clicking on icon Load Models .

3- Select the tables you want to include

Welcome Screen

4- Select the generation module (in this version only WebAPI is enable) and fill in the parameters like the namespace and the Output Directory. Cybtans Code Generation was designed from the beginning to generate code for several types of projects in the same application. Therefore you can chose to generate the code for all the selected modules into the application Output Directory or to generate the code in each module Output Directory.

5- Generate the code by clicking on the button generate code buttton.

Naming Conventions

Cybtans Code Generator allows you to name your models and properties using either Pascal(Microsoft, C#) style or Camel(java, js, python) case naming conventions. Just do right click on the models treeview and select a naming convention.

Welcome Screen

The results are shown below:

Welcome Screen

You can also open the Model Manager Window and specify parameters like the name or description for specific models and properties. In addition which is required for generating views you can specify the primary key for a model, this is a Entity Framework requirement and you will get a warning after loading the model from the database as is shown below.

Warning Message

Then when you modify your database and after the models are loaded again you can chose to keep your naming convention by selecting Yes on the dialog box that will appear. Although you will be required to select the naming convention again for the new models and columns, but your previous changes will be untouched.

Customize your Code

After some point, you would like to make modifications to the code generated, so you'd want those modifications to be keep the next time you generate the code. Well happy for you Cybtans Code Generator support that. The tool was designed using a Step-Template system you can modify. In short the Code Generator for each generation module defines a series of Steps and each step has an associated default template. Even more a step can have a collection of conditional templates that are applied when its condition or predicate evaluates True.

You can edit the default templates for a module step or add conditional templates using the Template Manager Window. For opening the windows just do click on the Templates button located in the generation module.

An example for editing the default template for the Model step is shown below

An example shown next is for adding conditional templates to the Model-Simple-Property step. In this case we specified a condition on columns but we did not defined a templates (left the TEMPLATE field empty) so this means the columns that satisfy the condition will not be included in the generated code. This type of conditional template can be usefully when you already have defined in your code a hierarchy or base class for your models, so the generated models don't have to define the properties you already defined for the base model class.

In addition you will be assisted with auto-complete support when setting the conditions for the step

Although conditional templates can be used to apply custom templates to models, it's more easy and efficient to use "Custom Model Templates" To access the "Custom Model Templates" click on the corresponding tab in the Custom Template Manager windows.

Custom Model Templates

Condition Expressions

The conditions or predicates for the conditional templates are boolean expression which can contains logical or relational operators. The supported operators are:

  • < :Column predicate, (Size < 30)
  • > :Column predicate, (Position > 1)
  • = :Model predicate, (Name = 'Film')
  • != :Model predicate, (Name != 'Film')
  • and : Column predicate, (Model.IsManyToMany and Model.Name like '%Rel')
  • or : Model predicate, (IsManyToMany or Name like '%Rel')
  • like : Model predicate (Name like '%state%')

The like operator will match any string that contains the specified pattern. For example if the patterns are:

  • '%state' :will match any string that ends in 'state'
  • 'state%' :will match any string that begins in 'state'
  • '%state%' :will match any string that contains 'state'

Running the code

Cybtans Code Generator uses Enterlib.NET as support for the generated architecture as you can see in the resultant code. For example your business services extend from IEntityService<T> a base class Enterlib.NET defines for leverage basic operations on Business Services. The same case happens with your controllers, repositories and UnitOfWork. The approach of using Enterlib.NET reduce the code repetition and let you extends the parts you need.

Then in order to run the code you must install Enterlib.NET packages. Those are available from the Nuget Package Manager Console or the Package Browser Windows. You just type enterlib and install Enterlib.WebApi and Enterlib.EF

Using the Package Manager Console type the following:

  • PM> Install-Package Enterlib.WebApi
  • PM> Install-Package Enterlib.EF

Integrating Enterlib.NET DI Engine into WebAPi

Also in order to run the code you must integrate Enterlib.NET's Dependency Injection Engine into WebAPI. But you don't worry about that because Cybtans Code Generator has already generated the code for you. It's located in the App_Start\WebApiConfig.cs. The most important statements are shown below:


    var container = DataServices.Configure(config, "AdventureWorks.Models", "AdventureWorks.Models.Responses", 
                 new Assembly[] { Assembly.GetExecutingAssembly() });              

    //Register a model validation action filter
    config.Filters.Add(new ValidateModelActionFilterAttribute());

And that's all. the last line of code also adds the model validation filter to the global filters collection.

If you want to integrates the DI engine yourself, below is the code for the DataService.Configure method. As you can see first the DependencyResolverContext is created, then optionally you can add ITypeResolver instances ,for example EntityControllerResolver is a ITypeResolver that will creates on-the-fly a ApiController class if you didn't defined one for the request. Then the configuration.DependencyResolver is set to WebApiDependencyResolver which is an adapter for the DependencyResolverContext.


    public static class DataServices
    {
        public static DependencyResolverContext Configure(HttpConfiguration configuration,
            string modelsNamespace,
            string serviceResponseNamespace,
            Assembly[] assemblies)
        {

            //Create the DependencyResolver
            DependencyResolverContext context = new DependencyResolverContext(assemblies);

            //Register the service controller resolver
            context.TypeResolvers.Add(new EntityControllerResolver(LifeType.Scope));

            //Set the WebApi DependencyResolver
            configuration.DependencyResolver = new WebApiDependencyResolver(context);

            //Set the controller selector
            configuration.Services.Replace(typeof(IHttpControllerSelector),
                new EntityApiControllerSelector(configuration,
                    modelsNamespace,
                    serviceResponseNamespace,
                    assemblies,
                    configuration.Services.GetHttpControllerSelector()));

            //Set the Exception Handler to handle ValidationException
            configuration.Services.Replace(typeof(IExceptionHandler), new ValidationExceptionHandler());

            return context;
        }
    }