- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在关注这个网站:http://deviq.com/repository-pattern/
其中有一个使用数据库上下文的存储库模式示例。我试图用一个列表来实现这个通用的 Repository 类(我想要 Repository 类。这是我的要求)。但是,我在使用 Find 方法时遇到了问题。
public class Repository<T> : IRepository<T> where T : class
{
private List<T> context;
virtual public T Find(int id)
{
// I can't figure out a way to make this work with the list of a generic type
}
}
是否有可能仅使用一个 ID 参数在 List.Find() 中创建谓词?我猜不会,但有哪些选择?
最佳答案
如果您无法控制 T 的类型以应用接口(interface),另一种选择是强制您的实现者进行艰苦的工作。
public abstract class Repository<T> : IRepository<T> where T : class
{
private List<T> context;
public virtual public T Find(int id)
{
return context.FirstOrDefault(x => GetId(x) == id);
}
public abstract int GetId(T entity);
}
一个示例实现可能是
// entity
public class Stooge
{
public Stooges MoronInQuestion {get;set;}
public double MoeEnragementFactor {get;set;}
public void PloinkEyes() { /*snip*/ }
public void Slap() { /*snip*/ }
public void Punch() { /*snip*/ }
// etc
}
// enum for an Id? It's not that crazy, sometimes
public enum Stooges
{
Moe = 1,
Larry = 2,
Curly = 3,
Shemp = 4,
Joe = 5,
/* nobody likes Joe DeRita */
//CurlyJoe = -1,
}
// implementation
public class StoogeRepository : IRepository<Stooge>
{
public override int GetId(Stooge entity)
{
if(entity == null)
throw new WOOWOOWOOException();
return (int)entity.MoronInQuestion;
}
}
关于c# - C#中以list为核心的Repository模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34075311/
在过去的几个月里,我一直在使用 Bzr 对我的项目进行版本控制。我是唯一的开发人员,目前我只有一个本地项目目录中的所有内容,我提交并同步到 DriveHQ。 我现在想到了一些可能会打破这条主线的大规模
我在一个多模块项目中使用 Maven 3.2.3。我想生成一个 checkstyle 和 findbugs 报告,所以我配置了以下内容:
我注意到 Repository 通常通过以下任一方式实现: 方法一 void Add(object obj); void Remove(object obj); object GetBy(int id
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 5年前关闭。 Improve thi
这是我的设置的详细信息: Gitlab 版本:5.2操作系统:Centos 6.3我在创建新项目 (/projects/new) 时导入现有存储库。 创建了一个新的 EMPTY 项目,但是没有导入存储
我的文件夹结构如下: repo1 | |---file1 |---fold1 | |---file2 |---repo2 | | |---file3 假设我有两
假设我有一个 TeacherRepository,需要根据下面的代码使用 CourseRepository。 Teacher 和 Course 形成多对多的关系。教师和类(class)不形成聚合。您会
我要同样的 repositories我在 buildscript.repositories 中指定的阻止与依赖项相同的存储库 repositories堵塞。请看我的例子: 正常 buidldscrip
是否使用 @EnableJpaRepositories 或 jpa:repositories(在 xml 上)让开发人员不要使用 Spring 的 @Repository 标签?当我查看 Spring
我是 git 和 Github 的新手。我已经了解了很多术语(推送、 pull 、提交、分支等),但我将主要使用通俗的说法来解释我最初的期望。 我假设过程是: 1.) Create a git rep
安装 Nexus Repository Manager OSS 3 后,我没有看到用于通过网页上传工件的选项Artifact Upload。 在 Nexus Repository Manager OS
CMS 和 DMS 有什么区别?两者都存储日期,可以访问数据,它们有什么不同?可以使用 apache Jack Rabbit 代替 Alfresco 吗? 最佳答案 我会根据管理数据的可变性来区分这两
在我的earlier question我问的是如何为使用 EF 等 ORM 框架构建的大型应用程序实现存储库/工作单元模式。 我现在无法解决的一个后续问题是将包含业务逻辑的代码放在哪里,但仍然足够低级
我正在尝试为 nuget git 存储库(我刚刚从中克隆)创建 pull 请求。我已经进行了本地提交。 但是当我尝试创建 pull 请求时,出现以下错误: Could not find any rel
我已经看到了下面的问题。 git diff between cloned and original remote repository 我的问题是如何在 SourceTree 中看到这个差异结果(而不
我在通过 Subversion (SVN) 中的 checkin 自动在 Review Board 中创建新的评论条目时遇到了困难。我创建了一个提交后 Hook ,当手动运行时会出现以下异常: Fai
我在尝试集成 Spring Data 时遇到此错误。完整的堆栈跟踪是 nested exception is org.xml.sax.SAXParseException; systemId: http
通知:https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-repository-email.html 触发器:https://
我正在学习 Laravel 中的存储库设计模式,我正在使用 https://github.com/andersao/l5-repository去做这件事。 但是在我将文件夹 prettus 复制/粘贴
我最近开始了一个使用现有数据库(Oracle)和 MVC 4 的项目。已经进行了很多编码..但是代码中没有“策略”..只有 DB -> ORM -> Controller。因此,我正在尝试为开发添加一
我是一名优秀的程序员,十分优秀!