2010-08-10 139 views
1

我需要将这两个属性定义到我的linqclasses(Employee)之一..因为我需要跟踪Employees的健康进度...每个员工都需要每月进行一次PhysicalExams,因此我定义了这些性能帮助将属性添加到LinqToSql类

public decimal? Initial_Mass_Index 
      { 
       get 
       { 
        return this.PhysicalExams.OrderBy(p => p.Date).First().MassIndex ?? 0; 
       } 
      } 
    public decimal? Current_Mass_Index 
      { 
       get 
       { 
        return this.PhysicalExams.OrderByDescending(p => p.Date).First().MassIndex ?? 0; 
       } 
     } 

我的问题是..我的应用程序正在使用存储库模式,我已阅读,这是不恰当的做法有示范类中数据访问...

你们有什么reccomend ?? 请帮助 在此先感谢

回答

0

夫妇观察:无需使小数为空,因为无论如何您都无法合并它。此外,您不需要创建ExtendedEmployee,因为您只需添加到部分类。

我会朝向实现IMassIndexRepository

public interface IMassIndexRepository 
{ 
    public decimal GetInitialMassIndex(Employee e); 
    public decimal GetCurrentMassIndex(Employee e); 
} 

然后,如果你想拥有这个作为你的模型的一部分可能是瘦,你可以添加一个扩展方法或模型引用资源库。我会倾向于后者:

public partial class Employee 
{ 
    public decimal? Initial_Mass_Index 
    { 
     get 
     { 
      return GetMassIndexRepository().GetInitialMassIndex(this); 
     } 
    } 

    public decimal? Current_Mass_Index 
    { 
     get 
     { 
      return GetMassIndexRepository().GetCurrentMassIndex(this); 
     } 
    } 
} 

你必须实现GetMassIndexRepository(可能使用一些DI容器)。一旦拥有MassIndexRepository,您就可以在该级别实现缓存,并且执行“数据访问”不会像直接Linq属性调用那样影响性能。

0

,你可以创建一个像ExtendedEmployee一类继承你的员工的模型

public class ExtendedEmployee : Employee 
{ 
    public decimal? Initial_Mass_Index 
    { 
     get 
     { 
      return this.PhysicalExams.OrderBy(p => p.Date).First().MassIndex ?? 0; 
     } 
    } 
    public decimal? Current_Mass_Index 
    { 
     get 
     { 
      return this.PhysicalExams.OrderByDescending(p => p.Date).First().MassIndex ?? 0;    
     } 
    } 
} 

并以此作为ExtendedEmployee模型。