Ejemplo n.º 1
0
        public static AddedMapContext AddYuzuMappersFromContainer(this MapperConfigurationExpression cfg, IFactory factory)
        {
            var mappingConfigs = factory.GetAllInstances <YuzuMappingConfig>();
            var config         = factory.GetInstance <IYuzuConfiguration>();
            var mapContext     = new AddedMapContext();

            foreach (var mappingConfig in mappingConfigs)
            {
                foreach (var item in mappingConfig.ManualMaps)
                {
                    var mapper  = factory.GetInstance(item.Mapper) as IYuzuBaseMapper;
                    var generic = mapper.MakeGenericMethod(item);
                    mapContext = generic.Invoke(mapper, new object[] { cfg, item, factory, mapContext, config }) as AddedMapContext;
                }
            }

            return(mapContext);
        }
        public AddedMapContext CreateMap <Dest, TService>(MapperConfigurationExpression cfg, YuzuMapperSettings baseSettings, IFactory factory, AddedMapContext mapContext, IYuzuConfiguration config)
            where TService : class, IYuzuTypeFactory <Dest>
        {
            var settings = baseSettings as YuzuTypeFactoryMapperSettings;

            if (settings != null)
            {
                Func <IYuzuTypeFactory> getFactory = () =>
                {
                    return(factory.GetInstance(typeof(TService)) as TService);
                };

                if (!config.ViewmodelFactories.ContainsKey(settings.Dest))
                {
                    config.ViewmodelFactories.Add(settings.Dest, getFactory);
                }
                config.AddActiveManualMap <TService, Dest>();

                return(mapContext);
            }
            else
            {
                throw new Exception("Mapping settings not of type YuzuTypeFactoryMapperSettings");
            }
        }
Ejemplo n.º 3
0
        public AddedMapContext CreateMap <Source, DestParent, DestChild>(MapperConfigurationExpression cfg, YuzuMapperSettings baseSettings, IFactory factory, AddedMapContext mapContext, IYuzuConfiguration config)
        {
            var settings = baseSettings as YuzuGroupMapperSettings;

            if (settings != null)
            {
                var groupNameWithoutSpaces = settings.GroupName.Replace(" ", "");

                cfg.RecognizePrefixes(groupNameWithoutSpaces);

                mapContext.AddOrGet <Source, DestChild>(cfg);

                var parentMap = mapContext.AddOrGet <Source, DestParent>(cfg);
                parentMap.ForMember(settings.PropertyName, opt => opt.MapFrom(y => y));


                return(mapContext);
            }
            else
            {
                throw new Exception("Mapping settings not of type YuzuGroupMapperSettings");
            }
        }
Ejemplo n.º 4
0
        public AddedMapContext CreateMap <DestMember, Source, Dest, TService>(MapperConfigurationExpression cfg, YuzuMapperSettings baseSettings, IFactory factory, AddedMapContext mapContext, IYuzuConfiguration config)
            where TService : class, IYuzuTypeFactory <DestMember>
        {
            var settings = baseSettings as YuzuPropertyFactoryMapperSettings;

            if (settings != null)
            {
                config.AddActiveManualMap <TService, Dest>(settings.DestPropertyName);

                Func <Source, Dest, object, ResolutionContext, DestMember> mappingFunction = (Source m, Dest v, object o, ResolutionContext context) =>
                {
                    var propertyResolver = factory.GetInstance(typeof(TService)) as TService;
                    var yuzuContext      = contextFactory.From <UmbracoMappingContext>(context.Items);
                    return(propertyResolver.Create(yuzuContext));
                };

                var map = mapContext.AddOrGet <Source, Dest>(cfg);

                map.ForMember(settings.DestPropertyName, opt => opt.MapFrom(mappingFunction));

                return(mapContext);
            }
            else
            {
                throw new Exception("Mapping settings not of type YuzuPropertyFactoryMapperSettings");
            }
        }
Ejemplo n.º 5
0
        public AddedMapContext CreateMap <Source, Dest, Resolver>(MapperConfigurationExpression cfg, YuzuMapperSettings baseSettings, IFactory factory, AddedMapContext mapContext, IYuzuConfiguration config)
            where Resolver : class, IYuzuTypeAfterConvertor <Source, Dest>
        {
            var settings = baseSettings as YuzuTypeAfterMapperSettings;

            if (settings != null)
            {
                config.AddActiveManualMap <Resolver, Dest>();

                var map = mapContext.AddOrGet <Source, Dest>(cfg);

                Action <Source, Dest, ResolutionContext> mappingFunction = (Source source, Dest dest, ResolutionContext context) =>
                {
                    var typeConvertor = factory.GetInstance(typeof(Resolver)) as Resolver;
                    var yuzuContext   = contextFactory.From <UmbracoMappingContext>(context.Items);

                    typeConvertor.Apply(source, dest, yuzuContext);
                };
                map.AfterMap(mappingFunction);

                return(mapContext);
            }
            else
            {
                throw new Exception("Mapping settings not of type YuzuTypeMappingSettings");
            }
        }
Ejemplo n.º 6
0
        public AddedMapContext CreateMap <Source, Dest, SourceMember, DestMember, Resolver>(MapperConfigurationExpression cfg, YuzuMapperSettings baseSettings, IFactory factory, AddedMapContext mapContext, IYuzuConfiguration config)
            where Resolver : class, IYuzuFullPropertyResolver <Source, Dest, SourceMember, DestMember>
        {
            var settings = baseSettings as YuzuFullPropertyMapperSettings;

            if (settings != null)
            {
                if (settings.IgnoreProperty)
                {
                    importConfig.IgnorePropertiesInViewModels.Add(new KeyValuePair <string, string>(typeof(Dest).Name, settings.DestPropertyName));
                }

                if (settings.IgnoreReturnType)
                {
                    importConfig.IgnoreViewmodels.Add(typeof(Type).Name);
                }

                if (!string.IsNullOrEmpty(settings.GroupName))
                {
                    cfg.RecognizePrefixes(settings.GroupName);
                }

                Func <Source, Dest, object, ResolutionContext, DestMember> mappingFunction = (Source m, Dest v, object o, ResolutionContext context) =>
                {
                    var propertyResolver = factory.GetInstance(typeof(Resolver)) as Resolver;
                    var sourceValue      = ((SourceMember)typeof(Source).GetProperty(settings.SourcePropertyName).GetValue(m));
                    var yuzuContext      = contextFactory.From <UmbracoMappingContext>(context.Items);

                    return(propertyResolver.Resolve(m, v, sourceValue, settings.DestPropertyName, yuzuContext));
                };

                var map = mapContext.AddOrGet <Source, Dest>(cfg);

                map.ForMember(settings.DestPropertyName, opt => opt.MapFrom(mappingFunction));

                return(mapContext);
            }
            else
            {
                throw new Exception("Mapping settings not of type YuzuPropertyMappingSettings");
            }
        }
Ejemplo n.º 7
0
        public static void AddProfilesForAttributes(this MapperConfigurationExpression cfg, IEnumerable <Assembly> assembliesToScan, AddedMapContext mapContext, IFactory factory)
        {
            var allTypes = assembliesToScan.Where(a => !a.IsDynamic && a != typeof(NamedProfile).Assembly).SelectMany(a => a.DefinedTypes).ToArray();
            var autoMapAttributeProfile = new NamedProfile(nameof(YuzuMapAttribute));
            var config       = factory.GetInstance <IYuzuConfiguration>();
            var importConfig = factory.GetInstance <IYuzuDeliveryImportConfiguration>();

            foreach (var viewModels in allTypes)
            {
                foreach (var attribute in viewModels.GetCustomAttributes <YuzuMapAttribute>())
                {
                    var cmsModel = config.CMSModels.Where(x => x.Name == attribute.SourceTypeName).FirstOrDefault();
                    if (cmsModel != null && !mapContext.Has(cmsModel, viewModels) && !importConfig.IgnoreUmbracoModelsForAutomap.Contains(cmsModel.Name))
                    {
                        cfg.CreateMap(cmsModel, viewModels);
                    }
                }
            }
        }
        public AddedMapContext CreateMap <Source, Dest, TService>(MapperConfigurationExpression cfg, YuzuMapperSettings baseSettings, IFactory factory, AddedMapContext mapContext, IYuzuConfiguration config)
            where TService : class, IYuzuTypeConvertor <Source, Dest>
        {
            var settings = baseSettings as YuzuTypeConvertorMapperSettings;

            if (settings != null)
            {
                config.AddActiveManualMap <TService, Dest>();

                if (settings.IgnoreReturnType)
                {
                    importConfig.IgnoreViewmodels.Add(typeof(Dest).Name);
                }

                var map = mapContext.AddOrGet <Source, Dest>(cfg);

                Func <Source, Dest, ResolutionContext, Dest> mappingFunction = (Source source, Dest dest, ResolutionContext context) =>
                {
                    var typeConvertor = factory.GetInstance(typeof(TService)) as TService;
                    var yuzuContext   = contextFactory.From <UmbracoMappingContext>(context.Items);

                    return(typeConvertor.Convert(source, yuzuContext));
                };
                map.ConvertUsing(mappingFunction);

                return(mapContext);
            }
            else
            {
                throw new Exception("Mapping settings not of type YuzuTypeMappingSettings");
            }
        }
        public AddedMapContext CreateMap <Source, Dest>(MapperConfigurationExpression cfg, YuzuMapperSettings baseSettings, IFactory factory, AddedMapContext mapContext, IYuzuConfiguration config)
        {
            var settings = baseSettings as YuzuGlobalMapperSettings;

            if (settings != null)
            {
                if (settings.GroupName != null)
                {
                    cfg.RecognizePrefixes(settings.GroupName);
                }

                mapContext.AddOrGet <Source, Dest>(cfg);

                return(mapContext);
            }
            else
            {
                throw new Exception("Mapping settings not of type YuzuGlobalMapperSettings");
            }
        }
Ejemplo n.º 10
0
        public AddedMapContext CreateMap <Source, DestMember, Dest, Resolver>(MapperConfigurationExpression cfg, YuzuMapperSettings baseSettings, IFactory factory, AddedMapContext mapContext, IYuzuConfiguration config)
            where Resolver : class, IYuzuPropertyAfterResolver <Source, DestMember>
        {
            var settings = baseSettings as YuzuPropertyAfterMapperSettings;

            if (settings != null)
            {
                //need a fix here
                //config.AddActiveManualMap<Resolver, Dest>(settings.DestProperty);

                if (!string.IsNullOrEmpty(settings.GroupName))
                {
                    cfg.RecognizePrefixes(settings.GroupName);
                }

                Func <DestMember, DestMember> mappingFunction = (DestMember input) =>
                {
                    var propertyResolver = factory.GetInstance(typeof(Resolver)) as Resolver;
                    return(propertyResolver.Apply(input));
                };

                var map = mapContext.AddOrGet <Source, Dest>(cfg);

                map.ForMember <DestMember>(settings.DestProperty as Expression <Func <Dest, DestMember> >, opt => opt.AddTransform(x => mappingFunction(x)));

                return(mapContext);
            }
            else
            {
                throw new Exception("Mapping settings not of type YuzuPropertyMappingSettings");
            }
        }