Base solution for your next web application
Open Closed

Updating DB withouth actually calling update #3019


User avatar
0
bilalhaidar created

Hi, I read in the documentation that whenever a code is withing UOW, there is no need to explicitly calling update method on the repository, as this is handled automatically by the framework.

What are the cases that I need to actually call update methods?

Thanks


7 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Actually there is no need to call update method in a unit of work.

  • User Avatar
    0
    bilalhaidar created

    Thanks. But existing code of ABP Framework does it in more than places, so I got confused.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share an example, I might be wrong about it.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Here you go:

    [AbpAuthorize(AppPermissions.Pages_Administration_Users_Edit)]
            protected virtual async Task UpdateUserAsync(CreateOrUpdateUserInput input)
            {
                Debug.Assert(input.User.Id != null, "input.User.Id should be set.");
    
                var user = await UserManager.FindByIdAsync(input.User.Id.Value);
    
                //Update user properties
                input.User.MapTo(user); //Passwords is not mapped (see mapping configuration)
    
                if (input.SetRandomPassword)
                {
                    input.User.Password = User.CreateRandomPassword();
                }
    
                if (!input.User.Password.IsNullOrEmpty())
                {
                    CheckErrors(await UserManager.ChangePasswordAsync(user, input.User.Password));
                }
    
                CheckErrors(await UserManager.UpdateAsync(user));
    
                //Update roles
                CheckErrors(await UserManager.SetRoles(user, input.AssignedRoleNames));
    
                if (input.SendActivationEmail)
                {
                    user.SetNewEmailConfirmationCode();
                    await _userEmailer.SendEmailActivationLinkAsync(user, input.User.Password);
                }
            }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In this case, it is used to check if there are errors on updating user. And the update method belongs to UserManager class which is not a repository.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    I understand, so if there is no need to check for errors, you simply don't cal l any update method?

    Thanks

  • User Avatar
    0
    alirizaadiyahsi created

    Yes, you are right.