/// <summary> /// Adds the track referenced to the playlist Tracks colleciton. It /// optionally adds it to the TrackIs collection too. /// </summary> /// <param name="Playlist">The playlist being updated</param> /// <param name="Track">The Track being added.</param> /// <remarks> /// Only add an AlbumId if to the AlbumIds collection if it's not already there. /// It won't be if the its is a New Album being created, but there will be when /// reloading the persisted data. /// </remarks> private void AddTrackToPlaylist(PlayList Playlist, Track Track) { // Add the Track to the Tracks collection Playlist.Tracks.Add(Track); // Only add a TrackId if to the TrackIds collection if it's not already there. // It won't be if the its is a New Album being created, // but there will be when reloading the persistent data. if (!Playlist.TrackIds.Contains(Track.Id)) Playlist.TrackIds.Add(Track.Id); }
/// <summary> /// Adds a new playlist to the Playlists collection of the domain model. It also /// return the new playlist, fully updated. /// </summary> /// <param name="UnitOfWork">In-Memory Context</param> /// <param name="Playlist">The playlist being added</param> /// <param name="Track">The track associated with the playlist</param> /// <returns>The new playlist.</returns> /// <remarks> /// Only add an AlbumId if to the AlbumIds collection if it's not already there. /// It won't be if the its is a New Album being created, but there will be when /// reloading the persisted data. /// <para> /// Concurrency: /// </para> /// <Para> /// This routine provides a Monitor lock to ensure that only one instance of this /// can modify the Playlists collection at a time. /// It locks from the point which a new Id is generated for the Playlsit being added /// and retains that until the Genre is added to the Genres collection. /// This type of lock is preferred to ensure the generated Id is updated within /// the Playlists collection before any other thread can try and Add a Playlist which /// would require the Id for this one, which woudl be used in the GenerateId /// methods. This is to avoid duplicate Id's being generated. /// </Para> /// <para> /// This is a very course grained way of locking but is acceptible within this /// application as it is a Windows 8 store app, and not subjected to multiple /// users, only the individual threads within the application. The performance /// hit should not be that noticable here. /// </para> /// </remarks> private PlayList AddPlaylist(IUnitOfWork UnitOfWork, PlayList Playlist, Track Track) { lock (_PlaylistLock) { // Concurrency: Lock between getting the Id and performing the update, as the // this new Id won't be availble for checking purposes by another // thread that is looking to add a Playlist. This should avoid the // possiblilties of duplicate Id's // i. Generate PlaylistId, if not one already (will be reloading persisted data). Playlist.Tracks = new List<Track>(); if (Playlist.Id == 0) Playlist.Id = GeneratePlaylistId(UnitOfWork); // ii. Add Track reference to the Playlist AddTrackToPlaylist(Playlist, Track); // iii.Add Playlist to Pl;aylists Collection UnitOfWork.PlayLists.Add(Playlist); } return Playlist; }
/// <summary> /// Updates an existling playlist, but adding the tracks to it. /// </summary> /// <param name="UnitOfWork">In-Memory context</param> /// <param name="Playlist">The playlist being updated</param> /// <param name="Track">The track associated with the playlist</param> /// <returns>The updated playlist.</returns> /// <remarks> /// Only add an AlbumId if to the AlbumIds collection if it's not already there. /// It won't be if the its is a New Album being created, but there will be when /// reloading the persisted data. /// <para> /// Concurrency: /// </para> /// <Para> /// This routine provides a Monitor lock to ensure that only one instance of this /// can modify the Artists collection at a time. /// It locks from the point where the Genre to be updated is retrieved from /// the Artist /// and retains that until the Artist is updated in the Artists collection. /// This type of lock is preferred to ensure that no other updates to the Artist /// are possible while this update occurs. /// </Para> /// <para> /// This is a very course grained way of locking but is acceptible within this /// application as it is a Windows 8 store app, and not subjected to multiple /// users, only the individual threads within the application. The performance /// hit should not be that noticable here. /// </para> /// </remarks> private PlayList UpdatePlaylist(IUnitOfWork UnitOfWork, PlayList Playlist, Track Track) { int idx = 0; lock (_PlaylistLock) { // Get the index of the Playlist idx = UnitOfWork.PlayLists.FindIndex(p => p.Id == Playlist.Id); // Get the Playlist var updatedPlaylist = UnitOfWork.PlayLists[idx]; // If track exists in the playlist retrieved playlist. var tr = updatedPlaylist.Tracks.FirstOrDefault(a => a.Id == Track.Id); if (tr != null) { // The track is found, so replace var trIdx = updatedPlaylist.Tracks.IndexOf(tr); updatedPlaylist.Tracks[trIdx] = Track; } else { // Track dosn't exist in the Playlist.Tracks collection, so just add it. AddTrackToPlaylist(updatedPlaylist, Track); } // Replace the Playlist in the context UnitOfWork.PlayLists[idx] = updatedPlaylist; } // return the updated Genre return UnitOfWork.PlayLists[idx]; //return updatedPlaylist; }
/// <summary> /// Adds or Updates the playlist within the Playlist collection of the domain model. /// It returns a collection of updated playlists, for updating within the Tracks, so /// that the navigation properties are fully updated. /// </summary> /// <param name="UnitOfWork">In-Memory Context</param> /// <param name="Playlists">The Playlists being updated</param> /// <param name="Track">The Track belonging to the playlists</param> /// <returns>Update playlists collection</returns> /// <remarks> /// Only add an AlbumId if to the AlbumIds collection if it's not already there. /// It won't be if the its is a New Album being created, but there will be when /// reloading the persisted data. /// </remarks> private List<PlayList> AddOrUpdatePlaylists(IUnitOfWork UnitOfWork, List<PlayList> Playlists, Track Track) { var updatedPlaylists = new List<PlayList>(); foreach (var p in Playlists) { if (PlaylistExists(UnitOfWork, p.Id)) { var updatedPlaylist = UpdatePlaylist(UnitOfWork, p, Track); updatedPlaylists.Add(updatedPlaylist); } else { var newPlaylist = AddPlaylist(UnitOfWork, p, Track); updatedPlaylists.Add(newPlaylist); } } return updatedPlaylists; }