EntityDTOs.tt
1. Update the Input Arguments
Update the paths and namespace names at the top of the text template script according to your project.
var targetPath = Host.ResolvePath("../../KLO128.D3ORM.Sample.Application.Contracts/DTOs/Entities/");
var jsonConfigPath = Host.ResolvePath("EntityDTOs.tt").Replace(".tt", ".json");
DefaultNameSpace = "KLO128.D3ORM.Sample.Application.Contracts.DTOs.Entities";
Input arguments are defined in first 3 lines in this case and are divided by an empty line.
- targetPath is the path of the directory where the generated DTO objects will be created or overwritten.
- jsonConfigPath is the path of the generation configuration file (EntityDTOs.json).
- DefaultNameSpace is the name of the namespace that will be used for each DTO class. It should correspond with the target folder path.
Modify EntityDTOs.json
- Entity: Name of the entity you want to configure. The asterix "" means any entity name (wild mask). Specify the wild mask rule only once and place it at the bottom of the configuration file. **
- DtoName: The name of DTO class to be created. The asterix "" is replaced by original entity name. Beware of using ambiguous DTO names. **
- ExcludeProps: The secret or nonpublic properties that should not be included in the target DTO class.
The following example shows the configuration for User entity. We may want to include hidden properties like UserRoles in some cases of service results, so that is why the special class ZUserDTO is generated. Notice, that UserClaims and UserRoles are included only to ZUserDTO, so we do not want to create 2 DTO objects. So, the last rule with the asterix "" will not generate another UserClaimDTO or UserRoleDTO, because both already exist..., but the User entity is generated as ZUserDTO and also UserDTO. **
[
{
"Entity": "User",
"DtoName": "ZUserDTO",
"ExcludeProps": [ "PasswordHash", "AccessFailedCount", "GuidexpirationDate", "LockoutEnabled", "LockoutEndDateUtc", "RegistrationGuid", "SecurityStamp", "TwoFactorEnabled" ],
"AddProps": [] // [PrimitiveDataType, PropName, PrimitiveDataType, PropName2,...]
},
{
"Entity": "UserClaim",
"DtoName": "UserClaimDTO",
"ExcludeProps": [ "PasswordHash", "AccessFailedCount", "GuidexpirationDate", "LockoutEnabled", "LockoutEndDateUtc", "RegistrationGuid", "SecurityStamp", "TwoFactorEnabled" ],
"AddProps": []
},
{
"Entity": "UserRole",
"DtoName": "UserRoleDTO",
"ExcludeProps": [ "PasswordHash", "AccessFailedCount", "GuidexpirationDate", "LockoutEnabled", "LockoutEndDateUtc", "RegistrationGuid", "SecurityStamp", "TwoFactorEnabled" ],
"AddProps": []
},
{
"Entity": "*",
"DtoName": "*DTO",
"ExcludeProps": [ "Email*", "Password*", "PhoneNumber*", "AccessFailedCount", "ExternalLogin", "GuidexpirationDate", "LockoutEnabled", "LockoutEndDateUtc", "RegistrationGuid", "SecurityStamp", "TwoFactorEnabled", "UserName", "UserClaims", "UserRoles", "UserLogins" ],
"AddProps": []
}
]
3. Run the Text Template Script
Run the text template to generate DTO classes for entities.
Example
public class User
{
[Key]
public int UserId { get; set; }
public string Email { get; set; } = null!;
public bool EmailConfirmed { get; set; }
public string? PasswordHash { get; set; }
public string? SecurityStamp { get; set; }
public string? PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTime? LockoutEndDateUtc { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public string UserName { get; set; } = null!;
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
public string Gender { get; set; } = null!;
public DateTime DateOfBirth { get; set; }
public DateTime RegistrationDate { get; set; }
public bool ExternalLogin { get; set; }
public string? RegistrationGuid { get; set; }
public DateTime? GuidexpirationDate { get; set; }
private ICollection<TournamentPlayerStat>? tournamentPlayerStats;
public ICollection<TournamentPlayerStat> TournamentPlayerStats
{
get
{
if (tournamentPlayerStats == null)
{
tournamentPlayerStats = new List<TournamentPlayerStat>();
}
return tournamentPlayerStats;
}
set
{
tournamentPlayerStats = value;
}
}
private ICollection<UserClaim>? userClaims;
public ICollection<UserClaim> UserClaims
{
get
{
if (userClaims == null)
{
userClaims = new List<UserClaim>();
}
return userClaims;
}
set
{
userClaims = value;
}
}
private ICollection<UserLogin>? userLogins;
public ICollection<UserLogin> UserLogins
{
get
{
if (userLogins == null)
{
userLogins = new List<UserLogin>();
}
return userLogins;
}
set
{
userLogins = value;
}
}
private ICollection<UserRole>? userRoles;
public ICollection<UserRole> UserRoles
{
get
{
if (userRoles == null)
{
userRoles = new List<UserRole>();
}
return userRoles;
}
set
{
userRoles = value;
}
}
}
public class UserDTO
{
public int UserId { get; set; }
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
public string Gender { get; set; } = null!;
public DateTime DateOfBirth { get; set; }
public DateTime RegistrationDate { get; set; }
private ICollection<TournamentPlayerStatDTO>? tournamentPlayerStats;
public ICollection<TournamentPlayerStatDTO> TournamentPlayerStats
{
get
{
if (tournamentPlayerStats == null)
{
tournamentPlayerStats = new List<TournamentPlayerStatDTO>();
}
return tournamentPlayerStats;
}
set
{
tournamentPlayerStats = value;
}
}
}
public class ZUserDTO
{
public int UserId { get; set; }
public string Email { get; set; } = null!;
public bool EmailConfirmed { get; set; }
public string? PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public string UserName { get; set; } = null!;
public string FirstName { get; set; } = null!;
public string LastName { get; set; } = null!;
public string Gender { get; set; } = null!;
public DateTime DateOfBirth { get; set; }
public DateTime RegistrationDate { get; set; }
public bool ExternalLogin { get; set; }
private ICollection<UserClaimDTO>? userClaims;
public ICollection<UserClaimDTO> UserClaims
{
get
{
if (userClaims == null)
{
userClaims = new List<UserClaimDTO>();
}
return userClaims;
}
set
{
userClaims = value;
}
}
private ICollection<UserRoleDTO>? userRoles;
public ICollection<UserRoleDTO> UserRoles
{
get
{
if (userRoles == null)
{
userRoles = new List<UserRoleDTO>();
}
return userRoles;
}
set
{
userRoles = value;
}
}
}