CSharp · Console
using System;
using AutoMapper;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
MapperConfiguration _config = new MapperConfiguration(config =>
{
config.CreateMap<Notification, NotificationItemDTO>()
.ForMember(
dest => dest.Sent,
opts => opts.MapFrom(src => src.Status == 2)
);
});
IMapper _mapper = _config.CreateMapper();
List<Notification> inputs = new List<Notification>
{
new Notification { Status = 1 },
new Notification { Status = 2 }
};
for (int i = 0; i < inputs.Count; i++)
{
Console.WriteLine($"Test case {i+1}");
var src = inputs[i];
Console.WriteLine($"Input: {JsonConvert.SerializeObject(src)}");
var dest = _mapper.Map<NotificationItemDTO>(src);
Console.WriteLine($"Output: {JsonConvert.SerializeObject(dest)}");
Console.WriteLine("---");
}
}
}
public class NotificationItemDTO
{
public long Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public bool Sent { get; set; }
}
public class Notification
{
public long Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public int Status { get; set; }
}