public static void AddBookComplex(string title, List<string> authors, long? isbn, decimal? price, string webSite, List<ReviewStruct> reviews) { using (BookstoreEntities dbContext = new BookstoreEntities()) { //Create authors List<Author> bookAuthors = new List<Author>(); foreach (var authorName in authors) { Author author = CreateOrLoadAuthor(authorName, dbContext); bookAuthors.Add(author); } //Create reviews List<Review> bookReviews = new List<Review>(); foreach (var reviewInfo in reviews) { Review review = new Review(); if (reviewInfo.AuthorName != null) { review.Author = CreateOrLoadAuthor(reviewInfo.AuthorName, dbContext); } review.Creation_Date = reviewInfo.CreationDate; review.Text = reviewInfo.Text; bookReviews.Add(review); } CheckIsbnUniqueness(isbn, dbContext); //Create book Book newBook = new Book() { Title = title, ISBN = isbn, Price = price, Web_Site = webSite }; foreach (var author in bookAuthors) { newBook.Authors.Add(author); } foreach (var review in bookReviews) { newBook.Reviews.Add(review); } //Save book dbContext.Books.Add(newBook); dbContext.SaveChanges(); } }
public void BooksComplexParse() { var books = this.GetBooksXml(); foreach (var bookXml in books) { var title = bookXml.Element("title").Value; var book = new Book { Title = title }; var authorsXml = bookXml.Elements("authors"); var authors = authorsXml.Select(authorXml => AuthorDao.GetOrCreateAuthor(this.Entities, authorXml.Value)).ToList(); book.Authors = authors; this.BookSetNotRequiredProperty("ISBN", "isbn", bookXml, book); this.BookSetNotRequiredProperty("Price", "price", bookXml, book); this.BookSetNotRequiredProperty("Url", "web-site", bookXml, book); var reviewsXml = bookXml.Elements("reviews"); var reviews = new List<Review>(); foreach (var reviewXml in reviewsXml) { var dateAttribute = reviewXml.Attribute("date"); string date = null; if (dateAttribute != null) { date = dateAttribute.Value; } var authorAttribute = reviewXml.Attribute("author"); string author = null; if (authorAttribute != null) { author = authorAttribute.Value; } reviews.Add(ReviewDao.CreateReview(this.Entities, reviewXml.Value, date, author)); } book.Reviews = reviews; this.Entities.Books.Add(book); this.Entities.SaveChanges(); } }
public void SimpleBooksParse() { var books = this.GetBooksXml(); foreach (var bookXml in books) { var title = bookXml.Element("title").Value; var book = new Book { Title = title }; var authorName = bookXml.Element("author").Value; var author = AuthorDao.GetOrCreateAuthor(this.Entities, authorName); book.Authors.Add(author); this.BookSetNotRequiredProperty("ISBN", "isbn", bookXml, book); this.BookSetNotRequiredProperty("Price", "price", bookXml, book); this.BookSetNotRequiredProperty("Url", "web-site", bookXml, book); this.Entities.Books.Add(book); this.Entities.SaveChanges(); } }
public static void AddBook(string title, string authorName, long? isbn, decimal? price) { using (BookstoreEntities dbContext = new BookstoreEntities()) { Author author = CreateOrLoadAuthor(authorName, dbContext); CheckIsbnUniqueness(isbn, dbContext); Book newBook = new Book() { Title = title, ISBN = isbn, Price = price }; newBook.Authors.Add(author); dbContext.Books.Add(newBook); dbContext.SaveChanges(); } }
protected void BookSetAuthors(ICollection<Author> authors, Book book) { book.Authors = authors; }
protected void BookSetNotRequiredProperty(string propertyName, string xmlTagName, XElement bookXml, Book book) { var element = bookXml.Element(xmlTagName); if (element == null) { return; } var bookType = book.GetType(); var property = bookType.GetProperty(propertyName); object elementValue = element.Value; if (property.PropertyType.IsAssignableFrom(typeof(Nullable<decimal>))) { elementValue = decimal.Parse(element.Value); } property.SetValue(book, elementValue); }