0

I have a database (which cannot be changed) with a whole load of values that are stored in two columns:

Currently each column is mapped to one property and my Domain object looks like this:

int? Value1
bool IsValue1RealValue

string Value2
bool IsValue2RealValue

bool? Value3
bool IsValue3RealValue

I want to put those into a ValueType which would hold 2 or 3 values:

-The value (stored in object or in T)
-The boolean 'RealValue'
-And (optionally) the name of the .NET property name

At the moment I have the following NHibernate mapping:

  Component(x => x.Value1, fdf =>
  {
     fdf.Map(f => f.Value).Column("Value1");
     fdf.Map(f => f.IsRealValue).Column("IsValue1RealValue");
  });

Value1 has the following type:

public class FlaggedNullableIntValue
{
    public int? Value;
    public bool IsRealValue;
}

My domain object would then look like:

public class DomainEntity
{
    public virtual FlaggedNullableIntValue Value1;
    public virtual FlaggedStringValue Value2;
    public virtual FlaggedNullableBooleanValue Value3;
}

I wonder if it is a good idea to go further down this road or is there another construct that might better suit my needs?

5
  • I don't think I understand. How do you want your model to look like? Where there are those T or objects if the the type of Value is int?? What property name do you want to map? Commented Sep 26, 2013 at 19:37
  • I've updated my question. The model would have 1 property for every Value+IsRealValue combination in the database. The value is currently still int, but ideally, the mapped object would be able to handle ints, bools and strings as Type for Value. Commented Sep 26, 2013 at 21:13
  • Are you asking if nHibernate has a feature that converts columns in rows (objects) ? If so, I'm pretty sure it's not possible. You have to map each pair manually. Commented Sep 26, 2013 at 21:18
  • You can only map one DB column to one property, this is a limitation of NHibernate (if that is a limitation) Commented Sep 27, 2013 at 8:58
  • @Rippo, actually, using components as @Laoujin demonstrated is one way to map multiple columns to one property. Another way is to define an ICompositeUserType. Commented Sep 30, 2013 at 20:37

1 Answer 1

1

I think you are on exactly the right track. One suggestion - you should be able to make your FlaggedNullable*Value class generic so that you don't have to create lots of different classes.

public class FlaggedNullableValue<T>
    where T : struct
{
    public T? Value;
    public bool IsRealValue;
}

It looks like you're using Fluent NHibernate to specify your mappings. As long as you are consistent in the way you name those column pairs, you should be able to create a convention to ease the burden of mapping all those properties and column names.

Sign up to request clarification or add additional context in comments.

1 Comment

We ended up using the ICompositeUserType (as per your comment). It works pretty well so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.