using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
[Serializable]
public class Book
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Url { get; set; }
public double Price { get; set; }
public DateTime PublishDate { get; set; }
public string PriceFormatted
{
get { return string.Format("{0:C}", Price); }
}
public string PublishDateShort
{
get { return PublishDate.ToShortDateString(); }
}
public Book() { }
public static Book Create(int id)
{
Book bk = new Book();
Random random = new Random();
bk.ID = id;
bk.Title = string.Format("Title of Book {0}", id);
bk.Author = string.Format("Author {0}", id);
bk.Url = string.Format("http://site.com/book/{0}/", id);
bk.Price = Convert.ToDouble(string.Format("{0:##.##}", random.Next(5, 35) + random.NextDouble()));
bk.PublishDate = Convert.ToDateTime(string.Format("{0}/{1}/{2}", random.Next(1, 12), random.Next(1, 28), random.Next(1990, DateTime.Now.Year)));
return bk;
}
}
public class BookRepository
{
private static BookRepository _instance;
public static BookRepository Instance
{
get { return _instance; }
}
public BookRepository() { }
static BookRepository()
{
_instance = new BookRepository();
}
public Book GetByID(int id)
{
return Book.Create(id);
}
///
/// Returns a List of 10 Book objects
///
[DataObjectMethod(DataObjectMethodType.Select, true)]
public List GetBooks()
{
return this.GetBooks(10);
}
///
/// Mocks insertion into a persistence layer
///
[DataObjectMethod(DataObjectMethodType.Insert, true)]
public void Insert(string title, string author, string url, double price, DateTime publishDate)
{
Debug.WriteLine("Insert object into database");
}
///
/// Mocks update to a persistence layer
///
[DataObjectMethod(DataObjectMethodType.Update, true)]
public void Update(int ID, string title, string author, string url, double price, DateTime publishDate)
{
Debug.WriteLine("Update database");
}
///
/// Mocks deleting from a persistence layer
///
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public void Delete(int ID)
{
Debug.WriteLine("Delete from database");
}
///
/// Returns a list of Book objects where you decide the quantity
///
[DataObjectMethod(DataObjectMethodType.Select, false)]
public List GetBooks(int numberOfBooks)
{
List books = new List();
for (int i = 1; i < (numberOfBooks + 1); i++)
{
books.Add(Book.Create(i));
}
return books;
}
///
/// Returns 10 book records in a DataSet
///
[DataObjectMethod(DataObjectMethodType.Select, true)]
public DataSet GetBooksAsDataSet()
{
return this.GetBooksAsDataSet(10);
}
///
/// Returns a series of books in a DataSet
///
[DataObjectMethod(DataObjectMethodType.Select, false)]
public DataSet GetBooksAsDataSet(int numberOfBooks)
{
DataSet ds = new DataSet();
DataTable dt;
dt = new DataTable();
dt.Columns.Add(new DataColumn("ID"));
dt.Columns.Add(new DataColumn("Title"));
dt.Columns.Add(new DataColumn("Author"));
dt.Columns.Add(new DataColumn("Price"));
dt.Columns.Add(new DataColumn("Url"));
dt.Columns.Add(new DataColumn("PublishDate"));
for (int i = 1; i < (numberOfBooks + 1); i++)
{
this.AddNewRow(ref dt, i);
}
ds.Tables.Add(dt);
return ds;
}
private void AddNewRow(ref DataTable dt, int index)
{
DataRow dr = dt.NewRow();
Book bk = Book.Create(index);
dr["ID"] = index;
dr["Title"] = bk.Title;
dr["Author"] = bk.Author;
dr["Price"] = bk.Price;
dr["Url"] = bk.Url;
dr["PublishDate"] = bk.PublishDate;
dt.Rows.Add(dr);
}
}