如何获得 属性 字段的 属性 表达式
How can I get Property Expression for field of a property
我正在使用 Entity Framework 6. 我有 2 个 classes:
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 作为业务单位。
我的业务逻辑是:
If User is Administrator, the user can open all booking records in the system.
If User is Employee, the user can open all booking records with the same BusinessUnitId on User.
If User is RegisteredUser, the user can only open booking records with the same UserId with the user.
If User is other roles, the user cannot open booking record at all.
所以我为上面的业务逻辑创建了一个客户表达式Class。
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 class,我需要将相同的逻辑应用于客户 属性。即,BookingRecord.Customer.BusinessUnitId
和 BookingRecord.Customer.UserId
必须在从 DB 中选择时应用上述表达式。
如何在 BookingRecord 上重复使用该方法?
换句话说,我怎样才能得到一个类似于 Expression.Property(param, typeof(Customer).GetProperty("BusinessUnitId"))
但接受 BookingRecord 类型的参数并应用于 BookingRecord 的 CustomerField 字段的表达式?
像
Expression.Property(param,
typeof(BookingRecord)
.GetProperty(Customer)
.GetProperty("BusinessUnitId"))
谢谢 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;
}
在客户 class 的来电者中,我将其称为
GetUserRoleExtraConditionExpression(param);
(param
是 Customer
类型 ParameterExpression
)
在 BookingRecord class 的调用者中,我称之为
GetUserRoleExtraConditionExpression(Expression.Property(param, "Customer"))
(param
是一个 BookingRecord
类型 ParameterExpression
)
我正在使用 Entity Framework 6. 我有 2 个 classes:
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 作为业务单位。
我的业务逻辑是:
If User is Administrator, the user can open all booking records in the system. If User is Employee, the user can open all booking records with the same BusinessUnitId on User. If User is RegisteredUser, the user can only open booking records with the same UserId with the user. If User is other roles, the user cannot open booking record at all.
所以我为上面的业务逻辑创建了一个客户表达式Class。
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 class,我需要将相同的逻辑应用于客户 属性。即,BookingRecord.Customer.BusinessUnitId
和 BookingRecord.Customer.UserId
必须在从 DB 中选择时应用上述表达式。
如何在 BookingRecord 上重复使用该方法?
换句话说,我怎样才能得到一个类似于 Expression.Property(param, typeof(Customer).GetProperty("BusinessUnitId"))
但接受 BookingRecord 类型的参数并应用于 BookingRecord 的 CustomerField 字段的表达式?
像
Expression.Property(param,
typeof(BookingRecord)
.GetProperty(Customer)
.GetProperty("BusinessUnitId"))
谢谢 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;
}
在客户 class 的来电者中,我将其称为
GetUserRoleExtraConditionExpression(param);
(param
是 Customer
类型 ParameterExpression
)
在 BookingRecord class 的调用者中,我称之为
GetUserRoleExtraConditionExpression(Expression.Property(param, "Customer"))
(param
是一个 BookingRecord
类型 ParameterExpression
)