Skip to content

Latest commit

 

History

History
143 lines (123 loc) · 3.35 KB

Gen-Dto.md

File metadata and controls

143 lines (123 loc) · 3.35 KB

Usage

//netstandard2.0 +
[AutoDto(Type entityType ,params string[] ignoredProperties)]

//C#11(NET7+) support generic attribute
[AutoDto<T>(params string[] ignoredProperties)]

Your Entitys

namespace Biwen.AutoClassGen.TestConsole.Entitys
{
    public class User : Info
    {
        /// <summary>
        /// Id
        /// </summary>
        public string Id { get; set; } = null!;
        /// <summary>
        /// first name
        /// </summary>
        public string FirstName { get; set; } = null!;
        /// <summary>
        /// last name
        /// </summary>
        public string LastName { get; set; } = null!;
        /// <summary>
        /// age
        /// </summary>
        public int? Age { get; set; }
        /// <summary>
        /// fullname
        /// </summary>
        public string? FullName => $"{FirstName} {LastName}";
    }
    public abstract class Info : Other
    {
        /// <summary>
        /// email
        /// </summary>
        public string? Email { get; set; }
    }
    public abstract class Other
    {
        /// <summary>
        /// remark
        /// </summary>
        public string? Remark { get; set; }
    }
}

Define Dto Partial Class & mark AutoDtoAttribute

using Biwen.AutoClassGen.TestConsole.Entitys;
namespace Biwen.AutoClassGen.TestConsole.Dtos
{
    [AutoDto(typeof(User), nameof(User.Id), "TestCol")]
    public partial class UserDto
    {
    }

    /// <summary>
    /// C#11(NET7+) support generic attribute
    /// </summary>
    [AutoDto<User>(nameof(User.Email), "TestCol")]
    public partial class User3Dto
    {
    }
}

Enjoy

  • 请注意MapperTo扩展函数仅针对属性相同的情况,如果需要映射不同的属性名或特殊映射规则,请使用AutoMapper或者Maspter等第三方库
// <auto-generated />
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Biwen.AutoClassGen.TestConsole.Entitys;

#pragma warning disable
namespace Biwen.AutoClassGen.TestConsole.Dtos
{
    public partial class UserDto
    {
        /// <inheritdoc cref = "User.FirstName"/>
        public string FirstName { get; set; }
        /// <inheritdoc cref = "User.LastName"/>
        public string LastName { get; set; }
        /// <inheritdoc cref = "User.Age"/>
        public int? Age { get; set; }
        /// <inheritdoc cref = "User.FullName"/>
        public string? FullName { get; set; }
        /// <inheritdoc cref = "Info.Email"/>
        public string? Email { get; set; }
        /// <inheritdoc cref = "Other.Remark"/>
        public string? Remark { get; set; }
    }
}

namespace Biwen.AutoClassGen.TestConsole.Entitys
{
    using Biwen.AutoClassGen.TestConsole.Dtos;
    public static partial class UserToUserDtoExtentions
    {
        /// <summary>
        /// mapper to UserDto
        /// </summary>
        /// <returns></returns>
        public static UserDto MapperToDto(this User model)
        {
            return new UserDto()
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                Age = model.Age,
                FullName = model.FullName,
                Email = model.Email,
                Remark = model.Remark,
            };
        }
    }
}
#pragma warning restore

Report Diagnostic Code

  • GEN041: Repeat Mark [AutoDto] mutil times