/// <summary> /// Gets all DAuthors objects from the database. /// </summary> /// <param name="command">Command containing the gett all query.</param> /// <returns>All dauthors from the database.</returns> private IEnumerable <DAuthor> ToDAuthorList(IDbCommand command) { using (var reader = command.ExecuteReader()) { List <DAuthor> items = new List <DAuthor>(); while (reader.Read()) { var item = new DAuthor(); Map(reader, item); items.Add(item); } return(items); } }
/// <summary> /// Adds a DAuthor to the database. /// </summary> /// <param name="dAuthor">author to add</param> private void AddDAuthor(DAuthor dAuthor) { using (var command = context.CreateCommand()) { command.CommandText = @$ "Select * From Authors Where Authors.name = @name"; command.AddParameter($"name", dAuthor.Name); int?id = (int?)command.ExecuteScalar(); if (id != null) { dAuthor.Id = (int)id; } else { command.CommandText = @"Insert into Authors (Name) " + $"values (@name)" + "SELECT CAST(scope_identity() AS int);"; dAuthor.Id = (int)command.ExecuteScalar(); } } }
/// <summary> /// Maps a record to a DAuthor object. /// </summary> /// <param name="record">Record to bind.</param> /// <param name="dAuthor">Author to bind to.</param> private void Map(IDataRecord record, DAuthor dAuthor) { dAuthor.Id = (int)record["ID"]; dAuthor.Name = (string)record["Name"]; }
/// <summary> /// Transforms DAuthor objects into a Author object. /// </summary> /// <param name="dAuthor">Author to transform.</param> /// <returns>A Author object.</returns> public static Author ToAuthor(DAuthor dAuthor) { return(new Author(dAuthor.Name)); }