三种方式加入nhibernate

时间:2011-07-19 01:37:34

标签: nhibernate c#-4.0

我正在尝试创建一个NHibernate映射到下表:

AutomatedTestSequence - SequenceID, SequenceName
TestStep - StepID, StepDisplay
StepSequenceMapping - SequenceID, StepID
TestParameters - SequenceID, StepID, ParamID, ParamName, ParamValue

我的课程和制图如下:

public class AutomatedTestSequence
{
    public virtual int Id
    {
        get;
        set;
    }

    public virtual string Name
    {
        get;
        set;
    }

    public virtual IList<TestStep> StepsList
    {
        get;
        set;
    }
}

,映射是:

<class name="AutomatedTestSequence" table="AutomatedTestSequence">
  <id name="Id" column="SequenceID"/>
  <property name="Name" column="SequenceName"/>

  <bag name="TestStep" table="StepSequenceMapping" cascade="none" lazy="false">
    <key column ="SequenceID" />
    <many-to-many class="TestStep" column="StepID" />
  </bag>
</class>

第二课是:

public class TestStep
{
    public virtual int Id
    {
        get;
        set;
    }

    public virtual string Display
    {
        get;
        set;
    }

    public virtual IList<StepParameter> StepParameterList
    {
        get;
        set;
    }
}

,映射是:

<class name="TestStep" table="TestStep">
  <id name="Id" column="StepID"/>
  <property name="Display" column="StepDisplay"/>

  <bag name="TestParameters" table="TestParameters" cascade="none" lazy="false">
    <key column ="StepID" />
    <many-to-many class="TestParameters" column="ParameterId"/>
  </bag>
</class>

第三课是:

public class StepParameter
{
    public virtual int Id
    {
        get;
        set;
    }

    public virtual string Name
    {
        get;
        set;
    }

    public virtual string value
    {
        get;
        set;
    }
}

,映射是:

<class name="TestParameters" table="TestParameters">
  <id name="Id" column="ParamID"/>
  <property name="Name" column="ParamName" />
  <property name="Value" column="ParamValue" />
</class>

我的问题是,该参数连接到特定自动测试序列中的特定步骤(例如,我有一个名为StartDate的参数,它可以为步骤{{1}设置值7AM在自动序列Open Application中,以及自动序列Morning Sequence中的步骤7PM的值Open Application,但在当前映射中我将参数连接到步骤无论它属于哪个自动测试序列。在映射参数时,如何更改映射以包含自动测试序列ID?

1 个答案:

答案 0 :(得分:0)

我不确定这个类结构是否已修复。如果不考虑这个:

public class AutomatedTestSequence
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }
    public virtual IList<ParameterDictionary> StepParameters { get; set; }
}

public class TestStep
{
    public virtual int Id { get; set; }

    public virtual string Display { get; set; }
}

public class ParameterDictionary
{
    public virtual AutomatedTestSequence Sequence { get; set; }
    public virtual TestStep Step { get; set; }
    /// <summary>
    /// dictionary with paramtername/value pairs
    /// </summary>
    public virtual IDictionary<string, string> Parameter { get; set; }

    public override bool Equals(object obj)
    {
        var other = obj as ParameterDictionary;
        return (other != null) && (Sequence == other.Sequence) && (Step == other.Step);
    }

    public override int GetHashCode()
    {
        return Sequence.GetHashCode() ^ Step.GetHashCode();
    }
}


class AutomatedTestSequenceMap : ClassMap<AutomatedTestSequence>
{
    public AutomatedTestSequenceMap()
    {
        Id(seq => seq.Id, "AutomatedTestSequence");

        Map(seq => seq.Name, "SequenceName");

        HasMany(seq => seq.StepParameters)
            .Table("StepSequenceMapping")
            .KeyColumn("SequenceID");
    }
}

class TestStepMap : ClassMap<TestStep>
{
    public TestStepMap()
    {
        Id(step => step.Id, "StepID");

        Map(step => step.Display, "StepDisplay");
    }
}

class ParameterDictionaryMap : ClassMap<ParameterDictionary>
{
    public ParameterDictionaryMap()
    {
        Table("StepSequenceMapping");

        CompositeId()
            .KeyReference(spmap => spmap.Sequence, "SequenceID")
            .KeyReference(spmap => spmap.Step, "StepID");

        HasMany(spmap => spmap.Parameter)
            .Table("TestParameters")
            .KeyColumns.Add("SequenceID", "StepID")
            .AsMap("ParamName")
            .Element("ParamValue");
    }
}

如果您不能或不想使用FluentNHibernate,请告诉我并发布xml映射。

备注:ParamID非常需要,如果它在数据库中自动生成那么很好,如果以编程方式设置则必须调整上面的代码。

希望这会有所帮助

编辑:xml映射

  <class mutable="true" name="AutomatedTestSequence" table="`AutomatedTestSequence`">
    <id name="Id" type="System.Int32" column="SequenceId">
      <generator class="identity" />
    </id>
    <property name="Name" type="System.String">
      <column name="SequenceName" />
    </property>
    <bag name="StepParameters" table="StepSequenceMapping" mutable="true">
      <key>
        <column name="SequenceID" />
      </key>
      <one-to-many class="ParameterDictionary" />
    </bag>
  </class>

  <class mutable="true" name="TestStep" table="`TestStep`">
    <id name="Id" type="System.Int32" column="StepID">
      <generator class="identity" />
    </id>
    <property name="Display" type="System.String">
      <column name="StepDisplay" />
    </property>
  </class>

  <class mutable="true" name="ParameterDictionary" table="StepSequenceMapping">
    <composite-id mapped="false">
      <key-many-to-one name="Sequence" class="AutomatedTestSequence" column="SequenceID"/>
      <key-many-to-one name="Step" class="TestStep" column="StepID"/>
    </composite-id>
    <map name="Parameter" table="TestParameters" mutable="true">
      <key>
        <column name="SequenceID" />
        <column name="StepID" />
      </key>
      <index type="System.String" column="ParamName"/>
      <element type="System.String" column="ParamValue"/>
    </map>
  </class>