public Book Map(Book book, Author author) { if (author.Id != 0) { Author outAutor; if (!m_authors.TryGetValue(author.Id, out outAutor)) { outAutor = author; m_authors.Add(outAutor.Id, outAutor); } book.Author = outAutor; outAutor.Books.Add(book); } return book; }
public Author Map(Author author, Book book) { if (author == null) return m_current; if (m_current != null && m_current.Id == author.Id) { m_current.Books.Add(book); book.Author = m_current; return null; } var previous = m_current; m_current = author; m_current.Books.Add(book); book.Author = m_current; return previous; }
static void Main(string[] args) { var db = new DatabaseAccessor(new DatabaseConfiguration() { DbName = "test", Host = "localhost", Password = "", User = "******", ProviderName = "MySql.Data.MySqlClient", }); db.RegisterMappingAssembly(Assembly.GetExecutingAssembly()); db.Initialize(); db.OpenConnection(); db.Database.Execute("DELETE FROM authors"); db.Database.Execute("DELETE FROM books"); var author = new Author() { Books = new List<Book>(), Name = "Bernard" }; var author2 = new Author() { Books = new List<Book>(), Name = "Clement" }; var book = new Book() { Author = author, Name = "Livre de Bernard 1", Type = BookType.Romance, PublicationDate = DateTime.Now, }; var book2 = new Book() { Author = author, Name = "Livre de Bernard 2", Type = BookType.Polar, PublicationDate = DateTime.Now, }; var book3 = new Book() { Author = author2, Name = "Livre de Clement 1", Type = BookType.Romance, PublicationDate = DateTime.Now, }; author.Books.Add(book); author.Books.Add(book2); author2.Books.Add(book3); db.Database.Save(author); db.Database.Save(author2); db.Database.Save(book); db.Database.Save(book2); db.Database.Save(book3); var sw = Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { var books = db.Database.Fetch<Book, Author, Book>(new BookRelator().Map, BookRelator.FetchQuery); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 100d + "ms"); var authors = db.Database.Fetch<Author, Book, Author>(new AuthorRelator().Map, AuthorRelator.FetchQuery); sw = Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { var bookGet = db.Database.Query<Book, Author, Book>(new BookRelator().Map, "SELECT * FROM books LEFT JOIN authors ON books.AuthorId = authors.Id WHERE books.Name=@0 LIMIT 1", "").FirstOrDefault(); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 100d + "ms"); sw = Stopwatch.StartNew(); for (int i = 0; i < 100; i++) { var cmd = db.Database.Connection.CreateCommand(); cmd.CommandText = "SELECT * FROM books LEFT JOIN authors ON books.AuthorId = authors.Id"; var reader = cmd.ExecuteReader(); while (reader.NextResult()) ; reader.Close(); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds / 100d + "ms"); var exists = db.Database.ExecuteScalar<bool>("SELECT EXISTS(SELECT 1 FROM books WHERE Name='Livre de Benard 1')"); Console.Read(); }