2010-03-30 136 views
1

(表示在C#)这是最有可能的一个为你性感的DBA那里:建模通用关系数据库

怎么会借此我在有一个领域我effieciently模型关系数据库定义“SportType”的“Event”表?

这个“SportsType”字段可以保存一个链接到不同的运动表E.g. “FootballEvent”,“RubgyEvent”,“CricketEvent”和“F1事件”。

这些运动表中的每一个都有特定于该运动的其他字段

我的目标是能够根据需要在未来通用添加运动类型,但将运动特定事件数据(字段)作为我的事件实体的一部分。

是否有可能使用一个ORM,如NHibernate/Entity framework/DataObjects.NET这会反映这种关系?

我已经一起引发一个快速的C#示例来表达我的意图在更高层次上:

public class Event<T> where T : new() 
{ 
    public T Fields { get; set; } 

    public Event() 
    { 
     EventType = new T(); 
    } 
} 

public class FootballEvent 
{ 
    public Team CompetitorA { get; set; } 
    public Team CompetitorB { get; set; }  
} 

public class TennisEvent 
{ 
    public Player CompetitorA { get; set; } 
    public Player CompetitorB { get; set; } 
} 

public class F1RacingEvent 
{ 
    public List<Player> Drivers { get; set; } 
    public List<Team> Teams { get; set; } 
} 

public class Team 
{ 
    public IEnumerable<Player> Squad { get; set; } 
} 

public class Player 
{ 
    public string Name { get; set; } 
    public DateTime DOB { get; set;} 
} 

football Event code completion f1 Event code completion

回答

3

DataObjects.Net支持开放泛型自动映射。一些细节在这里被描述here

+0

链接到DO4论坛描述这种情况下一个话题:http://forum.x-tensive.com/viewtopic.php ?F = 29&T = 5820 – 2010-04-01 03:03:43

0

有一堆像XML列和EAV选项(亦称作为数据库中的数据库),但其中没有一个能够很好地与ORM一起转换为传统的静态面向对象的语言,并且所有这些在数据库级别的数据类型安全性和参照完整性方面都存在缺陷。

如果您在数据库和客户端都需要这种级别的动态结构,那么您可能需要使用更加动态化的对象或文档数据库(和语言) - 关系数据库倾向于与静态关系和数据模型。

2

转换为DO4必须如下所示的示例:

// I'd add this type - adding an abstract base makes design more clean + allows you to share 
// the behavior among all the descendants 
[Serializable] 
[HierarchyRoot] 
public abstract class EventBase : Entity 
{ 
    [Key] 
    Guid Id { get; private set; } // Or some other type 
} 

[Serializable] 
public class Event<T> : EventBase 
    where T : IEntity, new() // IEntity indicates DO4 must try to map its descendants automatically 
    // Although I'd put some stronger requirement, e.g. by using IEventData instead of IEntity here 
{ 
    public T Data { get; set; } 

    public Event(T data) 
    { 
     Data = data; 
    } 
} 

[Serializable] 
[HierarchyRoot] 
public class FootballEvent 
{ 
    // You need [Key] here 
    public Team CompetitorA { get; set; } 
    public Team CompetitorB { get; set; } 
} 

[Serializable] 
[HierarchyRoot] 
public class TennisEvent 
{ 
    // You need [Key] here 
    public Player CompetitorA { get; set; } 
    public Player CompetitorB { get; set; } 
} 

[Serializable] 
[HierarchyRoot] 
public class F1RacingEvent 
{ 
    // You need [Key] here 
    public EntitySet<Player> Drivers { get; private set; } 
    public EntitySet<Team> Teams { get; private set; } 
} 

[Serializable] 
[HierarchyRoot] 
public class Team 
{ 
    // You need [Key] here 
    public EntitySet<Player> Squad { get; set; } 
} 

[Serializable] 
[HierarchyRoot] 
public class Player 
{ 
    public string Name { get; set; } 
    public DateTime DOB { get; set; } 
} 

在这种情况下的事件实例将可用(=自动映射),用于从模型中的所有合适的TS。例如。在这种情况下,他们会: - EventBase //是的,即使它,因为它是适合 - FootballEvent - TennisEvent - F1RacingEvent - 团队 - 玩家

如果你想限制这只是某些类型,您必须执行以下操作: - 添加从IEntity继承的接口,所有这些类型都将支持,例如IEventData。 - 将其用作Event中通用参数T的通用参数约束。

十字发布来自:http://forum.x-tensive.com/viewtopic.php?f=29&t=5820, 回答Alex Yakunin, 首席执行官DataObjects.NET