21个具有相同结构的实体。当需要为这些实体创建 CRUD 时我应该做什么
我有 21 个具有相同结构的实体。 属性也一样。
每个实体都包含以下属性:
区域类型
身份证号
处于活动状态
语言 ID
TXT
模块类型
身份证号
处于活动状态
语言ID
TXT ...
执行通用 Crud 需要什么。我已经知道我需要创建一个通用存储库。我的问题是执行一种通用的 ViewModel。
如何为创建表单创建通用视图。
我不知道我需要在视图的继承中传递什么才能成为通用。
... Inherits="System.Web.Mvc.ViewPage<...Dont know>"
有什么想法吗?
I Have 21 entities with the same structure.
Same Attribute too.
Every entity contains these Attributes :
AreaType
ID
IsActive
LangID
TXT
ModuleType
ID
IsActive
LangID
TXT
...
What I Need to perform a generic Crud. I already know that I need to create a generic repository. My problem is to perform a kind of generic ViewModel.
How can I create a generic View for the Create Form.
I Dont know what I need to pass in the Inherits of the view to be Generic.
... Inherits="System.Web.Mvc.ViewPage<...Dont know>"
Any Idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此问题的常见方法是使用 ViewModel。您可以在此处创建要用作强类型视图中的模型的特定类。这些类不是 EF 创建的类。 ViewModel 类可以有一个封装公共字段的公共基础。在数据访问层中,您需要在 ViewModel 类和 EF 类之间移动数据。像 AutoMapper(来自 CodePlex)这样的东西确实可以很好地减少(如果不是消除的话)所有繁琐的“左手右手”编码。
A common approach this problem is to use ViewModels. This is where you create specific classes to be used as the models in your strongly typed views. These classes would not be the ones created by EF. The ViewModel classes can have a common base that encapulate your common fields. In your data access layer you would need to move data between your ViewModel classes and your EF classes. Things like AutoMapper (from CodePlex) work really well to reduce, if not eliminate, all of the the tedious "left-hand right-hand" coding.
对 MVC 不太熟悉,但是(假设它适合您的层次结构),我认为您可以创建一个包含您需要的属性的抽象类,例如
然后用您的普通类(AreaType 等)实现它,例如
:使视图使用抽象类。
Not too familiar with MVC, but (assuming it fits in with your hierarchy), I think you could create an abstract class which contains the properties you need, e.g.
Then implement that with your normal classes (AreaType and so on), e.g:
and make the view use the abstract class.
一种想法是简单地更改您的基础表。将 AreaType 和 ModuleType 合并到一个“WhateverType”表中,该表包含一个准确标识其类型的字段。然后,当您对类进行代码生成时,您将只有一个类需要处理。
但是,还有其他问题,只有在您的应用程序有意义时才应该这样做。
One idea is to simply change your underlying tables. Combine AreaType and ModuleType into a single "WhateverType" table that contains a field identifying exactly what type it is. Then when you codegen your classes you'll have exactly one class to deal with.
However, there are other concerns and you should only do this if it makes sense in your application.