コード例 #1
0
        public bool UpdateUser(UserModle user)
        {
            ValidateUser(user);

            var commandType = CommandType.StoredProcedure;

            try
            {
                var spUpdateUser = "******";
                var parameters   = ConvertUserToParameter(user);
                _tableContext.ExecuteNonQuery(spUpdateUser, commandType, parameters);

                var spUpdateUserAddresses = "UpdateUserAddresses";
                var id = (Guid)user.Id;
                foreach (var address in user.Addresses)
                {
                    var addresses = ConvertAddress(id, address);
                    _tableContext.ExecuteNonQuery(spUpdateUserAddresses, commandType, addresses);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(true);
        }
コード例 #2
0
        private SqlParameter[] ConvertUserToParameter(UserModle user)
        {
            var parameters = new List <SqlParameter>();

            if (user.Id != null)
            {
                parameters.Add(CreateParameter("id", user.Id));
            }

            parameters.Add(CreateParameter("firstname", user.FirstName));
            parameters.Add(CreateParameter("lastname", user.LastName));
            parameters.Add(CreateParameter("age", user.Age));

            return(parameters.ToArray());
        }
コード例 #3
0
        private void ValidateUser(UserModle user)
        {
            if (string.IsNullOrEmpty(user.FirstName))
            {
                throw new Exception("User must have first name");
            }

            if (string.IsNullOrEmpty(user.LastName))
            {
                throw new Exception("User must have last name");
            }

            if (user.Age < 18 || user.Age > 120)
            {
                throw new Exception("User age must be 18-120");
            }

            if (!user.Addresses.Any())
            {
                throw new Exception("User must have at least one address");
            }
        }