1

我使用實體框架6場我有2類:我怎樣才能獲得屬性表達式爲屬性

public class BookingRecord 
{ 
    public int Id {get; set;} 
    public int CustomerId {get; set;} 

    [ForeignKey("CustomerId")] 
    public virtual Customer Customer {get; set;} 

    ...... 
} 

public class Customer 
{ 
    public int Id {get; set;} 
    public int BusinessUnitId {get; set;} 
    public int UserId {get; set;} 

    ...... 
} 

而且我BusinessUnitId對用戶的業務單位。

我的商業邏輯是:

如果用戶是管理員,用戶可以在系統中打開所有預訂記錄。 如果用戶是員工,則用戶可以在用戶上打開所有具有相同BusinessUnitId的預訂記錄。 如果用戶是RegisteredUser,則用戶只能打開與用戶具有相同UserId的預訂記錄。 如果用戶是其他角色,則用戶根本無法打開預訂記錄。

因此,我爲上面的業務邏輯創建了客戶類的表達式。

protected Expression GetUserRoleExtraConditionExpression(ParameterExpression param) 
    { 
     Expression expression; 

     IPrincipal user = HttpContext.Current.User; 

     if (user.IsInRole(Consts.Roles.Administrator)) 
     { 
      expression = Expression.Constant(true); 
     } 
     else if (user.IsInRole(Consts.Roles.Employee)) 
     { 
      int businessUnitId = UserManager.FindById(user.Identity.GetUserId()).BusinessUnitId; 
      expression = Expression.Equal(
       Expression.Property(param, typeof(Customer).GetProperty("BusinessUnitId")), 
       Expression.Constant(businessUnitId)); 
     } 
     else if (user.IsInRole(Consts.Roles.RegisteredUser)) 
     { 
      string userId = user.Identity.GetUserId(); 
      expression = Expression.Equal(
       Expression.Property(param, typeof(Customer).GetProperty("UserId")), 
       Expression.Constant(userId)); 
     } 
     else 
     { 
      expression = Expression.Constant(false); 
     } 
     return expression; 
    } 

其中ParameterExpression參數是Customer類型的參數。

對於BookingRecord類,我需要在Customer屬性上應用相同的邏輯。即,當從DB中選擇時,BookingRecord.Customer.BusinessUnitIdBookingRecord.Customer.UserId必須應用上述表達式。

如何重新使用BookingRecord上的方法?

換句話說,我如何得到一個類似於Expression.Property(param, typeof(Customer).GetProperty("BusinessUnitId"))的表達式,但接受了帶有BookingRecord類型的param並應用於BookingRecord的CustomerField的字段? 類似於

Expression.Property(param, 
    typeof(BookingRecord) 
     .GetProperty(Customer) 
     .GetProperty("BusinessUnitId")) 
+0

爲什麼你要這樣不遺餘力地在這裏生成的表達? –

+2

'Expression.Property(Expression.Property(param,...),...)' – pinkfloydx33

+0

謝謝pinkfloydx33,我很快發現它就是這麼簡單... –

回答

0

謝謝pinkfloydx33。 Expression.Property(Expression.Property(param,...),...)就是我正在尋找的。

所以我的方法現在作爲

protected Expression GetUserRoleExtraConditionExpression(Expression param) 
    { 
     Expression expression; 

     IPrincipal user = HttpContext.Current.User; 

     if (user.IsInRole(Consts.Roles.Administrator)) 
     { 
      expression = Expression.Constant(true); 
     } 
     else if (user.IsInRole(Consts.Roles.Employee)) 
     { 
      int businessUnitId = UserManager.FindById(user.Identity.GetUserId()).BusinessUnitId; 
      expression = Expression.Equal(
       Expression.Property(param, "BusinessUnitId"), 
       Expression.Constant(businessUnitId)); 
     } 
     else if (user.IsInRole(Consts.Roles.RegisteredUser)) 
     { 
      string userId = user.Identity.GetUserId(); 
      expression = Expression.Equal(
       Expression.Property(param, "UserId"), 
       Expression.Constant(userId)); 
     } 
     else 
     { 
      expression = Expression.Constant(false); 
     } 
     return expression; 
    } 

,並且以客戶類的調用者我稱其爲

GetUserRoleExtraConditionExpression(param);

param爲​​Customer型)

,並在BookingRecord類的調用者我稱之爲

GetUserRoleExtraConditionExpression(Expression.Property(param, "Customer"))

paramBookingRecord型​​)

相關問題