0

I have following Domain.

Role is an abstract class. Programmer extends Role. Employee has multiple Roles like Programmer, Manager, etc.

I want to create a Model for it. How can we specify this composition of Roles in Employees entity?

Note: I cannot add EmpID in Roles table. Because same Role will be applicable for many employees.

Current Model

enter image description here

Pseudo Code for Required Conceptual model

public abstract class Role
{
    public abstract string RoleName { get; }
    public abstract int RoleID { get; }
}

public class ProgrammerRole : Role
{
    public override string RoleName { get { return "Programmer"; } }
    public override int RoleID { get { return 101; } }
}

public class ManagerRole : Role
{
    public override string RoleName { get { return "Manager"; } }
    public override int RoleID { get { return 102; } }
}


public class Employee
{
    private IList<Role> roles;
    public IList<Role> RolesList
    {
        get
        {
            return roles;
        }
    }


    public int EmployeeID { get; set; }

    //Constructor
    public Employee()
    {
        roles = new List<Role>();
    }

    public void TerminateEmployeeByRole(Role role)
    {
        if (RolesList == null)
        {
            //If employee has no role, make as inactive
            isActiveEmployee = false;
        }
        else
        {
            //If employee has no role other than the input role, make as inactive
            RolesList.Remove(role);
            if (RolesList.Count == 0)
           {
                isActiveEmployee = false;
            }
        }
    }

}
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    Generally speaking, each model class generally represents a collection object like `table`, so `class Employee` represents `Employee` table. Similarly, `ProgrammerRole` and `ManagerRole` would create two tables in the database. I am sure this is not what you want. – Vivek Jain Jun 20 '13 at 07:06

1 Answers1

0

This can be implemented in a simpler way:

public class Role
{
    public string RoleName { get; }
    public int RoleID { get; }
}

public class Employee
{
    public IList<int> RoleID { get; set; } // to store role id. This would form a table column
    [ForeignKey("RoleID")]
    public virtual IList<Role> RolesList { get; set; } // to reference Roles in the code. This will not for a table column

    public int EmployeeID { get; set; }

    //Constructor
    public Employee()
    {
        roles = new List<Role>();
    }

    public void TerminateEmployeeByRole(Role role)
    {
        if (RolesList == null)
        {
            //If employee has no role, make as inactive
            isActiveEmployee = false;
        }
        else
        {
            //If employee has no role other than the input role, make as inactive
            RolesList.Remove(role);
            if (RolesList.Count == 0)
            {
                isActiveEmployee = false;
            }
        }
    }
}

The reason for this design is that you would not be able to create more classes if more roles are added. You may create a wrapper class to fetch role details from the database.

Please use this as a starting point and not as a copy-paste solution.

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47