.NET - Class Design

Associate
Joined
28 Nov 2004
Posts
1,237
Location
Birmingham
Hi guys,
I'm quite new to this .NET (C#) lark but I'm wondering if you can advise me on my class design options below:

I have a system which has 'Customers' and 'Clients' in the database.
Each has its own table in the db and I've also created a single generic 'Address' table to store address info for either of the two.

Now, in my code I have created a class for Customers and its all good. I can create, load and save customers via it.

The next step is to create an Address class and this is where I'm after advice. Do I:
a) Create a stand alone Address class, just like the Customer one and access all its variables and functions (i.e. save address) via the Customer class?
b) Create Address as an 'abstract' class which Customers can be derived from which I'm assuming will give Customers full access to its variables and keep methods (i.e. Save Address) in the abstract class (can I do that?).
c) Do as (b) but have the methods in the candidate class. The only thing I thought with this is that I would have to copy the same methods over to anything else that uses them such as Clients....shouldn;t it be kept in one place?

Anyway - I think I just need to get my head around the best way of using inheritance on this one! Any advice is welcomed!!
 
Prefer encapsulation over inheritance. Following OO principles you should only derive in 'is a' circumstances.

For example, a Ford Fiesta IS A Car which IS A Vehicle, so there's a natural OO class chain.

However a customer is not an address, so there really shouldn't be inheritance there.

Therefore I'd head down the 'a' route, and avoid 'b/c'.
 
Hi AliP,
That sounds good to me. What I'm thinking then is to have my stand alone Address class, create the properties for address in my Customer class then just call the Address Load/Save/Whatever functions as I need to.
 
create the properties for address in my Customer class

The properties for the address should be in the address class and as such should be accessed that way.

How are you supposed to re-use the class (and after all that is a main benefit of OO) if the properties are in the customer class?

Doesn't make sense to me.
 
sorry, i mean I'd do something like this in my customer class:

private Address _customerAddress;

public Address custAddress
{
get { return _custAddress; }
set { _custAddress = value; }
}
 
If you are using .net 3 or 3.5 you can do:
public Address custAddress
{
get; set;
}

And as everyone is saying, if you want to save the address you should do _custVariable.Address.Save(); :)
 
Don't confuse him, that's just C# :)

Good old C++ hates that sort of thing - it takes some getting used to but save some typing.

I am loving the new lambda function expressions though. I'm sick to death of functors in C++

If you are using .net 3 or 3.5 you can do:
public Address custAddress
{
get; set;
}

And as everyone is saying, if you want to save the address you should do _custVariable.Address.Save(); :)
 
Last edited:
Back
Top Bottom