0

我有以下模型对应于下面列出的数据库表。实体框架:获取存储库中的子类对象

经理是员工。会计师也是一名员工。

  1. 什么是最好的方法来获取存储库中的所有管理人员?如何实现GetAllManagers()方法?
  2. 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; 
    } 



} 

编辑

这种模式有缺点。它应该考虑以下内容:

  1. 员工可以创建没有任何他的角色。
  2. 一名员工可以有多个角色。
+0

该模型有缺陷。应该考虑以下几点: 员工可以为他创建没有任何角色。 一名员工可以拥有多个角色。 – Lijo 2012-07-27 14:30:23

回答

2

只要做到:

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

然而,这是对员工进行建模一个非常糟糕的方式。如果会计师变化的工作,你需要杀掉并重新创建该对象..

见我回答于Inheritance vs enum properties in the domain model

+0

我会使用角色来做,例如员工或人员可以分配一组角色。 – 2012-07-27 08:52:14

+1

并让行为代替角色,这样,您不必在每次获得新职位时都杀死每个员工。 – 2012-07-27 08:52:57

+1

此外,经理和会计师的行为可能属于不同的有界上下文。 – 2012-07-27 08:56:34