Object with two base classes

  • Thread starter Thread starter Justin Dutoit
  • Start date Start date
J

Justin Dutoit

Hey gents. I have a technique I'm using in a pretty standard three-tier web
app: to show Customers to the user, I have a stored proc which queries the
customers table. The datatable is unpacked into a List<Customer> where the
Customer object has a property for each column of the db table. OK so far.
But now I want to show a shopping Basket, which contains columns from two
tables, Products and Purchases. Product Name comes from Products and the
number of Units in the basket comes from Purchases.

What I want is to have an object which contains all the properties from the
Product object and all those from the Purchase object. At the moment my
stop-gap is an object called ProductPurchase which inherits from Product and
manually adds the NumberOfUnits property the same as it is in Purchase. This
manually adding part doesn't seem right.

I want to continue using the List<object> in my app. What is the best way to
get organized? Can I get around the limitation where an object cannot have
two base classes? Or should I use a different approach...

Thanks in advance for your time

Justin Dutoit
 
Thanks for the reply Pete.

Re: "or make a whole new object (call it "BasketItem"
if you like :) ) that has references to both Product and Purchase objects,
and exposes properties appropriate only to the Basket, delegating the
getters for those properties to the actual objects."
-could you give some example code plz? Everything before was v informative,
but for this paragraph I need some elaborating.

Tks!
Justin


Peter Duniho said:
[...]
What I want is to have an object which contains all the properties from
the
Product object and all those from the Purchase object. At the moment my
stop-gap is an object called ProductPurchase which inherits from Product
and
manually adds the NumberOfUnits property the same as it is in Purchase.
This
manually adding part doesn't seem right.

I want to continue using the List<object> in my app. What is the best
way to
get organized? Can I get around the limitation where an object cannot
have
two base classes? Or should I use a different approach...

I think you should use a different approach.

I'm not really clear on what you mean by "get around the limitation".
It's a well-defined, hard-and-fast limitation in the language; it's not
like there are loopholes by which you could inherit two different
implementations.

In some cases, as an alternative to multiple inheritance since it's not
supported in C#, is to define interfaces corresponding to the multiple
behaviors you want to include as part of your object, then implement those
interfaces by delegating to some other class. But, your situation doesn't
really seem like a good place for inheritance.

In particular, you seem to have an object that represents an item in your
Basket. Using inheritance, you want to essentially say that each item "is
a" Product _and_ "is a" Purchase. But, if that were true, it would make
sense to put any item that's in your Basket into either the table of
Products or table of Purchases, where it could reside happily alongside
other Products or Purchases.

To me, that seems wrong. Instead, it seems to me that your "BasketItem"
(made up name) has a "has a" relationship with these objects, where you've
got a whole new item that contains within it references to a Product
object and a Purchase object. At best, you might simply refer to the
Product object from your Purchase object, and put Purchase objects in the
Basket instead of having a whole new item. But for sure, it doesn't seem
like you really have an object that is both a Product and a Purchase at
the same time.

So, either simply change your exiting Purchase object so that it refers to
the appropriate Product and has a ProductName property for the purposes of
displaying in the Basket, or make a whole new object (call it "BasketItem"
if you like :) ) that has references to both Product and Purchase objects,
and exposes properties appropriate only to the Basket, delegating the
getters for those properties to the actual objects.

At least, that's what I think. :)

Pete
 
Thanks :)

Peter Duniho said:
Sure (abridged example):

class BasketItem
{
private Product _product;
private Purchase _purchase;

public ProductName { get { return _product.Name; } }
public PurchaseUnits { get { return _purchase.NumberOfUnits; } }
}

Sorry if my description wasn't clear enough. Hope that helps!

Pete
 
Back
Top