I have just switched to AutoMapper 4.2.1 and am busily building maps for a project I inherited. I am also playing with mapping from Nullable<T> to T and vice-versa. So far, what I have, is something like:
namespace Sol3.Web
{
public class MappingProfileBase : Profile
{
protected override void Configure()
{
throw new NotImplementedException();
}
}
internal class ConvertEnum<TEnum> : ITypeConverter<int, TEnum> where TEnum : struct, IConvertible
{
public TEnum Convert(ResolutionContext context)
{
if (!typeof(TEnum).IsEnum)
throw new NotSupportedException("TEnum must be an enumerated type");
return (TEnum)context.SourceValue;
}
}
internal class MappingProfileSimpleTypes : MappingProfileBase
{
private readonly bool _disableNullableConversions;
internal MappingProfileSimpleTypes(bool disableNullableConversions = true)
{
_disableNullableConversions = disableNullableConversions;
}
protected override void Configure()
{
CreateMap<double, decimal>().ConvertUsing(src => (decimal)src);
CreateMap<decimal, double>().ConvertUsing(src => (double)src);
if (_disableNullableConversions) return;
CreateMap<bool?, bool>().ConvertUsing(c => c.ToBool());
CreateMap<bool, bool?>().ConvertUsing(c => c as bool?);
CreateMap<int?, int>().ConvertUsing(c => c.ToInt());
CreateMap<int, int?>().ConvertUsing(c => c as int?);
CreateMap<long?, long>().ConvertUsing(c => c.ToLong());
CreateMap<long, long?>().ConvertUsing(c => c as long?);
CreateMap<double?, double>().ConvertUsing(c => c.ToDouble());
CreateMap<double, double?>().ConvertUsing(c => c as double?);
CreateMap<decimal?, decimal>().ConvertUsing(c => c.ToDecimal());
CreateMap<decimal, decimal?>().ConvertUsing(c => c as decimal?);
}
}
internal static class MappingExtensions
{
internal static bool ToBool(this bool? src, bool @default = false) { return src ?? @default; }
internal static int ToInt(this int? src, int @default = int.MinValue) { return src ?? @default; }
internal static long ToLong(this long? src, long @default = long.MinValue) { return src ?? @default; }
internal static double ToDouble(this double? src, double @default = double.MinValue) { return src ?? @default; }
internal static decimal ToDecimal(this decimal? src, decimal @default = decimal.MinValue) { return src ?? @default; }
}
public class WebApiApplication : Sol3WebApiApplication
{
protected override void Application_Start(object sender, EventArgs e)
{
var appSettings = ApplicationSettings.Instance;
appSettings.Name = "Earth Portal";
appSettings.ApplicationType = typeof (WebApiApplication);
appSettings.Color = Color.FromArgb(141, 198, 227);
base.Application_Start(sender, e);
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
AutoMapperConfig.RegisterAutoMapper(Container);
Container.RegisterType<IAccessDeniedResult, AccessDeniedResult>();
Container.RegisterType<IExceptionResult, ExceptionResult>();
}
}
public class AutoMapperConfig : IIsAutoRegistered
{
public static void RegisterAutoMapper(IUnityContainer container)
{
var mapConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfileSimpleTypes());
cfg.AddProfile(new MappingProfileCommon());
cfg.AddProfile(new MappingProfileServiceA());
cfg.AddProfile(new MappingProfileServiceB());
cfg.AddProfile(new MappingProfileServiceC());
});
mapConfig.AssertConfigurationIsValid();
var mapper = mapConfig.CreateMapper();
container.RegisterInstance(mapper);
}
}
}
NOTE: Each class is in it's own CS file. :)
Now, in the MappingProfileSimpleTypes class I would like to add formatting for *any* field found:
DateTime: {0:mmmm dd, yyyy}
Decimal: {0:#,##0.000}
etc
I may also want to add NULL replacement values, etc, etc.
How do I add formatters for *all* DateTime fields? (Yea, long post for a simple answer. Feel free to point out any dumb mistakes as well!)
TIA
Keith