Below is the BooksExpressAdapter class.
Listing 3
public class BooksExpressAdapter: BookAdapter
{
private BooksProvider _provider = null;
protected BooksProvider Provider
{
get
{
if (_provider == null)
_provider = new BooksProvider();
return _provider;
}
}
public override Book RetrieveByISBN(string isbn)
{
BookItem item = this.Provider.GetByISBN(isbn);
if (item != null)
return this.CreateBook(item);
else
return null;
}
public override Book[]RetrieveBySearch(string searchText, bool anyTerms)
{
BookItem[]items = this.Provider.Search(searchText, anyTerms, false);
List < Book > books = new List < Book > ();
foreach (BookItem item in items)
books.Add(this.CreateBook(item));
return books.ToArray();
}
}
A few things to note is that the provider is lazy loaded, so
that it is instantiated when needed. In addition, these methods use the
CreateBook method to perform the conversion, which we will see next. The
Searching mechanism loops through the results, creating a Book object for each
item found in the collection. The Book object has a slightly different
interface, as shown below.
Listing 4
private Book CreateBook(BookItem item)
{
Book book = new Book();
book.Author = item.Author;
book.Description = item.Description;
book.ISBN = item.ISBN;
book.Title = item.Title;
//Nothing done with item.Keyword
return book;
}
Because the API uses a business object, the parameters come
over as a 1-to-1 mapping, however, the keyword parameter is ignored because the
new API does not use it.