public async IAsyncEnumerable <Movie> GetAllMoviesAsyncStreaming() { var dbConnection = CreateDbConnection(); DbCommand dbCommand = null; DbDataReader dbDataReader = null; try { await dbConnection.OpenAsync().ConfigureAwait(false); dbCommand = CommandFactoryMovies.CreateCommandForGetAllMovies(dbConnection); dbDataReader = await dbCommand.ExecuteReaderAsync().ConfigureAwait(false); var movie = new Movie(); while (await dbDataReader.ReadAsync().ConfigureAwait(false)) { movie.Title = (string)dbDataReader[0]; movie.Genre = GenreParser.Parse((string)dbDataReader[1]); movie.Year = (int)dbDataReader[2]; movie.ImageUrl = (string)dbDataReader[3]; yield return(movie); } } finally { dbDataReader?.Dispose(); dbCommand?.Dispose(); dbConnection.Dispose(); } }
private static Movie CreateRandomMovie(string title) { var shuffledYears = Randomizer.ShuffleArray(s_validYears); var shuffledGenres = Randomizer.ShuffleArray(s_validGenres); return(new Movie( title: title, genre: GenreParser.Parse(shuffledGenres[0]), year: shuffledYears[0], imageUrl: "http://www." + Randomizer.GetRandomAciiStringNoPunctuations(20) + ".com")); }
private static async Task <IEnumerable <Movie> > MapToMovies(DbDataReader dbDataReader) { var movies = new List <Movie>(); while (await dbDataReader.ReadAsync()) { movies.Add(new Movie( title: (string)dbDataReader[0], genre: GenreParser.Parse((string)dbDataReader[1]), year: (int)dbDataReader[2], imageUrl: (string)dbDataReader[3])); } return(movies); }