那么,您的视图模型需要提供扩展点。我假定数据为视图确实包含那些模块的部分,并且由控制器以某种方式选择的是:
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