AlexSC
AlexSC

Reputation: 1923

nHibernate: How to map a property that can be of any type?

I´m have a class like this:

public class Answer
{
    virtual public int Number { get; set; }
    virtual public object Value { get; set; }
}

The Value property is typed as object because for a yes/no question it will hold a bool value, for a multiple choice question it will hold an int value and for an open question it will hold a string value.

I intend to persist such a class in a Microsoft SQL Server 2012 table, being the Value stored in a varbinary column (not sure yet this is a good idea).

How to map such a class in nHibernate?

Thanks in advance!

Upvotes: 2

Views: 90

Answers (1)

Jerry Federspiel
Jerry Federspiel

Reputation: 1524

If you really just have three different possible types for Value, consider making four different Answer classes- a base class (Answer), and three subclasses (AnswerInt, AnswerString, AnswerBool). You can then do one of the standard mapping techniques- table-per-hierarchy or table-per-class.

Upvotes: 2

Related Questions