using System.Collections.Generic;
using System.Text.RegularExpressions;
public static void Main()
var configuration = new MapperConfiguration(cfg => {
cfg.AddProfile<StockProfile>();
IMapper mapper = configuration.CreateMapper();
var src = new CreateStockRequestDto("Item A", "", 3);
var dest = mapper.Map<Stock>(src);
Console.WriteLine(JsonConvert.SerializeObject(dest));
public class StockProfile : Profile
CreateMap<CreateStockRequestDto, Stock>()
.ForCtorParam("quantity", opt => opt.MapFrom(src => src.Quantity))
.ForCtorParam("stockId", opt => opt.MapFrom(src => new StockId(Guid.NewGuid().ToString())))
.ForCtorParam("itemName", opt => opt.MapFrom(src => src.Name));
public class CreateStockRequestDto
public CreateStockRequestDto(string name, string locationId, int quantity)
public string Name { get; init; }
public int Quantity { get; set; }
public string? Sku { get; set; }
public string LocationId { get; init; }
public Stock(StockId stockId, string itemName, int quantity, string? sku = null)
if (!Guid.TryParse(stockId.Id, out _))
throw new ArgumentException($"Invalid stock id {stockId.Id}", nameof(stockId));
throw new ArgumentException($"Invalid quantity : {quantity}", nameof(quantity));
if (!IsValidItemName(itemName))
throw new ArgumentException($"Invalid item name {itemName}", nameof(itemName));
public StockId StockId { get; init; }
public string ItemName { get; init; }
public int Quantity { get; init; }
public string? Sku { get; init; }
private static bool IsValidItemName(string itemName)
public StockId(string guid)