ASP.NET MVC ViewModel 自动生成
我的 MVC 项目中有大量 ADO.NET 实体。我打算直接从我的视图/控制器使用这些实体...但是,我认为最好使用 ViewModel,它更准确地反映视图的需求。
我正在寻找一种可以从现有实体自动生成 ViewModel 的方法,即从现有成员自动生成包装器或适配器模式...或者循环遍历某个实体的公共属性的 T4 模板ViewModel 的实体和输出属性...然后我可以删除不需要的属性或创建聚合视图模型等。
我似乎无法在任何地方找到从现有的包装器或适配器模式类自动生成的方法类型?
这个想法是在运行时使用 AutoMapper 在 ViewModel 和 Entity 之间进行映射。
谢谢
I have a load of ADO.NET Entities in my MVC project. I was going to use these entities directly from my views/controllers... however, I think it's probably best to use ViewModels which more accurately reflect what the View requires.
I'm looking for a way that I can auto-generate a ViewModel from an existing Entity, i.e., auto-generate the wrapper or adapter pattern from an existing member... Or a T4 template that would loop through the public properties of an Entity, and output properties for the ViewModel... then I can delete the properties I don't need or create aggregate view models etc.
I cannot seem to find anywhere a way to auto-gen a wrapper or adapter pattern class from an existing type?
The idea is then at runtime, use AutoMapper to map between the ViewModel and the Entity.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 AutoMapper 将域模型转换为视图模型。有一个 Jimmy Bogard 的精彩帖子解释了如何将其集成到控制器操作中。
You could use AutoMapper to convert from your domain model to a view model. There's a great post from Jimmy Bogard explaining how you could integrate this within your controller actions.
这会有所帮助。它实际上是用于现有实体类型的元数据生成。但您也可以使用它来生成带有数据注释的干净视图模型。也许需要一点修改。
http://weblogs.asp.net/rajbk/archive/2010/05/04/a-basic-t4-template-for-generating-model-metadata-in-asp-net-mvc2.aspx
That can help. It is actually for metadata generation for existing entity types. But you can use it to generate clean view models with data annotations as well. Maybe with a little modification.