编辑这个问题,希望让它更清楚。
我们首先设置了 Entity Framework 代码。为了示例的目的,我简化了两个类,实际上还有大约 10 多个类类似于“Record”,其中 Item 是一个导航属性/外键。
项目类别:
public class Item
{
public int Id { get; set; }
public int AccountId { get; set; }
public List<UserItemMapping> UserItemMappings { get; set; }
public List<GroupItemMapping> GroupItemMappings { get; set; }
}
记录类:
public class Record
{
public int ItemId { get; set; }
public Item Item { get; set; }
}
this.User 是一个注入(inject)到每个 repo 中的用户对象,并且包含在 repository base 中。 我们有一个包含以下代码的项目存储库:
var items = this.GetAll()
.Where(i => i.AccountId == this.User.AccountId);
我在存储库基础上创建了以下表达式,以便轻松地对其进行过滤(希望重新使用)。由于 LINQ to Entities 的工作方式,我们不能使用静态扩展方法(System.NotSupportedException“LINQ to Entities 无法识别方法 X,并且此方法无法转换为存储表达式。”)。
protected Expression<Func<Item, bool>> ItemIsOnAccount()
{
return item => item.AccountId == this.User.AccountId;
}
我已经通过这样做解决了上述情况:
var items = this.GetAll().Where(this.ItemIsOnAccount());
我们根据该帐户中的用户权限进行额外的过滤(同样,我不想在我们拥有的每个 repo 中重复此代码的另一种情况):
protected Expression<Func<Item, bool>> SubUserCanAccessItem()
{
return item => this.User.AllowAllItems
|| item.UserItemMappings.Any(d => d.UserId.Value == this.User.Id)
|| item.GroupItemMappings.Any(vm =>
vm.Group.GroupUserMappings
.Any(um => um.UserId == this.User.Id));
}
我可以按如下方式使用:
var items = this.GetAll().Where(this.SubUserCanAccessItem());
但是,我们还需要在 Record 存储库中有一种方法来解决以下问题:
var records = this.GetAll()
.Where(i => i.Item.AccountId == this.User.AccountId);
因为 Item 是一个单一的导航属性,我不知道如何将我创建的表达式应用到这个对象。
我想重用我在基于所有这些其他存储库的存储库中创建的表达式,以便我的“基于权限”的代码都在同一个地方,但我不能简单地把它扔进去,因为这里的 Where 子句case 是 Expression< func="">< record,bool="">>.
使用以下方法创建接口(interface):
Item GetItem();
由于 LINQ to entities,将其放在 Record 类中是行不通的。
我不能同时创建一个基本抽象类并从它继承,因为除了 Item 之外可能还有其他对象需要过滤。例如,一个记录也可以有一个具有权限逻辑的“事物”。并非所有对象都需要按“Item”和“Thing”过滤,有些只按一个,有些按另一个,有些按两者:
var items = this.GetAll()
.Where(this.ItemIsOnAccount())
.Where(this.ThingIsOnAccount());
var itemType2s = this.GetAll().Where(this.ThingIsOnAccount());
var itemType3s = this.GetAll().Where(this.ItemIsOnAccount());
因此,只有一个父类是行不通的。
有没有一种方法可以重用我已经创建的表达式,或者至少创建一个表达式/修改原始表达式以在 OTHER 存储库中全面工作,这些存储库当然会在 GetAll 中返回它们自己的对象,但是所有项目都有导航属性吗?我需要如何修改其他存储库才能使用这些存储库?
谢谢
最佳答案
表达式可重用性的第一步是将表达式移动到一个公共(public)静态类中。因为在你的情况下它们与 User 相关联,所以我会让它们成为 User 扩展方法(但请注意它们将返回表达式):
public static partial class UserFilters
{
public static Expression<Func<Item, bool>> OwnsItem(this User user)
=> item => item.AccountId == user.AccountId;
public static Expression<Func<Item, bool>> CanAccessItem(this User user)
{
if (user.AllowAllItems) return item => true;
return item => item.UserItemMappings.Any(d => d.UserId.Value == user.Id) ||
item.GroupItemMappings.Any(vm => vm.Group.GroupUserMappings.Any(um => um.UserId == user.Id));
}
}
现在 Item 存储库将使用
var items = this.GetAll().Where(this.User.OwnsItem());
或
var items = this.GetAll().Where(this.User.CanAccessItem());
为了对具有 Item 引用的实体可重用,您需要一个小的辅助实用程序来从其他 lambda 表达式组成 lambda 表达式,类似于 Convert Linq expression "obj => obj.Prop" into "parent => parent.obj.Prop" .
可以使用 Expression.Invoke 来实现它,但由于并非所有查询提供程序都支持调用表达式(EF6 不确定,EF Core 支持),因此我们将像往常一样使用用于将 lambda 参数表达式替换为另一个任意表达式的自定义表达式访问者:
public static partial class ExpressionUtils
{
public static Expression ReplaceParameter(this Expression expression, ParameterExpression source, Expression target)
=> new ParameterReplacer { Source = source, Target = target }.Visit(expression);
class ParameterReplacer : ExpressionVisitor
{
public ParameterExpression Source;
public Expression Target;
protected override Expression VisitParameter(ParameterExpression node)
=> node == Source ? Target : node;
}
}
两个组合函数如下(我不喜欢Compose这个名字,所以有时我用Map这个名字,有时用Select code>, Bind, Transform 等,但在功能上它们是一样的。在这种情况下,我使用的是 Apply 和 ApplyTo ,唯一不同的是转换方向):
public static partial class ExpressionUtils
{
public static Expression<Func<TOuter, TResult>> Apply<TOuter, TInner, TResult>(this Expression<Func<TOuter, TInner>> outer, Expression<Func<TInner, TResult>> inner)
=> Expression.Lambda<Func<TOuter, TResult>>(inner.Body.ReplaceParameter(inner.Parameters[0], outer.Body), outer.Parameters);
public static Expression<Func<TOuter, TResult>> ApplyTo<TOuter, TInner, TResult>(this Expression<Func<TInner, TResult>> inner, Expression<Func<TOuter, TInner>> outer)
=> outer.Apply(inner);
}
(没有什么特别之处,为完整性提供了代码)
现在您可以通过将它们“应用”到从另一个实体中选择 Item 属性的表达式来重用原始过滤器:
public static partial class UserFilters
{
public static Expression<Func<T, bool>> Owns<T>(this User user, Expression<Func<T, Item>> item)
=> user.OwnsItem().ApplyTo(item);
public static Expression<Func<T, bool>> CanAccess<T>(this User user, Expression<Func<T, Item>> item)
=> user.CanAccessItem().ApplyTo(item);
}
并将以下内容添加到实体存储库(在本例中为 Record 存储库):
static Expression<Func<Record, Item>> RecordItem => entity => entity.Item;
这将允许你在那里使用
var records = this.GetAll().Where(this.User.Owns(RecordItem));
或
var records = this.GetAll().Where(this.User.CanAccess(RecordItem));
这应该足以满足您的要求。
你可以更进一步,像这样定义一个接口(interface)
public interface IHasItem
{
Item Item { get; set; }
}
并让实体实现它
public class Record : IHasItem // <--
{
// Same as in the example - IHasItem.Item is auto implemented
// ...
}
然后像这样添加额外的助手
public static partial class UserFilters
{
public static Expression<Func<T, Item>> GetItem<T>() where T : class, IHasItem
=> entity => entity.Item;
public static Expression<Func<T, bool>> OwnsItem<T>(this User user) where T : class, IHasItem
=> user.Owns(GetItem<T>());
public static Expression<Func<T, bool>> CanAccessItem<T>(this User user) where T : class, IHasItem
=> user.CanAccess(GetItem<T>());
}
这将允许您在存储库中省略 RecordItem 表达式并使用它来代替
var records = this.GetAll().Where(this.User.OwnsItem<Record>());
或
var records = this.GetAll().Where(this.User.CanAccessItem<Record>());
不确定它是否会给您带来更好的可读性,但这是一个选项,并且在语法上更接近 Item 方法。
对于 Thing 等,只需添加类似的 UserFilters 方法即可。
作为奖励,您可以更进一步,添加常用的 PredicateBuilder 方法 And 和 Or
public static partial class ExpressionUtils
{
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
=> Expression.Lambda<Func<T, bool>>(Expression.AndAlso(left.Body,
right.Body.ReplaceParameter(right.Parameters[0], left.Parameters[0])), left.Parameters);
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
=> Expression.Lambda<Func<T, bool>>(Expression.OrElse(left.Body,
right.Body.ReplaceParameter(right.Parameters[0], left.Parameters[0])), left.Parameters);
}
所以如果需要的话你可以使用这样的东西
var items = this.GetAll().Where(this.User.OwnsItem().Or(this.User.CanAccessItem()));
在 Item 存储库中,或者
var records = this.GetAll().Where(this.User.OwnsItem<Record>().Or(this.User.CanAccessItem<Record>()));
在 Record 存储库中。
关于c# - 对象内对象的 Entity Framework LINQ 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55187496/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir