HLCores.Utilities.Mapper 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package HLCores.Utilities.Mapper --version 1.0.0                
NuGet\Install-Package HLCores.Utilities.Mapper -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HLCores.Utilities.Mapper" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HLCores.Utilities.Mapper --version 1.0.0                
#r "nuget: HLCores.Utilities.Mapper, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HLCores.Utilities.Mapper as a Cake Addin
#addin nuget:?package=HLCores.Utilities.Mapper&version=1.0.0

// Install HLCores.Utilities.Mapper as a Cake Tool
#tool nuget:?package=HLCores.Utilities.Mapper&version=1.0.0                

HLCores.Utilities.Mapper

HLCores.Utilities.Mapper is a utility that helps you convert data transfer objects quickly.

Features

  • MapOne: Map one TSource data to TDestination.
  • MapOne: Map one object data to TDestination.
  • MapAttributeOne: Map one TSource data to TDestination (include MapperAttribute).
  • MapAttributeOne: Map one object data to TDestination (include MapperAttribute).
  • MapDataJoinOne: Map one TSource data to TDestination (data include typeof join). Required key value string.
  • MapDataJoinOne: Map one object data to TDestination (data include typeof join). Required key value string.
  • Map: Map IQueryable TSource data to IQueryable TDestination.
  • Map: Map objects TSource data to IQueryable TDestination.
  • MapAttribute: Map IQueryable TSource data to IQueryable TDestination (include MapperAttribute).
  • MapAttribute: Map objects data to TDestination (include MapperAttribute).
  • MapDataJoin: Map IQueryable TSource data to IQueryable TDestination (data include typeof join). Required key value string.
  • MapDataJoin: Map objects data to IQueryable TDestination (data include typeof join). Required key value string.
  • MapTo: Map List TSource data to List TDestination.
  • MapTo: Map objects data to List TDestination.
  • MapAttributeTo: Map List TSource data to List TDestination (include MapperAttribute).
  • MapAttributeTo: Map objects data to List TDestination (include MapperAttribute).
  • MapDataJoinTo: Map List TSource data to List TDestination (data include typeof join). Required key value string.
  • MapDataJoinTo: Map objects data to List TDestination (data include typeof join). Required key value string.
  • MapDataReader: Map SqlDataReader data reader to IQueryable TDestination.
  • MapDataReaderTo: Map SqlDataReader data reader to List TDestination.

Usage

1. MapOne:

public async Task<OperationResult> Create(WardDto dto)
{
     Ward data = dto.MapOne<Ward>();
     _context.Ward.Add(data);
     try
     {
         await _context.SaveChangesAsync();
         return new OperationResult { IsSuccess = true };
     }
     catch (Exception ex)
     {
         return new OperationResult { IsSuccess = false, Message = ex.Message };
     }
}

2. Map:

public async Task<WardDto> GetDetail(Guid id)
{
     var data = await _context.Ward
         .Where(x => x.Id == id)
         .Include(x => x.District)
              .ThenInclude(x => x.Province)
         .Map<WardDto>().FirstOrDefaultAsync();

     return data;
}

3. MapDataJoin:

public async Task<PaginationUtility<WardDto>> GetDataPagination(PaginationParam pagination, WardParam param)
{
     var predicate = PredicateBuilder.New<Ward>(x => x.IsDelete == false);
     if (!string.IsNullOrWhiteSpace(param.Keyword))
     {
          string keyword = param.Keyword.ToLower();
          predicate.And(x => x.Title.ToLower().Contains(keyword) || x.Code.ToLower().Contains(keyword));
     }
     if (param.DistrictId is not null)
          predicate.And(x => x.DistrictId == param.DistrictId);

     var data = _context.Ward.Where(predicate)
         .Join(_context.District,
               x => x.DistrictId,
               y => y.Id,
               (x, y) => new { Ward = x, District = y })
          .Join(_context.Province,
               x => x.District.ProvinceId,
               y => y.Id,
               (x, y) => new { x.Ward, x.District, Province = y })
          .MapDataJoin<WardDto>("Ward");
	  // .Select(x => new WardDto
          // {
          //     Id = x.Id,
          //     Code = x.Code,
          //     Title = x.Title,
          //     Status = x.Status,
          //     DistrictId = x.DistrictId,
          //     DistrictCode = x.District.Code,
          //     DistrictTitle = x.District.Title,
          //     ProvinceId = x.District.Province.Id,
          //     ProvinceCode = x.District.Province.Code,
          //     ProvinceTitle = x.District.Province.Title,
          //     UpdateTime = x.UpdateTime ?? x.CreateTime
          // }).OrderByDescending(x => x.Code).AsNoTracking();

     return await PaginationUtility<WardDto>.CreateAsync(data, pagination.PageNumber, pagination.PageSize);
}

