问题标签 [repository-design]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
2103 浏览

design-patterns - AOP 在通用存储库上使用装饰器模式实现

我正在尝试使用装饰器构建一个原型,将面向方面的编程应用于我的项目。我的项目的某些部分将使用通用存储库(用于简单的 CRUD),但最终我还将合并命令和查询处理程序(这些将执行特定任务,如 ProcessCustomerOrders 等)。此外,我想在这里举例说明的横切关注点是安全和日志记录。

另外,我知道我的示例代码不是使用装饰器模式,而只是我为该原型提供上下文的代码示例。

我知道还有其他方法可以实现 AOP(或横切关注点),例如代理或代码编织模式,但我不熟悉这些模式,因此不知道它们之间的权衡。

我在这里使用控制台应用程序只是为了展示如果我以链式方式“新建”它们会是什么样子。

我的问题是:

(1) 如何使用 Simple Injector(在引导类中)将其连接起来并且仍然保持顺序相同?

(2)这是装饰器模式的正确使用(因为我没有使用基础抽象或接口类或装饰器基础)?

(3) 是否有一种干净的方法可以在同一个存储库中使用多个 ILogger 服务的实现(例如 DatabaseLogger 和 ConsoleLogger),而无需注入两个不同的版本?

(4) 实际的日志记录是在 Repository 方法中实现的,并且 ILogger 服务被注入到 Repository 类中,但是有没有比硬连接记录器并仍然使用 Generic Repositories 更好的方法呢?

(5) 根据我在此原型中使用存储库的方式,我应该使用代理模式还是代码编织模式?

此外,欢迎对此设计提出一般批评。

原型代码:

更新基于@Steven 的回答:

装饰器和存储库的简单注入器 DI:

控制器调用的装饰器链的顺序应该是控制器(检查)> 安全(如果可以继续,则允许调用)> 回购(然后更新持久层)> 日志(到某个设施)> 并返回到控制器。

新的控制器类:

关于上述代码摘录的问题:

如果我想根据返回值在 Controller 中执行一些操作(例如从 Security Decorator 返回一个 bool),我是否必须修改 IGenericRepository 接口(以及因此 GenericRepository 类)?这在某种程度上意味着,由于 Repo 和 Security Decorator 类都实现了相同的接口,如果我想更改 Security 方法的返回值或参数,我还需要更改 Repository 方法吗?

另外,我现在是否只将 IGenericRepository 的安全实现传递给控制器​​?

此外,记录器已更改为如下所示:

上面,我只是调用了 Decoratee 并在上面添加了 Decorator 的功能。

最后是安全装饰器:

上面我不明白的是,记录器在哪里/何时被调用?

更新 2:

似乎现在应该像装饰器模式一样工作;感谢 Steven 提供的所有出色答案。

原型主要功能:

验证(安全)装饰器:

日志装饰器:

通用存储库:

控制器:

0 投票
2 回答
637 浏览

servicestack - 存储库设计:共享事务

我正在使用 ServiceStack 实现一个 Rest 服务。我们使用存储库模式并通过 IOC 将存储库自动连接到服务中。

目前,我们有一种简单的方法,即一个数据库模型与一个存储库配对。这意味着只要在一项服务中操纵多个实体,就不会使用事务边界。存储库是按顺序调用的:如果一个或多个步骤失败,则必须手动将数据库“回滚”到其初始状态。最坏的情况是,如果请求线程死掉,或者如果发生未经检查的异常(例如,OutOfMemoryException),数据库将处于不一致的状态。

我有一套假设的解决方案,但我认为没有一个是足够的:

  1. 打开连接并在服务级别启动事务。调用存储库,将连接传递给它们。这显然是错误的,因为它违反了所有 ddd 设计准则。关键是上层完全不知道具体的持久性。此外,它会弄乱单元测试。
  2. 让第一个存储库开始事务。将调用其他依赖存储库,但传递已打开的连接。这听起来也像是糟糕的设计。
  3. 定义聚合。因为我不是领域建模专家,所以我不是这个的忠实粉丝,而且我觉得通过引入聚合,我很容易引入设计错误。当前模型的一个优点是它很简单。

有人对这个问题有建议吗?提前致谢

0 投票
1 回答
380 浏览

asp.net-mvc - 实体框架通用存储库 SelectListItem

