在處理大型ASP.NET MVC應用程序之後,我發現最有效的方法,同時最小化運行時轉換基於使用泛型來模仿視圖的嵌套結構。基本上看,獲得他們自己的數據類型。通常這些是域對象或包含元數據的域對象的集合。這些類型的通用版本在所有可能的母版頁上都可用,並且使用一個類型參數來定義與母版頁相關的數據。
public class Car {
// can be used as a model
}
public class CarCollection: Collection<Car> {
public BodyTypes BodyType {get;set;}
public Colors Color {get;set;}
// can also be used as a model
}
public interface ILayoutModel<TLayout> {
TLayout LayoutModel {get;set;}
}
public class CarView<TLayout>: Car, ILayoutModel<TLayout> {
// model that can be used with strongly-typed master page
}
public class CarCollection<TLayout> : CarCollection, ILayoutModel<TLayout> {
// model that can be used with strongly-typed master page
}
public class LayoutAData {
// model for LayoutA.master
}
public class LayoutBData {
// model for LayoutB.master
}
也有可能反轉一般的煩躁,但由於認爲決定了佈局,視圖數據應支配在我看來,佈局數據。 LayoutA.master將從ViewMasterPage<ILayoutModel<LayoutAData>>
派生和LayoutB.master將從ViewMasterPage<ILayoutModel<LayoutBData>>
派生。這使得視圖數據和佈局數據以一致,強類型和靈活的方式分開。
*嘆*在事後,這是令人尷尬顯而易見。 – nathanchere 2012-01-16 04:57:28