/// <summary> /// 添加 AutoMapper 对象映射组件。 /// </summary> /// <param name="services"></param> /// <param name="setupAction"></param> /// <returns></returns> public static IServiceCollection AddAutoMapper(this IServiceCollection services, Action <AutoMapperOptions> setupAction) { var options = new AutoMapperOptions(); setupAction?.Invoke(options); var mapperConfiguration = new AP.MapperConfiguration(c => { options.Configurators.ForEach(s => s?.Invoke(c)); }); services.AddSingleton(sp => mapperConfiguration.CreateMapper()); services.AddSingleton <IObjectMapper, ObjectMapper>(); return(services); }
/// <summary> /// Use the KickStart extension to configure AutoMapper. /// </summary> /// <param name="configurationBuilder">The configuration builder.</param> /// <param name="configure">The <see langword="delegate"/> to configure AutoMapper options.</param> /// <returns> /// A fluent <see langword="interface"/> to configure KickStart. /// </returns> /// <example>Configure AutoMapper on application startup /// <code><![CDATA[ /// Kick.Start(config => config /// .IncludeAssemblyFor<UserProfile>() /// .UseAutoMapper(c => c /// .Validate() /// .Initialize(map => map.AddGlobalIgnore("SysVersion")) /// ) /// .LogLevel(TraceLevel.Verbose) /// );]]></code> /// </example> public static IConfigurationBuilder UseAutoMapper(this IConfigurationBuilder configurationBuilder, Action <IAutoMapperBuilder> configure) { var options = new AutoMapperOptions(); var service = new AutoMapperStarter(options); if (configure != null) { var builder = new AutoMapperBuilder(options); configure(builder); } configurationBuilder.ExcludeName("AutoMapper"); configurationBuilder.Use(service); return(configurationBuilder); }
/// <summary> /// Initializes a new instance of the <see cref="AutoMapperConvention" /> class. /// </summary> /// <param name="options">The options.</param> public AutoMapperConvention(AutoMapperOptions?options = null) { _options = options ?? new AutoMapperOptions(); }