Paul Pickins
Paul Pickins

Reputation: 69

Create entity class from existing class using T4 templating

I need to easily create/automate some boilerplate code from a simple class in C#/.NET. I have found the T4 templating engine, but I am hoping someone can perhaps give some guidence on how best to implement and if I am on the right track.

I have classes that all look very similar to this:

using System;
using System.Collections.Generic;

namespace BarryAPI.Api
{
    public partial class Client
    {
        public int Id { get; set; }
        public int OrganId { get; set; }
        public string Title { get; set; } = null!;
        public string FirstName { get; set; } = null!;
        public string LastName { get; set; } = null!;
        public DateTime DateOfBirth { get; set; }
        public string Gender { get; set; } = null!;
        public int AddressId { get; set; }
        public string? Phone { get; set; }
        public string? Mobile { get; set; }
    }
}

And I am trying to (mostly) automate the creation of a matched class that looks like this:

using System.ComponentModel.DataAnnotations.Schema;
using Barry.Domain.Entities.Client.Email;
using Barry.Domain.Entities.Organisation;
using Barry.Infrastructure;

namespace Barry.Domain.Entities.Client;

[Table("Client")]
public class ClientEntity : Entity, IMapFrom<ClientDto>
{
    public const int TitleMaxLength = 50;
    public const int FirstNameMaxLength = 100;
    public const int LastNameMaxLength = 100;
    public const int GenderMaxLength = 20;
    public const int PhoneMaxLength = 20;
    public const int MobileMaxLength = 60;


#nullable disable
    public ClientEntity()
    {
    }
#nullable restore

    public string Title { get; set; } = null!;
    public string FirstName { get; set; } = null!;
    public string LastName { get; set; } = null!;
    public DateTime DateOfBirth { get; set; }
    public string Gender { get; set; } = null!;
    public int AddressId { get; set; }
    public string? Phone { get; set; }
    public string? Mobile { get; set; }
}

I understand I cannot make all the logic decisions in the template (Like MaxLength etc), but I am hoping to automate the bulk of it as there as many classes that need to be converted into entity models.

I have looked into T4, but I am struggling to know if this is the correct choice. Can anyone please shed a little light on this problem and if I am headed down the right path trying to solve it with T4? I've been tasked with writing something up to semi-automate this process (mostly just to automate the typing/copy/paste by hand) and I am not sure if a simple console app to read the file and output what's required would be more efficient? Would it?

If you think T4 is the go, pointing towards a similar case tutorial/docs that you know of would be a huge help.

Thank you.

Upvotes: 0

Views: 292

Answers (1)

Murr
Murr

Reputation: 9

T4 templates and execution subsystem not designed to EDIT any of existing files - T4 used to generate whole file and replace existing one.

You can put any logic you want in T4-template or in side DLL's used by the template. Any, including any kind of parsing existed C#/VB/F#&etc files. Just pay attention to complexity of T4-template - it difficult to support when pieces of text mixed with complex code.

Upvotes: 0

Related Questions