I originally arrived at this pattern after reading up on the MVC (Model View Controller). The idea of a model really intrigued me. Because the more I thought about standard web pages they are mostly based around only one "object", such as a User or a Product and everything else is just meta data, such as purchases for the User. So if you define all your models that you need for your website ahead of time such as User, UserProfile, Product, ProductProfile, etc you have not only helped define a development plan, but you have created very convent interfaces that both have common attributes. For example:
interface IUser {
int Id { get; set; }
string UserName { get; set; }
DateTime JoinedOn { get; set; }
}
interface IUserProfile : IUser {
string FullName { get; set; }
string Email { get; set; }
UserBilling Billing { get; set; }
Address Address { get; set; }
List Purchases { get; }
void MakePurchase(IProduct product);
}
interface IProduct {
int Id { get; set; }
string Name { get; set; }
decimal Price { get; set; }
string Description { get; set; }
}
interface IProductProfile : IProduct {
List<string> Comments { get; }
float CustomerRating { get; set; }
int StockCount { get; set; }
void Purchase(IUser user);
void AddPicture (string path);
}Then with these interfaces you create objects.
class User : IUser, IUserProfile {
// implement interfaces plus supporting code
}
class Product : IProduct, IProductProfile {
// implement interfaces plus supporting code
}Then you create a helper class to fill these methods in the data layer.
static class DataHelper {
public static IProduct GetProduct (int id) {
IProduct p = new Product();
// get data from database and only fill in IProduct interfaces
}
public static IProductProfile GetProductProfile (int id) {
IProductProfile p = new Product();
// get data from database and fill in IProductProfile interfaces
// including supporting tables such as the collections
// or fill in some and dirty fill the others
}
// do the same for User
}
Personally this is similar to how I am now implementing it. I am hoping LINQ is going to be able to help me out so that only the data I need is returned for each interface. If LINQ can't do the job I may have to pony up the time and create the DAL by hand. Honestly performance matters so I can't trust a DataSet in this instance.