多层 ASP.NET MVC 2 应用程序中的图像处理
我正在寻求一些帮助,以找到一种好的方法/架构来处理 ASP.NET MVC 2 应用程序中的图像。该应用程序是中间层 Web 服务应用程序的客户端,封装了我们的域逻辑和规则。这些图像存储在只能通过 Web 服务访问的后端数据库中。
为了便于讨论,我们将使用具有关联图像的产品的经典案例。每当我显示有关产品的信息时,我也会显示图像。因此,例如,当管理员查看产品列表进行编辑时,我会将图像和产品名称显示为超链接。编辑时,用户可以看到现有图像并上传替换图像。当用户浏览产品列表或查看“购物车”中的商品时,我还会显示该图像。
不用说,我必须获取大量的图像数据。 (是的,缓存将是解决方案的一部分,但不是我的问题的一部分。)
我的第一个任务是创建用于编辑产品数据的控制器和视图,其中包括上传图像的功能。我遵循 Pro ASP.NET MVC 2 Framework 中描述的方法,其中我将图像元素的 src 设置为返回图像数据的控制器操作。控制器在其构造函数中接收对 ProductManagementServiceAgent 的引用,并委托给处理对 Web 服务的调用的代理。这很有效,但是,当然,这意味着两次调用服务来显示信息。
接下来,我必须向管理员显示带有图像的产品列表。在这种情况下,这不是问题,因为我使用具有相同操作的相同控制器,因此我可以使用相同的方法。不幸的是,现在我正在对该服务进行“n+1”次调用。
当我想知道如何最好地处理需要显示图像的所有其他用例时,我感到困惑。例如,在“购物车”内。我的 ShoppingCartController 是否也应该引用 ProductManagementServiceAgent 并使用相同的方法来检索产品图像?这意味着任何显示产品图像的控制器都必须引用代理,是吗?
困扰我的第一件事是,如果我真的不需要,我不喜欢将多个依赖项注入到一个类中,并将构造函数中的长参数列表视为臭代码(告诉我该对象试图做太多事情) )。另外,ProductManagementServiceAgent 实际上是用于管理 UI 的,所以也许不同的代理会更好?还是采取不同的方法?
我必须认为其他人已经绘制了这个领域的地图,所以我很感激任何能让我走上正确方向的智慧。
I'm looking for some help wrapping my head around a good approach/architecture to handle images in my ASP.NET MVC 2 application. The application is a client for a middle-tier web service application that encapsulates our domain logic and rules. The images are stored in a back-end database that is only accessible through the web service.
For the sake of this discussion, we'll use the classic case of a Product which has an associated image. Whenever I display information about the Product, I also show the image. So, for instance, when viewing a list of Products to an admin for editing, I will show the image and the name of the product as a hyperlink. when editing, the user can see the existing image and upload a replacement. I also show the image when users browse lists of Products or look at what items are in their 'shopping cart'.
Needless to say, I have to obtain the image data a lot. (And, yes, caching will be a part of the solution but not part of my question.)
My first task was to create the controller and views used to edit Product data which includes the ability to upload the image. I followed the approach described in Pro ASP.NET MVC 2 Framework where I set the image element's src to a controller action which returns the image data. The controller receives a reference to the ProductManagementServiceAgent in its constructor and delegates to the agent which handles the call to the web service. This works great but, of course, it means two calls back to the service to display the information.
Next I have to display the list of Products, with images, to the admin. In this case it's not a problem because I'm using the same controller with the same action, so I can use the same approach. Unfortunately, now I'm making 'n+1' calls to the service.
My quandry comes when figuring out how best to handle all of the other use cases where I need to display the image. Inside the 'shopping cart', for instance. Should my ShoppingCartController also reference ProductManagementServiceAgent and use the same method to retrieve the product image? This would mean any controller that displays a product image would have to reference the agent, yes?
The first thing that bothers me about this is that I don't like injecting multiple dependencies into a class if I really don't need to and see long parameter lists in constructors as smelly code (tells me the object is trying to do too much). Plus, the ProductManagementServiceAgent is really intended for the admin UI, so maybe a different agent would be better? Or a different approach all together?
I have to think that others have already charted this territory, so I appreciate any wisdom to set me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有专用的图像控制器。
它将有一个依赖项 - ProductManagementServiceAgent。
在服务器端和客户端缓存它以最大程度地减少请求。
Have a dedicated controller for images.
It will have one dependency - the ProductManagementServiceAgent.
Cache it both server side and client side to minimise requests.
实际上,我提出了一个未发布到该网站的建议,因此我无法给予适当的信任。简而言之,当需要图像时,我们在控制器上公开其他操作。例如,我们的 ProductController 有一个 ProductImage 操作方法,它接受产品 id 并返回图像。看来可以满足我们的需求了。
I actually went with a suggestion not posted to this site so I can't give proper credit. In a nutshell, we expose additional actions on our controller when an image is required. So, for instance, our ProductController has a ProductImage action method that accepts the product id and returns the image. It seems to satisfy our needs.