/// <summary> /// Creates a new instance of a TestHelper class. /// </summary> public TestHelper() { var config = new TypeAdapterConfig(); var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("Pims")); // TODO: Use Singleton pattern to speed up initialization of this. var serializerOptions = Options.Create(new JsonSerializerOptions()); var registers = assemblies.Select(assembly => assembly.GetTypes() .Where(x => typeof(IRegister).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) && x.GetTypeInfo().IsClass&& !x.GetTypeInfo().IsAbstract)) .SelectMany(registerTypes => registerTypes.Select(registerType => registerType.GetConstructor(Type.EmptyTypes) == null ? (IRegister)Activator.CreateInstance(registerType, new[] { serializerOptions }) : (IRegister)Activator.CreateInstance(registerType))).ToList(); config.Apply(registers); config.Default.IgnoreNonMapped(true); config.Default.IgnoreNullValues(true); config.AllowImplicitDestinationInheritance = true; config.AllowImplicitSourceInheritance = true; config.Default.UseDestinationValue(member => member.SetterModifier == AccessModifier.None && member.Type.IsGenericType && member.Type.GetGenericTypeDefinition() == typeof(ICollection <>)); Services.AddSingleton <IntIdentityGenerator>(); Services.AddSingleton(config); Services.AddSingleton <IMapper, ServiceMapper>(); }
public static IServiceCollection AddMappings( this IServiceCollection services, Assembly assembly) { var config = new TypeAdapterConfig(); services.AddSingleton <TypeAdapterConfig>(config); assembly .GetExportedTypes() .Where(t => t.Namespace.Contains("Mappings")) .Where(t => t.GetInterface(nameof(IRegister)) != null) .ToList() .ForEach( t => config.Apply( Activator.CreateInstance(t, false) as IRegister ) ); return(services); }