FluentNHibernate mapping a single column to two properties: is this possible?

The legacy database I'm working with has a table with the following example information:

LiabilityType 134 137 140 143 146 999 001 003 006 009 

These codes actually contain two bits of information:

  • Whether debt is classified as an expense or liability (when you run the code with an expense of "1" β†’; if it starts with an obligation of "0" β†’
  • Type of debt (e.g. pledge, funeral expenses, overdraft, etc.).

Thus, I would like to map this field to two properties in my entity.

I looked at ICompositeUserType, but it looks like matching two fields with one (composite) property, rather than matching one field with two properties.

I see how I can create two classes that implement IUserType that examine this field and convert it to the correct property values, but I cannot understand how the class will convert the properties back to the corresponding database values. For example, I would like to map this so that I can create a linq query where I can say:

 .Where(x => x.ExpenseOrLiability == ExpenseOrLiability.Expense) 

and it will be converted to SQL as follows:

 WHERE LiabilityType LIKE '1%'. 

Is it possible?

+4
source share
1 answer

We dealt with this situation through denormalization. Save the code and type of debt classification in separate fields. This allows you to present the code to the user, but use its inline queries.

To prevent values ​​from syncing, you should get one from the other. For example, output a breakthrough:

 public virtual LiabilityType LiabilityType { get; set; } public virtual ExpenseOrLiability ExpenseOrLiability { get { return // extract ExpenseOrLiability from LiabilityType } set{} // ignore set } public virtual DebtType DebtType { get { return // extract DebtType from LiabilityType } set{} // ignore set } 

OR display code

 public virtual LiabilityType LiabilityType { get { return // combine ExpenseOrLiability with DebtType somehow } set { } // ignore set } public virtual ExpenseOrLiability ExpenseOrLiability { get; set; } public virtual DebtType DebtType { get; set; } 
+1
source

All Articles