2010-08-10 129 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模型。