我正在我的 MVC 项目中实现通用存储库模式,并且正在关注本教程。 http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an- asp-net-mvc-应用程序

我喜欢为所有实体使用一个存储库的想法。我正在努力实现一个通用方法来为我的 ListBoxFor 返回一个 SelectListItem。

这是我在存储库中的方法。

如您所见,我想将人的名字和姓氏连接起来。我在这一点上很挣扎,因为我不确定这是否是最好的方法。

0 投票
1 回答
477 浏览

maven - Start to use artifactory

in company where I am working we are starting to use artifactory like tool of repositories managment, and then I'm reading the user guide of this tool. We started in the configuration creating a virtual repository, a few local and remote repositories. On the use guide i found the following thing:

Prevent disclosing sensitive business information derived from your artifact queries to whomever can intercept the queries, including the owners of the remote repository itself.

I saw that this could be avoided through

exclude pattern

functionality on the virtual repository. Can you give us some suggestion about this? What kinds of request we should avoided to do?

0 投票
2 回答
267 浏览

c# - Should repository services know about transaction information?

I'm using NHibernate in conjunction with Repository Design Pattern. In current task, I need to update an entity and also delete another entity in the same transaction. I though about declare ISession.BeginTransaction() in Repository's Services layter and pass it to Repository's method as following:

What I want to ask are

  1. Is it a good idea to let Repository's Service layer manage ISession as well as ITransaction and pass it around Associate Repository?
  2. If it's not a good idea, how should I change my design in order to perform those task in a same transaction without letting Repository's Service layer know about that transaction but still make Repository layer as lightweight as possible?

EDIT

For clarify, my EntityService is a business logic layer which depends on Repository to perform business logic then pass the result to presentation layer (a winform in my case). So that I thought that letting EntityService manage ISession and ITransaction will lead to tight coupling which I want to avoid in the first place.

EDIT 2

According to ptk93, I made some change to design as follow:

Does this design enough for de-coupling Repository and Repository Service?

0 投票
2 回答
978 浏览

c# - 存储库应该抛出自定义异常还是返回状态类型?

我有一个网络应用程序和一个将交易记录设置为“完成”的存储库。要求是在存储库级别检查该要求是否已被另一个用户设置为“完成”,如果是,请通知当前用户记录已被设置为“完成”。

我应该抛出一个自定义异常还是返回一个状态类(带有状态枚举和消息集合)?

存储库的调用者(一种服务)处理存储库调用并将结果包装到 DTO 到 UI ...

0 投票
2 回答
57 浏览

mercurial - 多个存储库作为一个在 mercurial

我很难弄清楚如何为我开始的 Django 项目组织一个 mercurial 存储库。这是当前配置:

对于我的工作流程,我更愿意将与项目和文档相关的活动分开。拥有两个专用存储库可能是解决问题的一种方法,但是如果我想要文档和项目,我应该克隆两个存储库,而我最好只克隆一个包罗万象的存储库,作为其中一个上图。

是否有可能获得我想要的,以及如何重新组织项目、文档和存储库?

0 投票
2 回答
88 浏览

c# - 包含不包括链接实体的实体的存储库

我正在尝试以下。我有一个返回存款列表的存储库。我还想获得与存款实体相关的实体(即合同)

我没有完成它。谁能帮我?

这是我的存储库:

我的存款实体如下所示:

所以当我得到我的存款清单时,我还想要链接的 EnvelopeTypeCarrierClassification 和链接的 CarrierCustomerContractVersion

0 投票
1 回答
13930 浏览

git - 是否有我应该在 Mac OS X 上初始化我的 Git 存储库的首选路径?

我应该在哪里构建我的存储库?我的教程存储库进入了根目录,但我认为既然我将对我的生产站点进行测试,我应该将它们构建在~/sites/.

这可能看起来微不足道,但我见过的大多数 Git 介绍并没有真正指定特定的位置。

0 投票
1 回答
227 浏览

laravel-4 - laravel 4 如何实现多态关系的存储库

我有车辆类 morphTo(Bus) 和 MorphTo(SUV) 和 MorphTo(Sedan)。

在 VehicleController 中保存时,我调用了 vehicleRepositoryInterface、vehicleRepository。

如何保存巴士、SUV 或轿车数据?

我的车辆类

我的车辆存储库类如何处理带有 morphTo 关系的 if(Vehicle Type is Bus / SUV / Van)?