Using the Adapter Pattern
page 5 of 8
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35703/ 68

I Luv Books Adapter

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.


View Entire Article

User Comments

Title: Great !!   
Name: Gourik kumar Bora
Date: 2011-05-12 2:09:23 AM
Comment:
thanks a lot Brian ....
Title: Excellent article   
Name: Chetan P
Date: 2008-08-18 6:01:55 AM
Comment:
This is an excellent article i read about adapter pattern. I wish if it was given with UML diagrams, it would more appropriate to understand at a glance.
Title: Great   
Name: vishal
Date: 2007-12-05 12:54:21 AM
Comment:
very easy to understand and really simple yet powerful post.
Title: good one   
Name: rakesh gurjar
Date: 2007-05-21 9:11:14 AM
Comment:
nice to understand concept of adapter pattern






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 4:04:33 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search