在处理大型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