Gleeman.EffectiveMapper 1.0.0

dotnet add package Gleeman.EffectiveMapper --version 1.0.0
NuGet\Install-Package Gleeman.EffectiveMapper -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="Gleeman.EffectiveMapper" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gleeman.EffectiveMapper --version 1.0.0
#r "nuget: Gleeman.EffectiveMapper, 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 Gleeman.EffectiveMapper as a Cake Addin
#addin nuget:?package=Gleeman.EffectiveMapper&version=1.0.0

// Install Gleeman.EffectiveMapper as a Cake Tool
#tool nuget:?package=Gleeman.EffectiveMapper&version=1.0.0

Gleeman Effective Mapper

dotnet CLI

> dotnet add package Gleeman.EffectiveMapper --version 1.0.0

How to use?

Program.cs

builder.Services.AddEffectiveMapper();

When the entity and dto properties are the same.

// Entity
public class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}
// Dtos
public class GetProductDto
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

public class GetProductsDto
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

public class CreateProductDto
{
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
    private readonly IEffectiveMapper _effectiveMapper;
    private readonly AppDbContext _dbContext;
    public ProductsController(IEffectiveMapper effectiveMapper, AppDbContext dbContext)
    {
        _effectiveMapper = effectiveMapper;
        _dbContext = dbContext;
    }

    [HttpGet]
    public ActionResult<IEnumerable<GetProductsDto>> GetProducts()
    {
        IEnumerable<Product> products = _dbContext.Products.ToList();
        IEnumerable<GetProductsDto> productsDto = _effectiveMapper.Map<GetProductsDto, Product>(products);
        return Ok(productsDto);
    }

    [HttpGet("{id}")]
    public ActionResult<GetProductDto> GetProduct(int id)
    {
        Product product = _dbContext.Products.SingleOrDefault(p => p.Id == id)!;
        if (product != null)
        {
            GetProductDto productDto = _effectiveMapper.Map<GetProductDto, Product>(product);
            return Ok(productDto);
        }
        return NotFound();
    }

    [HttpPost]
    public ActionResult<GetProductDto> CreateProduct(CreateProductDto createProduct)
    {
        if (ModelState.IsValid)
        {
            Product product = _effectiveMapper.Map<Product, CreateProductDto>(createProduct);
            _dbContext.Products.Add(product);
            _dbContext.SaveChanges();
            return Created("", createProduct);
        }
        return BadRequest(ModelState.Select(e => e.Value.Errors).ToList());
    }
}

When the entity and dto properties are different.

public class Product
{
    public int Id { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}
public class GetProductsDto
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public decimal ProductPrice { get; set; }
    public int ProductQuantity { get; set; }
}

public class CreateProductDto
{
    public string ProductName { get; set; }
    public decimal ProductPrice { get; set; }
    public int ProductQuantity { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
    private readonly IEffectiveMapper _effectiveMapper;
    private readonly AppDbContext _dbContext;
    public ProductsController(IEffectiveMapper effectiveMapper, AppDbContext dbContext)
    {
        _effectiveMapper = effectiveMapper;
        _dbContext = dbContext;
    }

    [HttpGet]
    public ActionResult<IEnumerable<GetProductsDto>> GetProducts()
    {
        IEnumerable<Product> products = _dbContext.Products.ToList();
        IEnumerable<GetProductsDto> productsDto = _effectiveMapper.Map<GetProductsDto, Product>(products,p=> new GetProductsDto
        {
            ProductId = p.Id,
            ProductName = p.ProductName,
            ProductPrice = p.Price,
            ProductQuantity = p.Quantity
        });
        return Ok(productsDto);
    }

    [HttpPost]
    public ActionResult<GetProductDto> CreateProduct(CreateProductDto createProduct)
    {
        if (ModelState.IsValid)
        {
            Product product = _effectiveMapper.Map<Product, CreateProductDto>(createProduct,p=> new Product
            {
               ProductName = p.ProductName,
               Price = p.ProductPrice,
               Quantity = p.ProductQuantity
            });
            _dbContext.Products.Add(product);
            _dbContext.SaveChanges();
            return Created("", createProduct);
        }
        return BadRequest(ModelState.Select(e => e.Value.Errors).ToList());
    }
}
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
1.0.0 103 9/21/2023