-1

I have following model corresponding to the database tables listed below.

A manager is an employee. An accountant also is an employee.

  1. What is the best method to get all managers in the repository? How to implement GetAllManagers() method?
  2. Is it proper TPT?

enter image description here

CODE

MyRepository.MyEmployeeRepository rep = new MyEmployeeRepository();
List<Employee> e = rep.GetAllEmployees();



public class MyEmployeeRepository
{
    private string connectionStringVal;
    public MyEmployeeRepository()
    {
        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder.DataSource = ".";
        sqlBuilder.InitialCatalog = "LibraryReservationSystem";
        sqlBuilder.IntegratedSecurity = true;

        // Initialize the EntityConnectionStringBuilder.
        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.Provider = "System.Data.SqlClient";
        entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
        entityBuilder.Metadata = @"res://*/Test.csdl|res://*/Test.ssdl|res://*/Test.msl";

        connectionStringVal = entityBuilder.ToString();


    }


    public List<Employee> GetAllEmployees()
    {

        List<Employee> employees = new List<Employee>();
        using (var context = new MyEntityDataModelEDM.LibraryReservationSystemEntities1(connectionStringVal))
        {
            foreach (MyEntityDataModelEDM.Employee p in context.Employees)
            {
                employees.Add(p);
            }
        }

        return employees;
    }

    public List<Manager> GetAllManagers()
    {

        List<Manager> managers = new List<Manager>();
        using (var context = new MyEntityDataModelEDM.LibraryReservationSystemEntities1(connectionStringVal))
        {


        }

        return managers;
    }



}

EDIT

This model has drawbacks. It should consider following:

  1. Employee can be created without any role for him.
  2. One employee can have multiple roles.
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • This model has drawbacks. It should consider following: Employee can be created without any role for him. One employee can have multiple roles. – LCJ Jul 27 '12 at 14:30

1 Answers1

2

Just do:

return context.Employees.OfType<Accountant>().ToList()

This is however a really bad way to model employees.. if an accountant changes job, you need to kill and recreate that object..

See my answer on Inheritance vs enum properties in the domain model.

Community
  • 1
  • 1
Roger Johansson
  • 22,764
  • 18
  • 97
  • 193