0

I have a mapping like this.

public class MyObjectMap : ClassMap<MyObject> {
public MyObjectMap()
{
  Component(_ => _.MyItem, key =>
  {
    key.Map(x => x.MyItemValue).Column("COL");
    /** I want to set this value to a particular enum in this mapper **/
    key.Map(x => x.MyItemType).AssignSomeValue(MyEnum.MyValueType)
  });
}
}

How do I set the value to some particular item type. It is a component of a particular type.

Jim
  • 14,952
  • 15
  • 80
  • 167
  • do you mean it should always have the same value without the need of a column in the database? – Firo Jul 04 '12 at 14:25
  • Yes, the values of x.MyItemType should always have the same value when it is read out of the database. The class is used in other circumstances, but in this case it's read out of the database, and I wish to set a default value regardless. – Jim Jul 08 '12 at 12:32
  • Perhaps the mapper has an event of some kind. OnItemBeingRead or something. – Jim Jul 08 '12 at 12:34

2 Answers2

0

IUserType can do this

class ConstantValueUserType : IUserType
{
    NullSafeGet(IDataReader rd, string[] names, object owner)
    {
        return 5; // Constant Value
    }

    public object NullSafeSet(ICommand cmd, object value, int index)
    {
        // empty, we dont want to write
    }

    public SqlType[] SqlTypes { get { return new SqlType[0]; } }
}
Firo
  • 30,626
  • 4
  • 55
  • 94
0
key.Map(x => x.MyItemType).ReadOnly().Formula(((int)MyEnum.MyValueType).ToString()).CustomType<int>();