Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,
I have a question about Object Oriented concepts. So I have a program where I have a User class and I have a Customer class which inherits all the user properties and then has some extras for example: User has Id, Username, Password. Then Customer inherits all of those and has a couple extra like Firstname, etc.
In my program can I use the ‘User’ class somewhere like for making a Admin then I pass the object to the next screens and in my new screen I want to be able to make a Customer that has all the User properties and then can have the new fields added like customerid, firstname, etc.

I have 2 tables a User table and a customer table and I wanted to reference each other with the ID as a foreign key.
Am I thinking about this wrong or should I be using Customer throughout my whole project or am I able to upgrade a User to Customer when they go to a specific window and add these new fields.

What I have tried:

just using the base class as long as i can go
now i have a new window where to make a customer and i want the customer to have all the user info but to also be able to do have these new property data
Posted
Updated 9-Nov-18 1:38am

Hi to you,
First, let`s make your question a little bit more clear and make sure that I understood right. So you have a class called User and a class called Customer which is a child class of the User class.

C#
public class User
{
   public string Id {get;set;}
   public string Username {get;set;}
   public SecureString Password {get;set;}
   //Read about SecureString in System.Security
}

public class Customer : User
{
   //This is more-less a builder pattern
   public Customer(User user)
   {
        Id = user.Id;
        Username = user.Username;
        Password = user.Password;
   }
   public string Name {get;set;}
}

var user = new User() { Id = "id", Username = "user" };
var customer = new Customer(user);


Ok, now where ever you want to create a customer from a user you just need to use the customer`s constructor and inject your user inside the customer object. However, I would prefer to have the user as a property of customer rather a child class of it. This will decouple these two entities and also will bring more flexibility and a clear understanding from the business point of view.
You can keep customers in a table with a foreign key to the user's table because a customer may have more than one user (depends strongly on your business model).
But, if for any reasonable reason you needed to use inheritance then what you can do is to create a base/abstract class like Person and drive both from this class.
Btw, you may also need to have a look at the Entity State Diagram which talks about different stats for the same Entity in different levels of the system.

Cheers,
AH
 
Share this answer
 
v5
It's wrong: you shouldn't try to set up circular references.
Why would you need to?
You have a list of Users - of which some are customers - and a separate list of Customers.
The Customer table refers to the Users table via a Foreign Key, but not all Users are Customers, so you don't need a link the other way.

You can then use a JOIN to combine information from the two tables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900