/// <summary> /// Scans the specified assembly for classes that implement <see cref="IDIObjectGraphBase{TSource}"/> and /// registers clr type mappings on the schema between that <see cref="DIObjectGraphType{TDIGraph, TSource}"/> /// (constructed from that class and its source type), and the source type. /// Skips classes where the source type is <see cref="object"/>, or where the class is marked with /// the <see cref="DoNotMapClrTypeAttribute"/>, or where another graph type would be automatically mapped /// to the specified type, or where a graph type has already been registered to the specified clr type. /// </summary> public static IGraphQLBuilder AddDIClrTypeMappings(this IGraphQLBuilder builder, Assembly assembly) { var typesAlreadyMapped = new HashSet <Type>( assembly.GetDefaultClrTypeMappings() .Where(x => x.GraphType.IsOutputType()) .Select(x => x.ClrType)); var types = assembly.GetTypes() .Where(x => x.IsClass && !x.IsAbstract && typeof(IDIObjectGraphBase).IsAssignableFrom(x)) .Select <Type, (Type DIGraphType, Type?SourceType)>(x => { var iface = x.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDIObjectGraphBase <>)); return(x, iface?.GetGenericArguments()[0]); }) .Where(x => x.SourceType != null && x.SourceType != typeof(object) && !x.DIGraphType.IsDefined(typeof(DoNotMapClrTypeAttribute)) && !typesAlreadyMapped.Contains(x.SourceType)) .Select <(Type DIGraphType, Type?SourceType), (Type ClrType, Type GraphType)>(x => (x.SourceType !, typeof(DIObjectGraphType <,>).MakeGenericType(x.DIGraphType, x.SourceType !))) .ToList(); if (types.Count == 0) { return(builder); } builder.ConfigureSchema(schema => { var existingMappings = new HashSet <Type>(schema.TypeMappings.Where(x => x.graphType.IsOutputType()).Select(x => x.clrType)); foreach (var type in types) { if (!existingMappings.Contains(type.ClrType)) { schema.RegisterTypeMapping(type.ClrType, type.GraphType); } } }); return(builder); }