10x Programming: Automating the creation of POCO(POJO) objects

It is possible to create the code for POCO (Plain Old C# Object) objects 10x to 1000x faster by automating the creation of this code.

With the 10x Programming blog series I want to teach you some new skills in increasing the speed you can create code. There are lots of methods that can allow you to create years worth of code in days! Firstly we will start with POCO objects then future posts will explore more techniques and methods. These will allow you to automate the creation of even more of your code.

Even with simple POCO objects we can automate the creation of the code for multiple languages(e.g. C#, Java, Typescript, XML etc) and keep them in sync. Even for the simplest use cases, SilverModel will save you a lot of time.

Now let’s see how to get starting by creating POCO’s with SilverModel.

Example: Modern Web Application

Most modern apps rely on serialisation to move data from one location to another e.g. we need to move the data from a web client written in TypeScript to a web server written in C# using JSON over HTTP.

To do this we need to create a lot of code to manage the objects on both sides of the software and manage the transport of the data over the HTTP.

Also, we need to ensure that the serialisation code is kept in sync with all the different code models otherwise we could break the application!. SilverModel will do this for you.

Converting a model to POCO objects.

I will be using the Todo application as an example. The code for this is available on Github. https://github.com/SilverdawnSoftware/Todo-dotnet-core

Firstly let’s start with the model.

 

This model allows tasks to be assigned to a user. So how do we convert this model into POCO(POJO) objects and automate the creation of the code into C# and TypeScript(Javascript)?

Firstly we need a sample of the code we want to automate. If we add a more complex model with 100’s of classes, we would only need sample code for one of the classes. Then when we automate the code, all the rest of the classes will have the code generated for them.

So the sample code for Task in C# looks like this.

public class Task
{
    public int TaskId  { get; set; }
    public string Name  { get; set; }
    public DateTime StartedDate  { get; set; }
    public List Tasks {get;set;}
}

Now we need to convert this code into a code generator. Firstly create a new code Generator on SilverModel or use an existing one.

Then create a blank template and add the sample code to it. So your template looks the same as above. The templates are written using T4 and C#.

Next, you need to add the references the template needs for the rest of the template code to work.

@using SilverDawn.SilverModel.UML.Extensions; 
@using SilverDawn.SilverModel.UML.Interfaces.Classes; 
@using SilverDawn.SilverModel.UML.Classes;

public class Task
{
    public int TaskId  { get; set; }
    public string Name  { get; set; }
    public DateTime StartedDate  { get; set; }
}

Now you want to get the class name from the model. In the template, the main object is called Model. The template will be passed  UML elements as required to the Model object. In this case, the “Class” which is a UML Classifier will be passed to the template. UML Interfaces are also Classifiers.

We can use the following code to access the Class name @Model.Classifier.Name

@using SilverDawn.SilverModel.UML.Extensions; 
@using SilverDawn.SilverModel.UML.Interfaces.Classes; 
@using SilverDawn.SilverModel.UML.Classes;

public class @Model.Classifier.Name
{
    public int TaskId  { get; set; }
    public string Name  { get; set; }
    public DateTime StartedDate  { get; set; }  
}

So our template will now insert the class name as required.

Next, we need to generate the properties for the C# object.

We will need to loop through all the UML Class’s attributes to create the properties in the code. You can do this with a simple loop in T4. E.g. @foreach (var property in Model.Classifier.Attributes())

Well, we are getting close now. The last thing is to turn the UML attributes into code which we can do with the following code

<text>public @property.Type.TypeName(“C#”)</text> @property.Name <text> { get; set; } </text>

So what does this line do?

@property.Type.TypeName(“C#”) asks SilverModel for the correct type for C#. Some languages the type maybe double, number or float etc. SilverModel can provide the correct type for any language. See DataTypes for more information.

@property.Name gets the name of the property. Now let’s add this to the template.

@using SilverDawn.SilverModel.UML.Extensions;
@using SilverDawn.SilverModel.UML.Interfaces.Classes;
@using SilverDawn.SilverModel.UML.Classes;

using System;
using System.Collections.Generic;
using System.Linq;


namespace @Model.Diagram.QualifiedName
{
    public class @Model.Classifier.Name
    {
        @foreach (var property in Model.Classifier.Attributes())
        {         
        <text>public @property.Type.TypeName("C#")</text> @property.Name <text> { get; set; } </text>
        }
    }
}

Also since documentation is a valuable part of any project let’s add this in too.

@using SilverDawn.SilverModel.UML.Extensions;
@using SilverDawn.SilverModel.UML.Interfaces.Classes;
@using SilverDawn.SilverModel.UML.Classes;


using System;
using System.Collections.Generic;
using System.Linq;

namespace @Model.Diagram.QualifiedName
{
    public class @Model.Classifier.Name
    {
        @foreach (var property in Model.Classifier.Attributes())
        {         
        <text>/// <summary>@property.Description</summary></text>   
        <text>/// <remarks>@Raw(property.Notes)</remarks></text>   
        <text>public @property.Type.TypeName("C#")</text> @property.Name <text> { get; set; } </text>
        }
    }
}

When run this template produces for the following code for the task class.

<code">using System;
using System.Collections.Generic;
using System.Linq;

namespace ToDo.POCO
{
    public class Task
    {
        /// <summary> Unique identifier for a task </summary>                   
        public int TaskId  { get; set; }        
        
        /// <summary> The name of the task to be done </summary>             
        public string Name  { get; set; }       
        
        /// <summary> When the task needs to be completed by </summary>          
        public DateTime DueDate  { get; set; }      
        
        /// <summary> When the task was started </summary>           
        public DateTime StartedDate  { get; set; }      
        
        /// <summary> The status of the task e.g, Inprogress, Completed </summary>           
        public int Status  { get; set; }        
        
        /// <summary> The data the task was completed </summary>     
        public DateTime CompletedDate  { get; set; }        
    }
}


 

Summary

Now the template is complete you can use SilverModel to generate POCO code for all the classes in your Model. A single Model could create code for 100’s of  C#, Javascript, TypeScript, Java objects and keep all of this code in sync.

Hopefully, this blog has shown you some ways that code automation can help you and how simple it is to get started.

Please check back on this blog for more tips in the future for speeding up your development with code automation.

Any questions please ask below in the comments section.