Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hello

I'm designing a new employee salary application the requirement is to add multiple employee types in the future and I'm confused if my approach is the right one.

What I have tried:

C#
public interface IEmployee {
        string Name { get; set; }
        decimal CalculateSalary(DateTime from,DateTime to,List<Vacation> vacations);
    }

and the Impelementation to work with sql database goes like this
C#
public class Emp1 : IEmployee {
        public int Id { get; set; }
        public string Name { get; set; }
        public readonly int emptype = 1;
        
    }
    public class Emp2 : IEmployee {
        public int Id { get; set; }
        public string Name { get; set; }
        public readonly int emptype = 2;
        
    }
    public class Vacation {
        public int Id { get; set; }
        public int EmpId { get; set; }
        public int EmpType { get; set; }
        public DateTime Date { get; set; }
        public string Reason { get; set; }
        
    }

I had gone with such implementation because each employee is very different from the others but vacations are the same for them all.
is there a better approach?
I'm also confused how am I supposed to validate employee name none repeating
Posted
Updated 2-May-20 2:49am

1 solution

Your approach looks ok to me, however when your application gets bigger and more complex you might be interested in implementing the Factory pattern:
https://dzone.com/articles/design-patterns-c-factory[^]
C# Design Patterns - Factory Method - Code Maze[^]

Sometimes creating an (UML) diagram can help, see: How do I design an employee manager relationship class diagram[^]

To validate the employee name you can use a UNIQUE constraint in your database, or if you prefer a C# solution you could use a dictionary, see: Dictionary<TKey,TValue> Class (System.Collections.Generic) | Microsoft Docs[^]

If your database needs are simple I would recommend LiteDB, see: best-databases-for-a-small-net-application[^]

If you want to use SQL Server (warning not the easiest option) take a look at: Querying SQL Server Tables from .NET[^]

If you want a database independent solution then you can use an ORM, see: orms-for-c[^]

To learn about database design, see: online-resources-to-learn-relational-database-design[^]
 
Share this answer
 
v6
Comments
M. Rawan 5-May-20 5:13am    
thank you for your replay and the factory pattern is very good but if this is Ok I have the following question:
how should this be stored in the database
if each emp concrete class has its own table how to apply a get all employees method?
should I have a BaseEmp table to store the names and each emp concrete class have its details in another table.
RickZeeland 5-May-20 7:34am    
See the SQL Server example to get an idea how to setup the database, however I would not recommend SQL Server if you just started database development.

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