使用存储库时,类型是否可以返回存储库用于测试现有实体的 Func?
例如,给定一个具有方法的工厂,
public static T Save<T>(T item) where T : Base, new()
{
/* item.Id == Guid.Empty therefore item is new */
if (item.Id == Guid.Empty && repository.GetAll<T>(t => t.Name == item.Name))
{
throw new Exception("Name is not unique");
}
}
如何创建 Base
的属性(例如 MustNotAlreadyExist
),以便我可以将上面的方法更改为
public static T Save<T>(T item) where T : Base, new()
{
/* item.Id == Guid.Empty therefore item is new */
if (item.Id == Guid.Empty && repository.GetAll<T>(t.MustNotAlreadyExist))
{
throw new Exception("Name is not unique");
}
}
public class Base
{
...
public virtual Expression<Func<T, bool>> MustNotAlreadyExist()
{
return (b => b.Name == name); /* <- this clearly doesn't work */
}
}
,然后如何重写 MustNotAlreadyExist在帐户:基础
public class Account : Base
{
...
public override Expression<Func<T, bool>> MustNotAlreadyExist()
{
return (b => b.Name == name && b.AccountCode == accountCode); /* <- this doesn't work */
}
...
}
For example given a Factory with a method
public static T Save<T>(T item) where T : Base, new()
{
/* item.Id == Guid.Empty therefore item is new */
if (item.Id == Guid.Empty && repository.GetAll<T>(t => t.Name == item.Name))
{
throw new Exception("Name is not unique");
}
}
how do I create a property of Base
(say MustNotAlreadyExist
) so that I can change the method above to
public static T Save<T>(T item) where T : Base, new()
{
/* item.Id == Guid.Empty therefore item is new */
if (item.Id == Guid.Empty && repository.GetAll<T>(t.MustNotAlreadyExist))
{
throw new Exception("Name is not unique");
}
}
public class Base
{
...
public virtual Expression<Func<T, bool>> MustNotAlreadyExist()
{
return (b => b.Name == name); /* <- this clearly doesn't work */
}
}
and then how can I override MustNotAlreadyExist in Account : Base
public class Account : Base
{
...
public override Expression<Func<T, bool>> MustNotAlreadyExist()
{
return (b => b.Name == name && b.AccountCode == accountCode); /* <- this doesn't work */
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
如果任何记录与谓词匹配,Any() 方法将返回 true。可能有人会说,在保存之前检查记录是否存在不属于存储库的责任。
更新:
CodeProject 上有一篇很棒的文章,描述了实体框架的通用存储库:
http://www .codeproject.com/KB/database/ImplRepositoryPatternEF.aspx
这可以应用于非实体框架数据上下文。以下摘录提供了一种非常灵活的方法,通过接受字段名称、值和键值来检查现有值。您可以将此应用于任何实体类型,并在尝试保存之前使用它来检查实体是否存在。
Try this:
The Any() method will return true if any record matches the predicate. It could be argued that it is outside the responsibility of the repository to check for presence of a record before saving.
UPDATE:
There is a great article on CodeProject that describes a generic Repository for Entity Framework:
http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx
This could be applied to a non-Entity Framework data context. Here is an excerpt that provides a very flexible method for checking for an existing value by accepting the name of a field, a value, and a key value. You can apply this to any Entity type and use it to check for the presence of an entity before attempting a save.
我不确定您的问题是否可以解决,因为您需要访问存储库和要检查的新项目。要检查的新项目在单独的方法中不可用。
但是,您可以将调用外包给 GetAll,以便您的代码变得类似于
(未测试)
I am not shure if you problem is solvable since you need to access both the repository and the new item to be checked. The new item to be checked is not available in a seperate method.
However you can outsource the call to GetAll so that your code becomes something similar to
(not tested)
好的,这就是答案,这是 Dave Swersky 发布的代码和一些常识的组合。
本质上,通过为特定类型实现 IUniqueable 接口,我可以为每种类型返回不同的表达式。一切都好:-)
OK, here is the answer, this is a combination of the code posted by Dave Swersky and a little but of common sense.
Essentially by implementing the
IUniqueable
interface for a specific type I can return a differentExpression
for each type. All good :-)