public void Test3()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();
            var mapper = provider.GetMapper <S2, T1>();

            var s2 = new S2()
            {
                A = null
            };
            var t2 = mapper.Convert(s2);

            Assert.IsType <T1>(t2);
            Assert.Equal(0, t2.A);

            s2 = new S2()
            {
                A = 10
            };
            t2 = mapper.Convert(s2);
            Assert.IsType <T1>(t2);
            Assert.Equal(10, t2.A);

            var mapper2 = provider.GetMapper <T1, S2>();
            var t1      = new T1()
            {
                A = 10
            };
            var s3 = mapper2.Convert(t1);

            Assert.Equal(10, s3.A);
        }
        public async Task <ActionResult> ListAll()
        {
            ApplicationUser user = await GetUserAsync();

            List <Project> result = _projectService.ListAll(user.Id);
            var            mapper = _mapper.GetMapper <Project, ProjectDto>();

            return(Json(result.Select(mapper.MapToDto).ToList()));
        }
Beispiel #3
0
        public async Task <ActionResult> ListAll()
        {
            List <MaterialCategory> result = _MaterialCategoryService.ListAll();
            var mapper = _mapper.GetMapper <MaterialCategory, MaterialCategoryDto>();

            return(Json(result.Select(mapper.MapToDto).ToList()));
        }
        public void Test5()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();
            var mapper = provider.GetMapper <S4, T4>();

            var s4 = new S4();
            var t4 = mapper.Convert(s4);

            Assert.Null(t4.T1);

            s4 = new S4()
            {
                S1 = new S1()
                {
                    A = 10
                }
            };
            t4 = mapper.Convert(s4);
            Assert.NotNull(t4.T1);
            Assert.Equal(10, t4.T1.A);
        }
        public async Task <ActionResult> ListAll()
        {
            List <TBEFrame> result = _TBEFrameService.GetAll();
            var             mapper = _mapper.GetMapper <TBEFrame, TBEFrameDto>();

            return(Json(result.Select(mapper.MapToDto).ToList()));
        }
        public void Test8()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper(options =>
            {
                options.AddConvert <S1, T1_2>((m, s, t) =>
                {
                    t.B = t.A + 1;
                });
                // 基类转换 ,在父类的转换会作用于子类
                options.AddConvert <S1, T1>((m, s, t) =>
                {
                    t.A = t.A + 2;
                });
            });

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();

            var m  = provider.GetMapper <S1, T1_2>();
            var t1 = m.Convert(new S1()
            {
                A = 10
            });

            Assert.Equal(11, t1.B);
            // 注意先后关系
            Assert.Equal(12, t1.A);
        }
        public async Task <ActionResult> ListAll()
        {
            List <TBEMaterial> result = _tbeMaterialService.GetAll();
            var mapper = _mapper.GetMapper <TBEMaterial, TBEMaterialDto>();

            return(Json(result.Select(mapper.MapToDto).ToList()));
        }
        public async Task <ActionResult> ListAll()
        {
            List <TBEHeatCorrectionFactor> result = _TBEHeatCorrectionFactorService.GetAll();
            var mapper = _mapper.GetMapper <TBEHeatCorrectionFactor, TBEHeatCorrectionFactorDto>();

            return(Json(result.Select(mapper.MapToDto).ToList()));
        }
        public void Test4()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();
            var mapper = provider.GetMapper <S3, T3>();

            var s3 = new S3()
            {
                A = 1,
                B = 1,
                C = 1
            };
            var t3 = mapper.Convert(s3);

            Assert.IsType <T3>(t3);
            Assert.Equal(1, t3.A);
            Assert.Equal(1, t3.B.Value);
            Assert.Equal(1, t3.C.Value);

            s3 = new S3()
            {
                A = null,
                B = 300,
                C = null
            };
            t3 = mapper.Convert(s3);
            Assert.Equal(0, t3.A);
            Assert.Null(t3.B);
            Assert.Null(t3.C);
        }
Beispiel #10
0
        public static Action <TSource, TTarget> DefineCopyTo <TSource, TTarget>(this IMapperProvider provider, Expression <Func <TSource, object> > excludeProperties = null)
            where TSource : class
            where TTarget : class
        {
            var mapper = provider.GetMapper <TSource, TTarget>();

            return(mapper.DefineCopyTo(excludeProperties));
        }
        public MessageMapper(IMapperProvider mappingProvider)
        {
            if (mappingProvider == null)
            {
                throw new ArgumentNullException(nameof(mappingProvider));
            }

            _autoMapper = mappingProvider.GetMapper();
        }
        public void Test6()
        {
            Dictionary <string, int> dic = new Dictionary <string, int>()
            {
                { "A", 10 },
                { "B", 300 }
            };
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();
            var mapper = provider.GetMapper <Dictionary <string, int>, Dictionary <string, byte> >();
            var t      = mapper.Convert(dic);

            Assert.Equal(10, t["A"]);
            Assert.Equal(0, t["B"]);

            Dictionary <S1, S1> dic2 = new Dictionary <S1, S1>()
            {
                { new S1 {
                      A = 10
                  }, new S1 {
                      A = 20
                  } },
                { new S1 {
                      A = 15
                  }, new S1 {
                      A = 15
                  } }
            };

            var mapper2 = provider.GetMapper <Dictionary <S1, S1>, Dictionary <S2, T1_2> >();
            var t2      = mapper2.Convert(dic2);

            Assert.Equal(10, t2.Keys.ToList()[0].A);
            Assert.Equal(15, t2.Keys.ToList()[1].A);
            Assert.Equal(15, t2.Values.ToList()[1].A);
        }
        public void Test7()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();

            // array ==> list
            var m1 = provider.GetMapper <string[], List <string> >();
            var t1 = m1.Convert(new string[] { "A", "B" });

            Assert.Equal(2, t1.Count);
            Assert.Equal("A", t1[0]);
            Assert.Equal("B", t1[1]);

            // array ==> array
            var m2 = provider.GetMapper <decimal[], byte[]>();
            var t2 = m2.Convert(new decimal[] { 1, 2, 300 });

            Assert.Equal(3, t2.Length);
            Assert.Equal(1, t2[0]);
            Assert.Equal(2, t2[1]);
            //越界
            Assert.Equal(0, t2[2]);

            // 如果目标是Object,取原类型
            var m3 = provider.GetMapper <List <S1>, List <object> >();
            var t3 = m3.Convert(new List <S1>()
            {
                new S1 {
                    A = 10
                }
            });

            Assert.IsType <S1>(t3[0]);
            Assert.Equal(10, (t3[0] as S1).A);
        }
Beispiel #14
0
        /// <summary>
        /// Called when [get callback asynchronous].
        /// </summary>
        /// <param name="returnUrl">The return URL.</param>
        /// <param name="remoteError">The remote error.</param>
        /// <returns>The <see cref="IActionResult"/>.</returns>
        public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (remoteError != null)
            {
                ErrorMessage = $"Error from external provider: {remoteError}";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            // Sign in the user with this external login provider if the user already has a login.
            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider);
                return(LocalRedirect(returnUrl));
            }

            if (result.IsLockedOut)
            {
                return(RedirectToPage("./Lockout"));
            }

            // If the user does not have an account, then ask the user to create an account.
            ReturnUrl     = returnUrl;
            LoginProvider = info.LoginProvider;
            Input         = _mapperProvider.GetMapper <ExternalLoginInfo, ExternalLoginViewModel>().Map(info);

            return(Page());
        }
        public async Task <ActionResult> ListAll()
        {
            List <NonTrasparentBuildingElemet> result = new List <NonTrasparentBuildingElemet>();
            ApplicationUser user = await GetUserAsync();

            Project project = user.Projects.FirstOrDefault();            //TODO take passed project id when there is support for more then one project

            if (project != null)
            {
                result = _nonTrasparentBuildingElemetService.ListAll(project.Id);
            }
            if (result == null || result.Count == 0)
            {
                return(Json(null));
            }
            var mapper = _mapper.GetMapper <NonTrasparentBuildingElemet, NonTrasparentBuildingElemetDto>();
            var mapperDtoNonTransparentBulidingElement = _mapper.GetMapper <NonTrasparentBuildingElemet, NonTrasparentBuildingElemetDto>();
            var mapperDtoMaterialThickness             = _mapper.GetMapper <MaterialThickness, MaterialThicknessDto>();
            var mapperDtoMaterial = _mapper.GetMapper <Material, MaterialDto>();

            List <NonTrasparentBuildingElemetDto> responce = result.Select(mapperDtoNonTransparentBulidingElement.MapToDto).ToList();

            for (int i = 0; i < responce.Count; i++)
            {
                if (result[i].MaterialsUsed != null && result[i].MaterialsUsed.Count > 0)
                {
                    responce[i].MaterialsUsed = new List <MaterialThicknessDto>();

                    foreach (var item in result[i].MaterialsUsed)
                    {
                        MaterialThicknessDto tmp = mapperDtoMaterialThickness.MapToDto(item);
                        tmp.Material = mapperDtoMaterial.MapToDto(item.Material);
                        responce[i].MaterialsUsed.Add(tmp);
                    }
                }
            }

            return(Json(responce));
        }
        public void Test1()
        {
            IServiceCollection sc = new ServiceCollection()
                                    .AddLightweightMapper();

            IServiceProvider sp       = sc.BuildServiceProvider();
            IMapperProvider  provider = sp.GetRequiredService <IMapperProvider>();
            var mapper = provider.GetMapper <S1, T1>();

            var s1 = new S1()
            {
                A = 10
            };
            var t2 = mapper.Convert(s1);

            Assert.IsType <T1>(t2);
            Assert.Equal(10, t2.A);
        }
Beispiel #17
0
        /// <summary>
        /// Called when [post asynchronous].
        /// </summary>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns>The <see cref="IActionResult"/>.</returns>
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user   = _mapperProvider.GetMapper <RegistrationViewModel, User>().Map(Input);
            var result = await _userManager.CreateAsync(user, Input.Password);

            if (!result.Succeeded)
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }

                return(Page());
            }

            _logger.LogInformation("User created a new account with password.");

            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.Page(
                "/Account/ConfirmEmail",
                pageHandler: null,
                values: new { userId = user.Id, code },
                protocol: Request.Scheme);

            // TODO: Copy of this code exists in ExternalLogin.cshtml.cs. Please extract it.
            await _emailSender.SendEmailAsync(
                Input.Email,
                "Confirm your email",
                $"Please confirm your account by clicking <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>here</a>.");

            return(LocalRedirect(returnUrl));
        }
Beispiel #18
0
        public static void CopyTo <TSource, TTarget>(this IMapperProvider provider, TSource sourceObj, TTarget targetObj)
        {
            var mapper = provider.GetMapper <TSource, TTarget>();

            mapper.CopyTo(sourceObj, targetObj);
        }
Beispiel #19
0
        public static List <TTarget> ConvertList <TSource, TTarget>(this IMapperProvider provider, List <TSource> sourceObj)
        {
            var mapper = provider.GetMapper <List <TSource>, List <TTarget> >();

            return(mapper.Convert(sourceObj));
        }
Beispiel #20
0
        public static TTarget Convert <TSource, TTarget>(this IMapperProvider provider, TSource sourceObj)
        {
            var mapper = provider.GetMapper <TSource, TTarget>();

            return(mapper.Convert(sourceObj));
        }