4. MapDataReaderTo:

public async Task<PaginationUtility<BaseDto>> GetDataPagination(PaginationParam pagination, BaseParam param)
{
     string cmdText = $"SELECT * FROM {param.Entity} WHERE IsDelete = 0 ";
     if (!string.IsNullOrWhiteSpace(param.Keyword))
     {
          string keyword = param.Keyword.ToLower();
          cmdText += $"AND (LOWER(Title) LIKE '%{keyword}%' OR LOWER(Code) LIKE '%{keyword}%')";
     }

     using SqlConnection conn = new(SettingsConfigUtility.GetCurrentSettings("ConnectionStrings:DefaultConnection"));
     await conn.OpenAsync();

     SqlCommand cmd = GetCommand(cmdText, conn);

     SqlDataReader reader = await cmd.ExecuteReaderAsync(CommandBehavior.Default);

     List<BaseDto> results = new();

     if (reader is not null)
     {
          results = reader.MapDataReaderTo<BaseDto>();
     }

     await cmd.DisposeAsync();

     return PaginationUtility<BaseDto>.Create(results, pagination.PageNumber, pagination.PageSize);
}

5. MapAttribute:

- Model:
public partial class Position
{
    [Key]
    public Guid Id { get; set; }
    [Required]
    [StringLength(10)]
    public string Code { get; set; }
    [Required]
    [StringLength(100)]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }
    public bool? Status { get; set; }
    public bool? IsDelete { get; set; }
    public Guid CreateBy { get; set; }
    [Column(TypeName = "datetime")]
    public DateTime CreateTime { get; set; }
    public Guid? UpdateBy { get; set; }
    [Column(TypeName = "datetime")]
    public DateTime? UpdateTime { get; set; }
    public Guid DepartmentId { get; set; }

    public string MapperTestA { get; set; }
    [Mapper("MapperB")]
    public string MapperTestB { get; set; }
}

- Dto:
public class PositionDto
{
    public Guid Id { get; set; }
    public string Code { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public bool? Status { get; set; }
    public bool? IsDelete { get; set; }
    public Guid CreateBy { get; set; }
    public DateTime CreateTime { get; set; }
    public Guid? UpdateBy { get; set; }
    public DateTime? UpdateTime { get; set; }
    public Guid DepartmentId { get; set; }
    public string DepartmentCode { get; set; }
    public string DepartmentTitle { get; set; }

    public List<Guid> RoleIds { get; set; }
    public List<Guid> FunctionIds { get; set; }

    [Mapper("MapperTestA")]
    public string MapperA { get; set; }
    public string MapperB { get; set; }
}

----------------------------------------------------------------------------------------------

public async Task<PaginationUtility<PositionDto>> GetDataPagination(PaginationParam pagination, string keyword)
{
    var predicate = PredicateBuilder.New<Position>(x => x.IsDelete == false);
    if (!string.IsNullOrWhiteSpace(keyword))
    {
        keyword = keyword.ToLower();
        predicate.And(x => x.Title.ToLower().Contains(keyword) || x.Code.ToLower().Contains(keyword));
    }

    var data = _context.Position.Where(predicate)
        .MapAttribute<PositionDto>();

    return await PaginationUtility<PositionDto>.CreateAsync(data, pagination.PageNumber, pagination.PageSize);
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 87 12/15/2024
1.0.0 142 8/14/2024