Table of Contents

Introduction

Since its generator library, and it works on compile time, it will not affect the performance of your application. It will generate the DTOs for you

Example

Library exports the following attributes

  • GenerateDto - defines which DTOs will be generated
  • MapTo - maps the property to another type
  • DtoVisibility - defines which DTOs will have the property
  • DtoIgnore - ignores the property in all DTOs

See also the Attributes page for more information


//...
using TenJames.DtoGenerator;
//...

[GenerateDto(DtoType.All)]
class User 
{
    [MapTo(typeof(string), "src.ToString()")]
    [DtoVisibility(DtoType.AllRead | DtoType.Update)]
    public Guid Id { get; set; }
    
    public string Name { get; set; }
    
    [DtoIgnore]
    public string Password { get; set; }
    
}

Will generate the following DTOs

class UserReadDto
{
    [Required] public string Id { get; set; }
    [Required] public string Name { get; set; }
}

class UserReadDetailDto
{
    [Required] public string Id { get; set; }
    [Required] public string Name { get; set; }
}

class UserCreateDto
{
    [Required] public string Name { get; set; }
}

class UserUpdateDto
{
    [Required] public string Name { get; set; }
}

And also it create a AutoMapper profile to map the DTOs to the original class

public class UserMappingProfile : Profile
{
    public UserMappingProfile()
    {
        CreateMap<User, UserReadDto>()
              .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.ToString()));
        CreateMap<User, UserReadDetailDto>()
              .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.ToString()));
        CreateMap<User, UserCreateDto>();
        CreateMap<User, UserUpdateDto>();
    }
}