- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在实现存储库模式的 ASP.NET MVC 应用程序中,我很好奇如果它们仍然与给定存储库的一般焦点相关,是否适合将非数据相关方法放置在存储库中。例如,假设 ProductsRepository 具有添加和删除 ProductImages 的方法,这些方法在数据库和本地文件存储中也有部分表示。如果需要删除 ProductImage,我们需要使用存储库方法从数据库中删除一行,还需要从存储介质中删除与该图像关联的文件。 IO 操作是否属于存储库,还是有更合适的位置?
在我刚才描述的情况下,我一直在做的一件事是在我的存储库中提供静态方法,这些方法通过使用存储在数据库中的文件名和预定义的目录模式以编程方式生成它,为我提供给定 ProductImage 的路径.这超出了存储库的预期用途吗?
编辑
如果这样的操作不属于存储库,那么在 MVC 模式中应该去哪里?在我看来,在 Controller 和 Repository 之间有另一个层可能是有意义的,它根据需要调用 Repository 并且可以从 Controller 静态调用。
最佳答案
我认为对存储库模式的更大担忧是您违反了单一责任原则。你的类应该有一个职责,比如操作数据库中的数据。您应该有一个不同的类来处理文件 IO,并且您可以将一个类中的函数向上分组。
更改一个类应该只有一个原因,而处理文件 IO 和 db 调用的存储库类将有两个。更改文件系统布局或更改数据库。
编辑
为了解决您的编辑问题,这是我将如何在 MVC 场景中实现它(这也假设您正在使用某种依赖注入(inject)来使生活更轻松)。
// Controller class
public class ProductsController
{
private IProductService _productService;
public ProductsController(IProductService productService)
{
_productService = productService
}
public void RemoveImage(int productId, int imageId)
{
_productService.RemoveImage(productId, imageId)
}
}
public class ProductService: IProductService
{
private IProductRepository _productRepository;
private IProductImageManager _imageManager;
public ProductService(IProductRepository productRepository, IProductImageManager imageManager)
{
_productRepository = productRepository;
_imageManager = imageManager;
}
public void RemoveImage(int productId, int imageId)
{
// assume some details about locating the image are in the data store
var details = _productRepository.GetProductImageDetails(productId, imageId);
// TODO: error handling, when not found?
_imageManager.DeleteImage(details.location);
_productRepository.DeleteImage(productId, imageId)
}
}
关于design-patterns - 什么属于存储库,什么不属于?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1300773/
这个问题在这里已经有了答案: Difference between / and /* in servlet mapping url pattern (5 个回答) 4年前关闭。 web.xml 中的/
Scala 具有支持模式匹配中析取的语言功能(“模式替代”): x match { case _: String | _: Int => case _ => } 但是,如果审查满足 P
解释我的问题: 类别:玩具 特质 1:说话像男性 特质2:说话像女性 我能否在运行时更改 Toy 的行为(特征),以便有时同一个对象说话像男性,有时同一个对象说话像女性? 我想在运行时改变说话行为。
我已经能够找到很好的资源,这些资源告诉我 Java API 中的 MouseAdapter 没有使用适配器模式。问题是:MouseAdapter 是否实现了某种模式? 我知道它的作用:它为 Mouse
我有兴趣了解有关模式识别的更多信息。我知道这是一个广泛的领域,所以我将列出一些我想学习处理的特定类型的问题: 在看似随机的字节集中查找模式。 识别图像中的已知形状(例如圆形和正方形)。 注意给定位置流
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
所以,问题很简单:在 awk 中,if (var ~/pattern/) 是否与 if (var ~ "pattern") 相同? 我已经对 csv 进行了一些基本测试,两者似乎都产生了相同的结果..
我的问题是 this 的 Scala (Java) 变体Python 上的查询。 特别是,我有一个字符串 val myStr = "Shall we meet at, let's say, 8:45
我最近一直在研究正则表达式并注意到了这一点。 Pattern pNoEmbed = Pattern.compile("[ a-z]+", Pattern.CASE_INSENSITIVE); Patt
在研究大型应用程序的 C++ 源代码时,我发现了这种模式(该示例的语法可能很粗略,但基本细节都在那里): class A : X friend B; B *parent; ...
有人可以举一个“中介者模式”在现实世界中有用的用例吗? 最佳答案 Mediator是一种添加第三方对象以控制一组(2 个或更多)对象之间交互的方法。 您能找到的最简单的示例是 Chat Room例如,
尝试编译以下代码片段时: type 'a frame = Empty | Frame of string * 'a * 'a frame let rec searchFrame f s = match
目标 我的目标是获得一个 servlet 过滤器来处理对主页的请求,然后再将它们转发到 index.jsp。 问题 我无法让过滤器接收来自“/”的请求。它的 URL 模式是 / 相反,对该模式的请求最
这个问题已经有答案了: Difference between / and /* in servlet mapping url pattern (5 个回答) 已关闭 6 年前。 我已经设置了一个具有此
第 6 章(代码重用模式)中有以下示例: // the parent constructor function Parent(name) { this.name = name || 'Adam
Pattern类中的pattern()方法和toString()方法有什么区别? 文档说: public String pattern() Returns the regular expression
我有脚本 here并且 ng-pattern 工作正常,因为 scope.subnet 仅在输入匹配模式后才显示在输出中。但是如果 ng-pattern 不匹配,ng-show 不会显示任何错误
我想知道为什么当提供相同的正则表达式和相同的字符串时,java regex pattern.matcher() 和 pattern.matches() 的结果会不同 String str = "hel
This SO answer引用“患有模式综合症的小男孩”。虽然我可以通过上下文推断出一些含义,但我并不完全理解。 “有模式综合症的小男孩”的良好定义是什么? 最佳答案 它只是意味着寻找将模式注入(i
我有以下微服务架构的用例。 我的问题是,在当前情况下,我有 3 个微服务和一个 APIGateway。 最后,网关必须在聚合(组合)来自 3 个服务的数据之前进行大量查询。因为这 3 个微服务只提供基
我是一名优秀的程序员,十分优秀!