如何处理实体框架5代码和MVC中的派生类型?

时间:2022-02-22 06:54:11

I am working on a set of classes to represent an Asset Register in Entity Framework. For the sake of keeping the question simple, I have built three classes:

我正在开发一组类,以在实体框架中表示资产注册。为了简化问题,我构建了三个类:

Asset

资产

Contains fields which are applicable to all asset types, e.g. Asset ID, Serial Number, Purchase Date

包含适用于所有资产类型的字段,例如资产ID、序列号、购买日期

Computer

电脑

Derives from Asset, adds on fields specific to Computer assets, e.g. MAC Address, Hostname

源自资产,在特定于计算机资产的字段中添加,例如MAC地址、主机名。

Monitor

监控

Derives from asset, adds on fields specific to Monitor assets, e.g. Size

派生自资产,添加用于监视资产的字段,例如大小


In pure C# I can instantiate these classes by calling, for example:

在纯c#中,我可以通过调用实例化这些类,例如:

Computer DansComputer = new Computer(){ AssetID=12345, SerialNumber="12345", Hostname="DanComputer"};

The UI that I am using to capture new assets requires that the user enter base data about their new asset (from the Asset) class and then discriminate to say, for example, they are creating a Computer asset. I'm wondering if this is less than elegant... In it's current form, the pseudo-code to perform an Insert, regardless of the type of asset would be:

我用来捕获新资产的UI要求用户输入关于他们的新资产(来自资产)类的基础数据,然后区别对待,例如,他们正在创建一个计算机资产。我想知道这是不是不够优雅……在它的当前形式中,执行插入操作的伪代码,不管资产的类型是什么:

  1. Capture data about the Asset class
  2. 捕获关于资产类的数据
  3. Commit these data via EF to the Asset table
  4. 通过EF将这些数据提交到资产表
  5. Redirect to a view, dependent on the Asset type, to capture type specific data, e.g. new Computer view
  6. 重定向到视图,取决于资产类型,以捕获类型特定的数据,例如新的计算机视图
  7. Retrieve the previously committed Asset, cast it to appropriate type (e.g. Computer) and project Computer-specific fields to the new object.
  8. 检索先前提交的资产,将其转换为适当的类型(例如,Computer),并将计算机特定的字段投射到新对象。
  9. Commit the Computer to the database.
  10. 将计算机提交到数据库。

I don't like the commit between views there.

我不喜欢两种观点之间的冲突。

Would a "valid" approach to be one whereby the user selects the type of Asset they wish to add (e.g. Computer, Printer) and then a view to create a new Computer/Printer is displayed? This solution sounds less than ideal to me, because:

“有效”方法是用户选择要添加的资产类型(如计算机、打印机),然后显示创建新计算机/打印机的视图?这个解决方案听起来不太理想,因为:

  1. I would need to draw up an MVC view for each type of asset, which seems to violate the DRY principle to me...
  2. 我需要为每种类型的资产绘制一个MVC视图,这似乎违反了DRY原则……
  3. I would need to manually keep a list of all the possible asset types and display some kind of UI to allow the user to select which type they want to use.
  4. 我需要手工保存所有可能的资产类型的列表,并显示某种UI,以便用户可以选择他们想要使用的类型。

I can't, however, think of any other way to make this UI.

但是,我不能想到其他方法来创建这个UI。

Am I missing something really obvious here?

我是否遗漏了一些很明显的东西?

1 个解决方案

#1


1  

I think the easiest way is to change your steps:

我认为最简单的方法就是改变你的步骤:

  1. Give user a choice: which asset is it
  2. 给用户一个选择:它是哪个资产
  3. Reditect to proper action for this asset
  4. 重新考虑对这项资产采取适当的行动
  5. Render view with common parts for Asset (using for example partial view) and specified for selected one
  6. 用资产的公共部分(例如部分视图)呈现视图,并为选定的部分指定
  7. Retrieve data and commit it to database.
  8. 检索数据并将其提交到数据库。

#1


1  

I think the easiest way is to change your steps:

我认为最简单的方法就是改变你的步骤:

  1. Give user a choice: which asset is it
  2. 给用户一个选择:它是哪个资产
  3. Reditect to proper action for this asset
  4. 重新考虑对这项资产采取适当的行动
  5. Render view with common parts for Asset (using for example partial view) and specified for selected one
  6. 用资产的公共部分(例如部分视图)呈现视图,并为选定的部分指定
  7. Retrieve data and commit it to database.
  8. 检索数据并将其提交到数据库。

相关文章