Inspired by the DDD hype, I designed my classes pretending there was no database at all. And then used NHibernate to map the classes to database tables. Part of my class graph look like this: Order (hasmany)--> Product (belongsto)-->Seller. To retrieve all orders placed for a certain seller. I have the code:
public class Order:ActiveRecordBase<Order>
{
[HasMany]
public ICollection<Product> Items{get;set;}
...
}
public class Product: Order:ActiveRecordBase<Product>
{
[BelongsTo]
public Seller Seller{get; set;}
...
}
public class OrderRepository:IOrderRepository
{
public IQuerable<Order> GetOrdersBySellerId(int sellerId)
{
return Order.FindAll().AsQuerable.Where(x=>x.Items.Count > 0 &&
x.Items.First().Seller.SellerID == sellerId).AsQuerable();
}
}
Not pretty, but it worked with my unit tests. I was happy until we started to inject real data into database. The performance is so bad that I want to vomit. So, I did a little investigation for my repository code. Not surprisedly, in order to find the orders I want, I had to get All data from Order table, and All data from Product table and some data from Seller table.
So, here comes my problem. As a total dummy in database area, I don't know how to improve my repository code even though I know it stinks badly. So, I am tempted to modify my Order class to have a reference to Seller class, so that I can use hql to add a Where clause to improve the performance. But modifying class structure due to database problem seems to be violating the DDD principles.
What is your suggestion,especially in improving my repository code? I have tried Linq for ActiveRecord and hql. But couldn't get them to work.