I need advice on what I need to do to make the following two blocks of code reusable. I have to produce another table of funds and while I'm doing that, I'd like to create FundsTable.ascx partial view that all Views that need to display a fund table can use.
// Inherits="System.Web.Mvc.ViewPage<CompanyViewModel> // this is a company
<%foreach (var fund in Model.PageFunds){%>
<% foreach (var shareClass in fund.ShareClasses) {%>
<tr class="shareclass">
<td>
// displays an image if the ViewModel's Company is not unlisted
<%= Html.TearsheetImage((Model.Company.ListingType != ListingType.UNLISTED))%>
</td>
</tr>
<% } }%>
and
// Inherits="System.Web.Mvc.ViewPage<GroupViewModel> // this is a group of companies
<%foreach (var fund in Model.PageFunds){%>
<% foreach (var shareClass in fund.ShareClasses) {%>
<tr class="shareclass">
<td>
// displays an image if any Company in the ViewModel's
// List<Company> is not unlisted
<%= Html.TearsheetImage(
(Model.Companies.WithCompanyId(fund.Company.Id) != ListingType.UNLISTED))%>
</td>
</tr>
<% } }%>
I think I need to abstract the differences away somewhere, but I'm not sure where to put it. Is there any rule-of-thumb I should be following here?
Should both CompanyViewModel
& GroupViewModel
implement an interface that decides whether the item is Unlisted? Also, what type should my FundTable.ascx be? I was thinking both CompanyViewModel
& GroupViewModel
could extend FundViewModel
(or something) and I could make FundTable a ViewUserControl<FundViewModel>
but I don't think that'll work because the functionality needed to determine whether to display the image needs to come from CompanyViewModel
& GroupViewModel
independently.
Plus, the more I think about this the more I'm cunfusing my self! Any ideas or suggestions? Thankss