0
votes

I have two similar tables (Table1, Table2) so I created a base abstract class which has common properties. Each table has a column indicating status of record processing. I'd like to map this columns to one enum:

enum RecordStatus
{
     UnkownStatus,
     NotProcessed,
     Processed,
}

Unfortunately for each table I need mapping different values for enums.

So I created two converters (Table1StatusConverter, Table2StatusConverter) which inherit from EnumType<RecordStatus> and setup in mappings. It works partially. Partially because NHibernate use only one converter in both classes.

Is this bug or maybe it works like described by design? Is there any workaround for this?

Edit: I write code from memory because the moment I do not have access to it

Entities:


class abstract TableBase
{
  public Guid Id { get; protected set; }
  public string Sender { get; protected set; }
  public DateTime ReceiveTime { get; protected set; }
  public RecordStatus Status { get; set; }
}

class Table1 : TableBase
{
  public string Message { get; set; }
}

class Table2 : TableBase
{
  public ICollection Parts { get; protected set; }
}

Converters: Table1StatusConverter and Table2StatusConverter override the same method, but in different ways.

class Table1StatusConverter : EnumType<RecordStatus>
{
    public override object GetValue(object enumValue) { ... }
    public override object GetInstance(object value) { ... }
}

Mappings:


Table1.hbm.xml
<class name="Table1" table="Table1">
 ..
 <property name="Status" type="MyAssembly.Table1StatusConverter, MyAssembly" />
 ..
</class>

Table2.hbm.xml
<class name="Table2" table="Table2">
 ..
 <property name="Status" type="MyAssembly.Table2StatusConverter, MyAssembly" />
 ..
</class>


2
can we see some mapping files and code please?Rippo

2 Answers

1
votes

This doesn't sound like a good use of inheritance. However, you could accomplish this by mapping the integer value for the enums as a protected field in the base class and use public properties in the extended classes to cast to and from the appropriate enum.

0
votes

May be you need to override this properties explicitly?


class abstract TableBase
{
   //  ...
   public virtual RecordStatus Status { get; set; }
}

class Table1 : TableBase
{
   public string Message { get; set; }
   public override RecordStatus Status { get; set; }
}

class Table2 : TableBase
{
   public ICollection Parts { get; protected set; }
   public override RecordStatus Status { get; set; }
}