예제 #1
0
        public static IEnumerable <TAppModel> Get <TAppModel>()
        {
            var realmType = MapperConfiguration.GetAllTypeMaps().Single(x => x.DestinationType == typeof(TAppModel)).SourceType;

            var mapper = MapperConfiguration.CreateMapper();

            return(OfflineRealm.All(realmType.Name).ToList().Select(mapper.Map <TAppModel>));
        }
예제 #2
0
 public void SetCurrentMap(Type sourceType)
 {
     if (EnableMapping)
     {
         CurrentMap =
             mapperConfiguration.GetAllTypeMaps().FirstOrDefault(x => x.SourceType == sourceType);
     }
 }
예제 #3
0
        public void WhenSecondCallTo_GetUnmappedPropertyNames_ShouldReturnBoo()
        {
            //Arrange
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <Foo, Foo2>().ReverseMap();
            });
            var typeMap = config.GetAllTypeMaps()
                          .First(x => x.SourceType == typeof(Foo2) && x.DestinationType == typeof(Foo));
            //Act
            var unmappedPropertyNames = typeMap.GetUnmappedPropertyNames();

            //Assert
            unmappedPropertyNames[0].ShouldBe("Boo");
        }
예제 #4
0
            public void GetUnmappedPropertyNames_ShouldReturnBoo()
            {
                //Arrange
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap(typeof(Foo), typeof(Foo2));
                });
                var typeMap = config.GetAllTypeMaps()
                              .First(x => x.SourceType == typeof(Foo) && x.DestinationType == typeof(Foo2));
                //Act
                var unmappedPropertyNames = typeMap.GetUnmappedPropertyNames();

                //Assert
                unmappedPropertyNames[0].ShouldEqual("Boo");
            }
예제 #5
0
    public static IMapperConfigurationExpression CreateWrapMap(
        this IMapperConfigurationExpression cfg,
        Func <Type, bool> needWrap, Type wrapperGenericType,
        Type converterGenericType)
    {
        var mapperConfiguration =
            new MapperConfiguration((MapperConfigurationExpression)cfg);
        var types = Assembly.GetExecutingAssembly().GetTypes();

        foreach (var dstType in types.Where(needWrap))
        {
            var srcType = mapperConfiguration.GetAllTypeMaps()
                          .Single(map => map.DestinationType == dstType).SourceType;
            var wrapperDstType = wrapperGenericType.MakeGenericType(dstType);
            var converterType  = converterGenericType.MakeGenericType(srcType, dstType);
            cfg.CreateMap(srcType, wrapperDstType)
            .ConvertUsing(converterType);
        }
        return(cfg);
    }
        /// <summary>
        /// Try to add all mappings in an Automapper profile.
        /// </summary>
        /// <typeparam name="TProfile">The Automapper profile.</typeparam>
        public void TryAddMappingsFromAutoMapperProfile <TProfile>()
            where TProfile : Profile, new()
        {
            var config = new MapperConfiguration(cfg => {
                cfg.AddProfile <TProfile>();
            });

            var typeMaps = config.GetAllTypeMaps();

            foreach (var typeMap in typeMaps)
            {
                try
                {
                    AddMapping(typeMap.SourceType, typeMap.DestinationType);
                }
                catch (Exception)
                {
                    // TODO: commented because options classes should have an empty constructor, thus a logger cannot be injected.
                    // Log a warning but don't throw back.
                    //_logger.LogWarning($"Could not add mapping from AutoMapper profile '{typeof(TProfile).Name}' for source type '{typeMap.SourceType.Name}' and destination type '{typeMap.DestinationType.Name}'.");
                }
            }
        }
예제 #7
0
 public static void UpdateMappingConfiguration(Action <IMapperConfigurationExpression> configuration)
 {
     MapperConfiguration = new MapperConfiguration(cfg =>
     {
         GetConfigurations(cfg);
         configuration(cfg);
     });
     Mapper          = MapperConfiguration.CreateMapper();
     KindVersionsMap = MapperConfiguration
                       .GetAllTypeMaps()
                       .SelectMany(x => new[] { x.Types.SourceType, x.Types.DestinationType })
                       .Where(x => x.GetCustomAttribute <KubernetesEntityAttribute>() != null)
                       .Select(x =>
     {
         var attr = GetKubernetesEntityAttribute(x);
         return(new { attr.Kind, attr.ApiVersion, Type = x });
     })
                       .GroupBy(x => x.Kind)
                       .ToDictionary(x => x.Key, kindGroup => kindGroup
                                     .GroupBy(x => x.ApiVersion)
                                     .ToDictionary(x => x.Key,
                                                   versionGroup => versionGroup.Select(x => x.Type).Distinct().Single())); // should only be one type for each Kind/Version combination
 }
예제 #8
0
 public void WhenSecondCallTo_GetUnmappedPropertyNames_ShouldReturnBoo()
 {
     //Arrange
     var config = new MapperConfiguration(cfg =>
     {
         cfg.CreateMap<Foo, Foo2>().ReverseMap();
     });
     var typeMap = config.GetAllTypeMaps()
               .First(x => x.SourceType == typeof(Foo2) && x.DestinationType == typeof(Foo));
     //Act
     var unmappedPropertyNames = typeMap.GetUnmappedPropertyNames();
     //Assert
     unmappedPropertyNames[0].ShouldBe("Boo");
 }
 public void GetUnmappedPropertyNames_ShouldReturnBoo()
 {
     //Arrange
     var config = new MapperConfiguration(cfg =>
     {
         cfg.CreateMap(typeof(Foo), typeof(Foo2));
     });
     var typeMap = config.GetAllTypeMaps()
               .First(x => x.SourceType == typeof(Foo) && x.DestinationType == typeof(Foo2));
     //Act
     var unmappedPropertyNames = typeMap.GetUnmappedPropertyNames();
     //Assert
     unmappedPropertyNames[0].ShouldEqual("Boo");
 }