/// <summary> /// 添加给定的用户到给定的队伍中。 /// </summary> /// <param name="team">队伍句柄。</param> /// <param name="user">用户句柄。</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="TeamNotFoundException"/> /// <exception cref="UserNotFoundException"/> public void AddUserToTeam(TeamHandle team, UserHandle user) { if (team == null) { throw new ArgumentNullException(nameof(team)); } if (user == null) { throw new ArgumentNullException(nameof(user)); } m_factory.WithContext(context => { TeamProfileEntity teamEntity = context.QueryTeamProfileEntity(team.TeamId); if (teamEntity == null) { throw new TeamNotFoundException(team); } UserProfileEntity userEntity = context.QueryUserProfileEntity(user.Username); if (userEntity == null) { throw new UserNotFoundException(user); } teamEntity.Members.Add(userEntity); context.SaveChanges(); }); }
/// <summary> /// 使用给定的队伍信息实体对象 /// </summary> /// <param name="context">数据上下文对象。</param> /// <param name="entity">队伍信息实体对象。</param> /// <param name="isReadOnly">一个值,该值指示当前对象是否为只读。</param> /// <exception cref="ArgumentNullException"/> private TeamDataProvider(UserDataContext context, TeamProfileEntity entity, bool isReadOnly) { m_context = context ?? throw new ArgumentNullException(nameof(context)); m_entity = entity ?? throw new ArgumentNullException(nameof(entity)); m_isReadOnly = isReadOnly; m_disposed = false; }
/// <summary> /// 从给定的队伍信息实体对象创建 TeamHandle 类的新实例。 /// </summary> /// <param name="entity">队伍信息实体对象。</param> /// <returns>从给定的队伍信息实体对象创建的 TeamHandle 对象。</returns> /// <exception cref="ArgumentNullException"/> public static TeamHandle FromTeamEntity(TeamProfileEntity entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } return(new TeamHandle(entity.Id)); }
/// <summary> /// 从数据库中删除给定的队伍信息实体数据。 /// </summary> /// <param name="entity">要删除的队伍信息实体数据。</param> /// <exception cref="ArgumentNullException"/> public void RemoveTeamProfileEntity(TeamProfileEntity entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } TeamProfiles.Remove(entity); SaveChanges(); }
/// <summary> /// 使用指定的 Team ID 查询队伍。 /// </summary> /// <param name="teamId">队伍 ID。</param> /// <returns>查找到的队伍的队伍句柄。若未找到这样的队伍,返回 null。</returns> public TeamHandle QueryTeamById(int teamId) { TeamProfileEntity entity = m_factory.WithContext(context => context.QueryTeamProfileEntity(teamId)); if (entity == null) { return(null); } return(new TeamHandle(teamId)); }
/// <summary> /// 在数据库中创建一个新的队伍实体对象。 /// </summary> /// <returns>新创建的队伍句柄。</returns> public TeamHandle CreateTeam() { TeamProfileEntity entity = new TeamProfileEntity(); m_factory.WithContext(context => { entity = context.AddTeamProfileEntity(entity); context.SaveChanges(); }); return(TeamHandle.FromTeamEntity(entity)); }
/// <summary> /// 将给定的队伍信息实体数据添加至数据库中。 /// </summary> /// <param name="entity">要添加的队伍信息实体数据。</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="InvalidOperationException"/> /// <returns> /// 添加到数据集的实体对象。 /// </returns> /// <remarks> /// 若给定的实体对象已经存在于数据库中,抛出 InvalidOperationException 异常。 /// 若要更新数据库中对应的实体数据,请调用 UpdateTeamProfileEntity 方法。 /// </remarks> public TeamProfileEntity AddTeamProfileEntity(TeamProfileEntity entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } entity = TeamProfiles.Add(entity); SaveChanges(); return(entity); }
/// <summary> /// 将给定的队伍信息实体数据添加至数据库中。 /// </summary> /// <param name="entity">要添加的队伍信息实体数据。</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="InvalidOperationException"/> /// <remarks> /// 若给定的实体对象已经存在于数据库中,抛出 InvalidOperationException 异常。 /// 若要更新数据库中对应的实体数据,请调用 UpdateTeamProfileEntity 方法。 /// </remarks> public void AddTeamProfileEntity(TeamProfileEntity entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (QueryTeamProfileEntity(entity.Name) != null) { throw new InvalidOperationException("给定的队伍信息实体对象已经存在于数据库中。"); } TeamProfiles.Add(entity); SaveChanges(); }
/// <summary> /// 更新数据库中给定的队伍信息实体数据。 /// </summary> /// <param name="entity">要更新的队伍信息实体数据。</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="InvalidOperationException"/> /// <remarks> /// 若给定的实体对象未在数据库中找到,抛出 InvalidOperationException 异常。 /// 若要将给定的实体对象添加至数据库,请调用 AddTeamProfileEntity 方法。 /// </remarks> public void UpdateTeamProfileEntity(TeamProfileEntity entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } TeamProfileEntity target = QueryTeamProfileEntity(entity.Name); if (target == null) { throw new InvalidOperationException("给定的队伍信息实体数据未在数据库中找到。"); } // 更新实体对象数据。 target.ProfileFile = entity.ProfileFile; SaveChanges(); }
/// <summary> /// 从数据库中移除给定的队伍。 /// </summary> /// <param name="team">要移除的队伍句柄。</param> /// <exception cref="ArgumentNullException"/> public void RemoveTeam(TeamHandle team) { if (team == null) { throw new ArgumentNullException(nameof(team)); } m_factory.WithContext(context => { TeamProfileEntity entity = context.QueryTeamProfileEntity(team.TeamId); if (entity == null) { return; } context.RemoveTeamProfileEntity(entity); context.SaveChanges(); }); }
/// <summary> /// 使用给定的队伍句柄和一个指示当前对象是否为只读对象的值创建一个新的 TeamDataProvider。 /// </summary> /// <param name="handle">队伍句柄。</param> /// <param name="isReadonly">一个值,该值指示当前对象是否为只读。</param> /// <returns>绑定到给定队伍句柄的 TeamDataProvider 对象。</returns> /// <exception cref="ArgumentNullException"/> /// <exception cref="TeamNotFoundException"/> public static TeamDataProvider Create(TeamHandle handle, bool isReadonly) { if (handle == null) { throw new ArgumentNullException(nameof(handle)); } // 从数据库中查询队伍数据。 UserDataContext context = new UserDataContextFactory().CreateContext(); TeamProfileEntity entity = context.QueryTeamProfileEntity(handle.TeamId); if (entity == null) { context.Dispose(); throw new TeamNotFoundException(handle); } return(new TeamDataProvider(context, entity, isReadonly)); }
/// <summary> /// 从给定的队伍中移除给定的用户。 /// </summary> /// <param name="team">队伍句柄。</param> /// <param name="user">用户句柄。</param> /// <exception cref="ArgumentNullException"/> /// <exception cref="TeamNotFoundException"/> /// <exception cref="UserNotFoundException"/> public void RemoveUserFromTeam(TeamHandle team, UserHandle user) { if (team == null) { throw new ArgumentNullException(nameof(team)); } if (user == null) { throw new ArgumentNullException(nameof(user)); } m_factory.WithContext(context => { TeamProfileEntity teamEntity = context.QueryTeamProfileEntity(team.TeamId); if (teamEntity == null) { throw new TeamNotFoundException(team); } UserProfileEntity userEntity = context.QueryUserProfileEntity(user.Username); if (userEntity == null) { throw new UserNotFoundException(user); } foreach (UserProfileEntity teamUser in teamEntity.Members) { if (string.Compare(teamUser.Username, userEntity.Username, false) == 0) { teamEntity.Members.Remove(teamUser); break; } } context.SaveChanges(); }); }