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 IEnumerable <SqlDataRecord> ConvertToSqlDataRecord(IEnumerable <Movie> movies) { var sqlDataRecord = new SqlDataRecord(s_sqlMetaDataCreateMovies); foreach (var movie in movies) { sqlDataRecord.SetString(0, movie.Title); sqlDataRecord.SetString(1, GenreParser.ToString(movie.Genre)); sqlDataRecord.SetInt32(2, movie.Year); sqlDataRecord.SetString(3, movie.ImageUrl); yield return(sqlDataRecord); } }
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); }
public static DbCommand CreateCommandForGetMoviesByGenre(DbConnection dbConnection, Genre genre) { var dbCommand = dbConnection.CreateCommand(); dbCommand.CommandType = CommandType.StoredProcedure; dbCommand.CommandText = "dbo.GetMoviesByGenre"; AddReturnValueParameter(dbCommand); AddCommandParameter(dbCommand, "@Genre", ParameterDirection.Input, DbType.String, GenreParser.ToString(genre)); return(dbCommand); }