在数据库中模拟抽象基类和子类 [英] model an abstract base class and subclasses in a database

查看:147
本文介绍了在数据库中模拟抽象基类和子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个子类:视频图像注意表单。每个数据包含不同类型的数据。例如, Image 类包含磁盘和映像属性上图像文件的路径,而 Form 类包含表单域值。然而,每个项目之间的常见元素是GPS坐标和标题,所以我有以下抽象基类:

I have 4 subclasses: Video, Image, Note, and Form. Each one contains different types of data. For example, the Image class contains a path to the image file on disk and image properties, and the Form class contains the form field values. The common element between each item, however, is the GPS coordinates and heading, so I have the following abstract base class:

public abstract class Content
{
    public float? Latitude { get; set; }
    public float? Longitude { get; set; }
    public float? Heading { get; set; }
}

然而,我弄朦胧的是如何在数据库中建模。目前,我有一张名为 Events 的表(为例,假设一个事件是生日聚会)和4个表(视频图片笔记表单 )。每个表有一个外键链接到事件'主键。

However, where I get hazy is on how to model this in a database. Currently I have a table called Events (for the sake of example, let's say an event is a birthday party) and 4 tables (Videos, Images, Notes, and Forms). Each table has a foreign key linking back to Events' primary key.

使用LINQ to SQL,I获得每个表的5个类。如果我只想要一种类型的数据,例如 Event.Images ,但是我想要做的就是将内容的总数设为一个事件具有并获取GPS坐标。我可以通过使用 Event.Images.Count()+ Event.Videos.Count()+ ... ,但是我不能做相同的GPS坐标。有没有办法我可以建模数据库,以便我可以为每个项目使用一个基类,当我需要查看其数据时,仍然可以获得个别强类型的项目。

Using LINQ-to-SQL, I get 5 classes for each table. This is fine if I only want one type of data, for example Event.Images, but what I want to do is count the total number of 'contents' an Event has and get the GPS coordinates. I can wing the count easy enough by just using Event.Images.Count() + Event.Videos.Count() + ..., but I can't do the same for the GPS coordinates. Is there some way I can model the database so that I can use a base class for every item and still be able to get the individual strongly-typed item when I need to see its data?

推荐答案

Martin Fowler的企业应用架构模式有三种不同的模式,每种模式都有不同的权衡:

There are three different patterns for this documented in Martin Fowler's Patterns of Enterprise Application Architecture, each with different trade-offs:

  • Single Table Inheritance
  • Class Table Inheritance
  • Concrete Table Inheritance

这篇关于在数据库中模拟抽象基类和子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