我认为对存储库模式的更大担忧是您违反了单一责任原则。你的类应该有一个职责,比如操作数据库中的数据。您应该有一个不同的类来处理文件 IO,并且您可以将一个类中的函数向上分组。
更改一个类应该只有一个原因,处理文件 IO 和 db 调用的存储库类将有两个。更改文件系统布局或更改数据库。
编辑
为了解决您的编辑问题,这是我将如何在 MVC 场景中实现它(这也假设您正在使用某种依赖注入来使生活更轻松)。
// 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)
}
}
然后,您可以根据具体实现对您的特定需求有意义的任何接口来实现 IProductImageManager 和 IProductRepository。