views:

48

answers:

1

I have the following classes:

public class InventoryItem 
{ 
     private Usage[] usages = new Usage[12]; 
     virtual public Usage[] Usages { get { return usages; }} 
     virtual public string Name{get;set;} 
} 

public class Usage 
{ 
     virtual public double Quantity{get;set;} 
     virtual public string SomethingElse{get;set;} 
}

I know that Usages.Length will always be 12. I think it would be best to store it in the DB like so:

Name nvarchar(64), 
Usage_Quantity_0 float, 
Usage_SomethingElse_0 nvarchar(16), 
Usage_Quantity_1 float, 
Usage_SomethingElse_1 nvarchar(16), 
... 
Usage_Quantity_11 float, 
Usage_SomethingElse_11 nvarchar(16),

How can I get this done?

A: 

What about a getter/setter that converts to/from an array and 12 private fields? In Hibernate (I don't know about NHibernate), the mapping can be placed on the field and you can use embeddable entities to have Usage be mapped to 2 columns in InventoryItem. If embeddable entities are not available in NHibernate, then you will need 24 fields.

A 1-to-many relationship is maybe still preferable even if there is a fixed cardinality. If you are worried about performance, search for the N+1 problem and eager fetching.

ewernli
Yes, NHibernate supports the equivalent of embeddable entity, called component in this case: https://www.hibernate.org/hib_docs/nhibernate/html/components.html
ewernli