本文介绍了建模一对零或一关系(Z 基数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找建模 1 : 0,1 关系的最佳方法(可能有一个"或最多有一个").我相信这称为 Z 基数.

I'm struggling to find the best way to model 1 : 0,1 relationships ("may have one" or "has at most one"). I believe this is called Z cardinality.

例如,假设我有两个类 WidgetWidgetTest.并非所有 Widget 都经过测试,而且该测试具有破坏性,因此每个 Widget 最多可以有一个 WidgetTest.还假设将 WidgetTest 字段添加到 Widget 中是不合适的.

For example, suppose I have two classes Widget and WidgetTest. Not all Widgets are tested and the test is destructive so there can be at most one WidgetTest per Widget. Also assume that it's inappropriate to add the WidgetTest fields to Widget.

我希望我的公共界面是:

I would like my public interface to be:

Widget
    WidgetTest { get; set; }

WidgetTest
    Widget { get; }

模型 1:Widget 具有 WidgetTest 属性,并且在数据库中,Widget 表具有唯一约束的 WidgetTest 外键.我的 DBA 认为这将允许 WidgetTest 记录在没有 Widget 的情况下存在.

Model 1: Widget has a WidgetTest property and in the database the Widget table has a uniquely constrained foreign key to WidgetTest. My DBA argues that this would allow a WidgetTest record to exist without a Widget.

WidgetTable
    WidgetTestId (FK, UQ)

模型 2:小部件具有 WidgetTest 的私有集合,并通过从公共 WidgetTest 属性控制的集合中添加或删除单个对象来强制执行 0,1 关系.数据库将此建模为 1:m,其中 WidgetTest 具有唯一约束的 Widget 外键.我认为这意味着采用适合数据库模式的模型(即对我来说有更多的工作).

Model 2: Widget has a private collection of WidgetTest and enforces the 0,1 relationship by adding or removing a single object from the collection controlled by a public WidgetTest property. The database models this as 1:m with WidgetTest having a uniquely constrained foreign key to Widget. I argue that this means adopting the model to fit the database schema (i.e. more work for me).

WidgetTestTable
    WidgetId (FK, UQ)

哪个型号更好?使用 NHibernate 哪个更容易实现?或者还有第三种方式吗?

Which model is better? Which is easier to achieve with NHibernate? Or is there a third way?

编辑...这是我的最终结果:

Edit ... Here's what I ended up with:

public class Widget
{
    // This is mapped in NH using a access strategy
    private IList<WidgetTest> _widgetTests = new List<WidgetTest>(1);

    public WidgetTest
    {
        get { return _widgetTests.FirstOrDefault(); }
        set
        {
            _widgetTests.Clear();
            if (value != null)
            {
                _widgetTests.Add(value);
            }
         }
     }
}

推荐答案

我的方法是对映射中的一对多关系建模,但将多"限制为单个项目.这允许可选的一对一,并且还保证您的 WidgetTest 实例在您保存 Widget 时被持久化.例如:

My approach has been to model a one-to-many relationship in the mappings, but to constrain the "many" to a single item. This allows the optional one-to-one, and also guarantees that your WidgetTest instance is persisted when you save the Widget. For example:

public class Widget
{
    /// <summary>
    /// This property is ignored by the NHibernate mappings.
    /// </summary>
    public virtual WidgetTest WidgetTest { get; set; }

    /// <summary>
    /// For easier persistence with NHibernate, this property repackages the
    /// WidgetTest property as a list containing a single item. If an
    /// attempt is made to set this property to a list containing more than
    /// one item, an exception will be thrown. But why bother? Just use the
    /// WidgetTest property.
    /// </summary>
    public virtual IList<WidgetTest> WidgetTests
    {
        get
        {
            IList<WidgetTest> widgetTests = new List<WidgetTest>();
            if (this.WidgetTest != null)
            {
                widgetTests.Add(this.WidgetTest);
            }
            return widgetTests;
        }
        set
        {
            if (value != null && value.Count > 1)
            {
                throw new Exception("The WidgetTests collection may not contain more than one item.");
            }
            else if (value != null && value.Count == 1)
            {
                this.WidgetTest = value[0];
            }
            else
            {
                this.WidgetTest = null;
            }
        }
    }
}

这篇关于建模一对零或一关系(Z 基数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 01:56