简体   繁体   English

如何获取属性字段的Property Expression

[英]How can I get Property Expression for field of a property

I'm using Entity Framework 6. I have 2 classes: 我正在使用Entity Framework 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;}

    ......
}

And I have BusinessUnitId on User as the Business Unit. 我将BusinessUnitId作为业务部门的用户。

My business logic is: 我的业务逻辑是:

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. 如果User是Employee,则用户可以在User上使用相同的BusinessUnitId打开所有预订记录。 If User is RegisteredUser, the user can only open booking records with the same UserId with the user. 如果User是RegisteredUser,则用户只能使用与用户相同的UserId打开预订记录。 If User is other roles, the user cannot open booking record at all. 如果用户是其他角色,则用户根本无法打开预订记录。

So I have created an Expression for Customer Class for the business logic above. 所以我为上面的业务逻辑创建了一个Expression for Customer 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;
    }

In which ParameterExpression param is a param of type Customer. 其中ParameterExpression参数是Customer类型的参数。

For BookingRecord class, I need the same logic to be applied on the Customer property. 对于BookingRecord类,我需要在Customer属性上应用相同的逻辑。 Ie, BookingRecord.Customer.BusinessUnitId and BookingRecord.Customer.UserId must apply the above Expression when it is selected from DB. 即, BookingRecord.Customer.BusinessUnitIdBookingRecord.Customer.UserId必须在从DB中选择时应用上述表达式。

How can I re-use the method on BookingRecord? 如何在BookingRecord上重复使用该方法?

In other word, how can I get an Expression similar like Expression.Property(param, typeof(Customer).GetProperty("BusinessUnitId")) but accepts param with the type of BookingRecord and applies on the field of the CustomerField of BookingRecord? 换句话说,如何获得类似Expression.Property(param, typeof(Customer).GetProperty("BusinessUnitId"))但接受具有Expression.Property(param, typeof(Customer).GetProperty("BusinessUnitId"))类型的param并应用于Expression.Property(param, typeof(Customer).GetProperty("BusinessUnitId"))的CustomerField字段? Something like 就像是

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

Thanks pinkfloydx33. 谢谢pinkfloydx33。 Expression.Property(Expression.Property(param,...),...) is just what I was looking for. Expression.Property(Expression.Property(param,...),...)正是我所寻找的。

So I have the method now as 所以我现在的方法是

    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;
    }

And in caller of Customer class I call it as 在Customer类的调用者中,我称之为

GetUserRoleExtraConditionExpression(param);

( param is Customer type ParameterExpression ) paramCustomer类型ParameterExpression

and in caller of BookingRecord class I call it as 在BookingRecord类的调用者中,我将其称为

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

( param is a BookingRecord type ParameterExpression ) param是一个BookingRecord类型的ParameterExpression

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM