/// <summary> /// Provides a list of books created by an author. It will use the IBookStoreEntities /// created by the factory to list the books. The IBookeStoreEntities created by /// the factory could be a fake database, or a real SQL server database, this code doesn't /// care. /// </summary> /// <param name="authorLastName">The last name of the author.</param> /// <returns>A list of books created by an author with the specified last name.</returns> public List <Book> ListBooksCreatedBy(string authorLastName) { using (IBookStoreEntities entities = factory.Create()) { return(entities.Books .Where(b => b.Author.LastName == authorLastName) .ToList()); } }
/// <summary> /// Initialises the books table of the database. /// </summary> /// <param name="database">The entities to update.</param> private void CreateBooks(IBookStoreEntities database) { // Add fake books. if (!database.Books.Any()) { var authorOne = new Author() { DateOfBirth = new DateTime(2010, 1, 1), FirstName = "First Name 1", LastName = "Last Name 1" }; database.Books.Add(new Book() { Author = authorOne, Title = "Book 1", YearPublished = 2010 }); database.Authors.Add(authorOne); var authorTwo = new Author() { DateOfBirth = new DateTime(2011, 1, 1), FirstName = "First Name 2", LastName = "Last Name 2" }; database.Books.Add(new Book() { Author = authorTwo, Title = "Book 2", YearPublished = 2011 }); database.Authors.Add(authorOne); database.SaveChanges(); } }
/// <summary> /// This method could be used to initialise either a real database /// or a fake one. /// </summary> public void Initialise(IBookStoreEntities database) { CreateBooks(database); database.SaveChanges(); }