简体   繁体   中英

Fluent Nhibernate mapping to different columns by condition

I have an a date table what contains some columns: INTEGER_VALUE, DOUBLE_VALUE, STRING_VALUE, DATETIME_VALUE

An object is:

public class Property
{
    public virtual int Id { set; get; }
    public virtual PropertyKind Kind { set; get; }
    public virtual object Value { set; get; }
    public PropertyValueType ValueType { set; get; }
}

PropertyValueType is:

public enum PropertyValueType
{
    TypeInt,
    TypeString,
    TypeDouble,
    TypeDateTime
}

The target is to map Value to different column of Database (firebird) condition to ValueType. For example if ValueType is TypeString we need to map it to STRING_VALUE column etc. ValueType sets while object begin created (in constructor) and never be changed. Is it possible? Or may be another solution?

NHibernate could help us to map Tables into C# objects / entities ( ORM ) . That means:

we should properly map ALL table's columns into ALL C# entity properties.

Later (eg business layer or inside of the get and set ) we should apply some validation rules to achieve required behaviour. Also we should extend the application tier to assure that for each kind of enum PropertyValueType , correct Property is filled, and will be persisted in a proper Column.

So the object structure could be like this

public class Property
{
    public virtual int Id { set; get; }
    public virtual PropertyKind Kind { set; get; }
    public PropertyValueType ValueType { set; get; }

    // not mapped
    public virtual object Value { ... some logic inside of the getter and setter }


    // mapped properties - could be protected and hidden from upper consumers
    protected virtual int?      IntValue      { get; set;}
    protected virtual string    StringValue   { get; set;}
    protected virtual double?   DoubleValue   { get; set;}
    protected virtual DateTime? DateTimeValue { get; set;}
}

So, NHibernate will help us to mapp columns and properties 1:1. The remaining handling is up to our application.

At the end this will bring us a lot, because we can issue even OrderBy accross all rows, while only some of them do have the column filled...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM