배경:
웹샵 애플리케이션에는 모든 종류의 제품 목록을 만들기위한 여러 부분보기가 있습니다. 주요 리팩토링 노력에서 우리는 중복 된 코드를 모두 피하기 위해 이러한 부분 중 하나를 제외하고 대부분을 제거하려고합니다.
목록은 <ul> <li>
CSS에서 지정한 디자인 속성 으로 간단 합니다.
다음과 같은 이미지 URL에 따라 이미지를 적절한 크기로 동적으로 조정하는 기존 프레임 워크가 있습니다.
http://images.shop.com/data/product/160x200/5155-5ca8e.jpg
이 이미지는 160x200으로 조정됩니다.
우리는 가능성이 있기 때문에 클라이언트에게 필요한 것보다 더 큰 이미지를 요청하도록 요청하고 싶지 않습니다.
질문:
이미지 크기 선택 논리에 가장 적합한 위치는 무엇입니까?
또는 다른 방식으로 표현합니다. img src
이 맥락에서 속성 은 어느 시점에서 결정되어야 하는가?
이런 종류의 논리에는 여러 가지 잠재적 인 위치가 있습니다.
# 1 또는 # 2가 아마도 가장 의미가 있다고 생각합니다. 그렇다면이 모델,보기 또는 컨트롤러 논리입니까? 그들 모두는 어떤 종류의 이점이 있지만 실제로는 적절하지 않은 것 같습니다.
제품 목록을 렌더링하는 부분은 모든 종류의 다른 제품 목록을 렌더링하는 데 사용할 수 있습니다. 전체 화면 이미지 슬라이더에서 작은 10x10 픽셀 썸네일까지. 그리고 가능한 가장 큰 목록을 수용하기 위해 축소판이 전체 화면 이미지를 요청하도록 강제하고 싶지 않습니다.
The current logic is basically view-logic. By calling the partial list for the place in question.
(This is a distilled version of our current approach)
Landingpage.cshtml
<ul>
Html.RenderAction("ListRandom", "Product", new { Category = feed.catId});
</ul>
ProductController.cs
public PartialViewResult ListRandom(int? CategoryId)
{
var model = new ProductListModel { ProductsRandomGet(CategoryId) };
return PartialView("_ProductList", model);
}
_ProductList.cshtml
<li>
<a href="@item.href">
<img alt="@item.Name"
src="data/product/35x44/@item.Image" /> /* Image Size */
<div class="listItemInfo">
@item.Name
@item.Price
</div>
</a>
</li>
While this works, it leads to a lot of duplicate html for all the different kinds of product lists. The html for the different lists are almost identical, and all the differences can be put in CSS.
But not the image src property
I don't think the controller is appropriate, because the controller doesn't do display logic. That's the view's responsibility. It's still not clear to me from your question where the decision is currently made which image size to include, where the image sizes come from (do you use a handful of fixed sizes, or can they vary per image?) or how many scenarios there are.
예를 들어 부분보기를 사용하여 다음과 같이 이미지 만 표시 할 수 있습니다 (예 ProductImageSmall
:보다 재사용 가능하지만 덜 구체적 임 ProductImageShoppingCart
).
<img src="data/product/35x44/@item.Image" [...] />
그런 다음 상위 뷰에서 항목 컬렉션을 렌더링하는 데 사용할 부분을 결정할 수 있습니다.
모델에 해상도를 포함 할 수도 있습니다.
<img src="data/product/@(item.ImageWidth)x@(item.ImageHeight)/@item.Image" [...] />
또는 그 치수를 ViewBag
대신 붙입니다.
목록에 대한 모든 방법에 대한 찬반 양론이 너무 많으므로 더 구체적인 답변을 원하면 질문 범위를 더 좁히십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다