Click here to Skip to main content
15,906,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

I'm having trouble figuring out How should this be stored in a sql database
C#
public interface IEmployee {
        string Name { get; set; }
        decimal CalculateSalary();
    }
public class Emp1 : IEmployee {
        public string Name { get; set; }
        //some other properties
        //.....
        public decimal CalculateSalary(){...}
    }
public class Emp2 : IEmployee {
        public string Name { get; set; }
        //some other properties
        //.....
        public decimal CalculateSalary(){...}
    }


What I have tried:

if each emp concrete class has its own table how to apply a get all employees method?

Or should I have a BaseEmp table to store the names with an emptype column and each emp concrete class has its own details in another table? if that is the right way should those types be an enum in my library or there is a better way?
Posted
Updated 5-May-20 8:05am
v2
Comments
CHill60 5-May-20 6:08am    
Emp1 and Emp2 sound more like instances of Employee than types of employee. Personally I would have a base employee table with a column for employee type. I would absolutely not have each concrete class in it's own table.
M. Rawan 5-May-20 7:13am    
thank you for your reply, I thought that too only confused about where to store those types (should it be an int that passed to each employee constrcutor or an enum in my library or just a fixed readonly int in each class)?
CHill60 5-May-20 9:42am    
So how you store it on the database is entirely up to you. If you use an ENUM in SQL you may have to change the table schema if a new type comes along. But you could store an Enum from your UI as an Int on your database. Or you could use a varchar or even a char(1). Whatever you think is going to be the most meaningful, but perhaps not take up too much space.
In terms of your classes though - you wouldn't pass anything to the constructors - the class should know what "kind" of employee it is e.g. using a hard-coded value for "type" in each class constructor - where all of the classes inherit from the basic "employee" class.
M. Rawan 5-May-20 10:09am    
thank you for your clear explanation, I think you should post this as an answer so I can mark it.

1 solution

As suggested by OP here is my comment as a solution

So how you store it on the database is entirely up to you. If you use an ENUM in SQL you may have to change the table schema if a new type comes along. But you could store an Enum from your UI as an Int on your database. Or you could use a varchar or even a char(1). Whatever you think is going to be the most meaningful, but perhaps not take up too much space.
In terms of your classes though - you wouldn't pass anything to the constructors - the class should know what "kind" of employee it is e.g. using a hard-coded value for "type" in each class constructor - where all of the classes inherit from the basic "employee" class.
 
Share this answer
 
Comments
Maciej Los 6-May-20 6:04am    
5ed!

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