Microsoft.Research.DataOnboarding.Services.UserService.UserServiceProvider.RegisterUser C# (CSharp) Method

RegisterUser() private method

private RegisterUser ( Microsoft.Research.DataOnboarding.DomainModel.User user ) : Microsoft.Research.DataOnboarding.DomainModel.User
user Microsoft.Research.DataOnboarding.DomainModel.User
return Microsoft.Research.DataOnboarding.DomainModel.User
        public User RegisterUser(User user)
        {
            Check.IsNotNull<User>(user, "user");
            Check.IsNotEmptyOrWhiteSpace(user.NameIdentifier, "user.NameIdentifier");

            try
            {
                User existingUser = this.userRepository.GetUserByNameIdentifier(user.NameIdentifier);
                if (null != existingUser)
                {
                    throw new UserAlreadyExistsException()
                    {
                        NameIdentifier = user.NameIdentifier,
                        IdentityProvider = user.IdentityProvider
                    };
                }

                user.CreatedOn = DateTime.UtcNow;
                user.ModifiedOn = DateTime.UtcNow;
                user.IsActive = true;
                user.UserRoles.Add(new UserRole()
                {
                    RoleId = (int)Roles.User
                    
                });
                
                User registeredUser = this.userRepository.AddUser(user);
                unitOfWork.Commit();

                // TODO: v-rajask, The code below  was added to return the roles.
                // Entity framework does not pupoulate the complete graph (it only 
                // populates the keys. Role object on UserRole is empty. This 
                // issue needs investigation and proper fix. 
                Role userRole = new Role()
                {
                    Name = Roles.User.ToString()
                };
                registeredUser.UserRoles.First().Role = userRole;
                return registeredUser;
            }
            catch (UnitOfWorkCommitException uowce)
            {
                throw new UserDataUpdateException("Failed to register user.", uowce);
            }
        }

Usage Example

Ejemplo n.º 1
0
        public void Throw_Exception_When_New_User_Registration_Fails()
        {
            // Prepare
            IUnitOfWork unitOfWork = new Fakes.StubIUnitOfWork();
            IUserRepository userRepository =
                new Fakes.StubIUserRepository()
                {
                    AddUserUser = (user) => { throw new UnitOfWorkCommitException("Some data update issue"); }
                };
            User userToRegister = new User()
            {
                NameIdentifier = "s0Me1De9Tf!Er$tRing",
                FirstName = "SomeFirstName",
                MiddleName = "SomeMiddleName",
                LastName = "SomeLastName",
                IdentityProvider = "Windows Live",
                Organization = "SomeOrganization",
                EmailId = "*****@*****.**"
            };

            // Perform
            IUserService userService = new UserServiceProvider(userRepository, unitOfWork);
            userService.RegisterUser(userToRegister);
        }
All Usage Examples Of Microsoft.Research.DataOnboarding.Services.UserService.UserServiceProvider::RegisterUser