Ejemplo n.º 1
0
        /// <summary>
        /// We cook up a function, SetReputationCalculatorStrategy(), that allows us to insert an appropriate strategy-implementation at runtime.
        /// </summary>
        public static void Main()
        {
            DatingUser someUser = new DatingUser()
            {
                NumberOfAnsweredQuestions = 3,
                UserReputation            = UserReputationEnum.BasicUserReputation
            };

            UserReputationStratategySelector userReputationStratategySelector = new UserReputationStratategySelector();
            IDatingUserReputationStrategy    datingUserReputationStrategy     = userReputationStratategySelector.GetUserReputationStrategy(someUser.UserReputation);

            someUser.SetReputationCalculatorStrategy(datingUserReputationStrategy);
            System.Diagnostics.Debug.WriteLine(someUser.CalculateReputation());

            DatingUser someSuperUser = new DatingUser()
            {
                NumberOfAnsweredQuestions = 3,
                UserReputation            = UserReputationEnum.SuperUserReputation
            };

            datingUserReputationStrategy = userReputationStratategySelector.GetUserReputationStrategy(someSuperUser.UserReputation);
            someSuperUser.SetReputationCalculatorStrategy(datingUserReputationStrategy);
            System.Diagnostics.Debug.WriteLine(someSuperUser.CalculateReputation());

            // What did we achieve? We factored out the strategies, factored out that which changed and
            // thus made our code more maintainable and flexible. We are now easily able to implement further strategies.

            // What else can we do? Well, we could inject the strategy directly into the Dating-user constructor, how about that.

            // We could inject the entire strategy-selector into the dating-user constructor, even. So we might change the users'
            // reputation-enum at runtime, and the class would know to get the proper strategy all by itself.
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Dilemma: we need to calculate a 'user reputation' for our dating service users, based on which type of member they are. How do we go about that in the best, most flexible way? In the below example, the DatingUser-class shifts between types to accomodate different UserReputation values.
        /// </summary>
        private static void Main()
        {
            DatingUser someUser = new DatingUser();

            someUser.NumberOfAnsweredQuestions = 3;
            someUser.userReputation            = UserReputationEnum.BasicUserReputation;

            int calculatedReputation = someUser.CalculateReputation();

            Debug.Assert(calculatedReputation > 0, "user's calculated reputation-score should be > 0");
            Debug.WriteLine($"User's calculated reputation-score: {calculatedReputation}");

            // Well, this works, but What if we need to calculate reputation by an all new reputation type? "SuperUserReputation"?
            // We'd have to go into the DatingUser-class and modify it.
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new DatingUser {
                    UserName  = Input.Email, Email = Input.Email,
                    FirstName = Input.FirstName,
                    LastName  = Input.LastName,
                    BirthDate = Input.BirthDate,
                    Gender    = Input.Gender
                };
                var destinationFolder = Path.Combine(_host.WebRootPath, "Photos");
                var localPath         = Path.Combine(destinationFolder, Input.Photo.FileName);
                var fileStream        = new FileStream(localPath, FileMode.Create);
                await Input.Photo.CopyToAsync(fileStream);

                user.PhotoPaths = Input.Photo.FileName;
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _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 = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }