Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DTO auto Gen #4

Open
vipwan opened this issue Nov 4, 2023 · 3 comments
Open

Support DTO auto Gen #4

vipwan opened this issue Nov 4, 2023 · 3 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@vipwan
Copy link
Owner

vipwan commented Nov 4, 2023

nuget add:

dotnet add package Biwen.AutoClassGen.Attributes

Entitys define:

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; }
    }
}

usage :

using Biwen.AutoClassGen.TestConsole.Entitys;
namespace Biwen.AutoClassGen.TestConsole.Dtos
{
    /// <summary>
    /// to be generated
    /// </summary>
    [AutoDto(typeof(User), nameof(User.Id), "TestCol")]
    public partial class UserDto
    {
    }
    /// <summary>
    /// to be generated more than one
    /// </summary>
    [AutoDto(typeof(User), nameof(User.Email))]
    public partial class User2Dto
    {
    }
    /// <summary>
    /// support generic attribute  NET7+(C#11)
    /// </summary>
    [AutoDto<User>(nameof(User.Email), "TestCol")]
    public partial class User3Dto
    {
    }
}

generated :

// <auto-generated />
// author:[email protected] 万雅虎
// issue:https://github.com/vipwan/Biwen.AutoClassGen/issues
// 如果你在使用中遇到问题,请第一时间issue,谢谢!
// This file is generated by Biwen.AutoClassGen.SourceGenerator
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

#pragma warning disable
//generate User-UserDto
namespace Biwen.AutoClassGen.TestConsole.Dtos
{
    using Biwen.AutoClassGen.TestConsole.Entitys;
    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 MapperToUserDto(this User model)
        {
            return new UserDto()
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                Age = model.Age,
                FullName = model.FullName,
                Email = model.Email,
                Remark = model.Remark,
            };
        }
    }
}

//generate User-User2Dto
namespace Biwen.AutoClassGen.TestConsole.Dtos
{
    using Biwen.AutoClassGen.TestConsole.Entitys;

    partial class User2Dto
    {
        /// <inheritdoc cref = "User.Id"/>
        public string Id { get; set; }
        /// <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 = "Other.Remark"/>
        public string? Remark { get; set; }
    }
}

namespace Biwen.AutoClassGen.TestConsole.Entitys
{
    using Biwen.AutoClassGen.TestConsole.Dtos;

    public static partial class UserToUser2DtoExtentions
    {
        /// <summary>
        /// mapper to User2Dto
        /// </summary>
        /// <returns></returns>
        public static User2Dto MapperToUser2Dto(this User model)
        {
            return new User2Dto()
            {
                Id = model.Id,
                FirstName = model.FirstName,
                LastName = model.LastName,
                Age = model.Age,
                FullName = model.FullName,
                Remark = model.Remark,
            };
        }
    }
}
#pragma warning restore
@vipwan vipwan added enhancement New feature or request documentation Improvements or additions to documentation labels Nov 4, 2023
vipwan added a commit that referenced this issue Nov 4, 2023
@vipwan
Copy link
Owner Author

vipwan commented Nov 5, 2023

提供简单的Mapper映射:
auto gen Mapper :

#pragma warning disable
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

vipwan added a commit that referenced this issue Nov 5, 2023
@vipwan
Copy link
Owner Author

vipwan commented Nov 7, 2023

C#11+(NET7+) 支持使用泛型特性标注(support Generic Attribute)

usage : [AutoDto<T>(params string?[])]
    [AutoDto<User>(nameof(User.Email), "TestCol")]
    public partial class User3Dto
    {
    }

@vipwan
Copy link
Owner Author

vipwan commented Nov 12, 2024

提供MapperToPartial 用于自定义Mapper实现

namespace Biwen.AutoClassGen.TestConsole.Entitys
{
    public static partial class UserToUserDtoExtentions
    {
        static partial void MapperToPartial(User from, UserDto to)
        {
            to.FirstName = "重写了FirstName";
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant