那麼,您的視圖模型需要提供擴展點。我假定數據爲視圖確實包含那些模塊的部分,並且由控制器以某種方式選擇的是:
public ActionResult Search(string text)
{
var model = seachService.Search(text)
// here either:
// 1. model contains .Modules collection and is populated by the service, or
// 2. controller does
var viewmodel = new SearchViewModel(model, modulesService.GetModulesFor(model));
// i.e. it adds modules-specific information to the view data
return View(model);
}
現在,特定的模塊數據由SearchViewModel轉換成一些視圖可以使用:
public interface IModuleSpecificViewPart
{
public IList<string> GetAdditionalLinksForUser();
public void RenderAdditionalInfo(Response response);
}
和你的觀點並
<%= Model.Results[i] %>
<% foreach (var module in Model.ModuleSpecific) { %>
<a href="<%=module.AdditionalLink%>">More</a>
<% module.RenderAdditionalInfo(Response); %>
<% } %>
或模塊的視圖模型可以提供局部視圖名稱等請注意,這是modules.GetModulesFor()和SearchViewModel該做的喬b - 例如,SearchViewModel可以執行IoC.GetInstanceByType(「ModuleViewModel」+ passedModule.GetType()。Name) - 自動獲取特定的模塊視圖模型類。
這是這個想法。這可能是我完全錯誤地理解你的問題 - 對不起,如果是的話。
一個建議,我個人發現,如果我試圖過於通用,它會增加項目的不必要的複雜性(我甚至會爲此添加wrote)。即是的,編譯時你的模塊是未知的,但你必須知道關於它們的一些事情!他們可能會添加鏈接到搜索結果 - 這是擴展點。一個人不能爲「任何事物」描繪延伸點,所以首先你需要列出你的具體情況。
感謝您的補充觀點。沒有簽出MEF是的,但瞭解它如何可以幫助一般。 – Dylan 2010-08-13 21:33:26