The ILuvBooksAdapter class is shown below.
Listing 5
public class ILuvBooksAdapter: BookAdapter
{
public override Book RetrieveByISBN(string isbn)
{
DataTable results = BooksService.GetBookByISBN(isbn);
if (results != null && results.Rows.Count > 0)
return this.CreateBook(results.Rows[0]);
else
return null;
}
public override Book[]RetrieveBySearch(string searchText, bool anyTerms)
{
DataTable results = BooksService.GetBooksBySearch(searchText, anyTerms);
List < Book > books = new List < Book > ();
if (results != null && results.Rows.Count > 0)
{
foreach (DataRow row in results.Rows)
books.Add(this.CreateBook(row));
}
return books.ToArray();
}
}
There are differences with this approach. Because the
provider is static, it makes it slightly easier to reference. However, there is
added code because of some conversions from the data source to the business
object, which was built-in with the last provider. To convert the row to a
Book object, the CreateBook method is used here too.
Listing 6
private Book CreateBook(DataRow bookRow)
{
Book book = new Book();
book.Author = bookRow["Author"].ToString();
book.Description = bookRow.IsNull("Description") ? string.Empty: bookRow[
"Description"].ToString();
book.ISBN = bookRow["ISBN"].ToString();
book.Title = bookRow["Title"].ToString();
return book;
}
As you can see, a certain amount of null checking must be
performed for the Description field. The rest of the fields are considered
required. The resulting book object is returned.