2

How do you handle look up values with NHibernate? For example, I have an Order entity and it has a BillingAddress property that is a value object named Address, it's simple if the Address object just contains State and Country properties as strings. But what if I want a Country contains a list of its states the Order form can populate appropriate state dropdown list for each selected country.

Can I still create Country and State as value objects? Or they must be entities? And if they are entities, can Address be a value object?

Below is a sample code of my example:

public class Order { public virtual int OrderId { get; set; } public virtual Address BillingAddress { get; set; } } public class Address { public virtual State State { get; set; } public virtual Country Country { get; set; } } public class Country { public virtual string Name { get; set; } public virtual ICollection<State> States { get; set; } } public class State { public virtual string Name { get; set; } public virtual Country Country { get; set; } } 
3
  • You can drive yourself mad trying to model address data. Postal codes are especially tough because they frequently don't follow political boundaries. Keep it as simple as possible. Commented Sep 6, 2011 at 19:14
  • May be :), but how can I populate a state list based on the current selected country? I know most of you guys are familiar with only one state list: US states, but I'm not from US and I want to offer same function for several countries. Commented Sep 6, 2011 at 19:55
  • Many (most?) countries don't include state/province in their addresses. You can start with the ISO 3166 country (bit.ly/qKq0iB) and subdivision (bit.ly/qS88i9) codes. Then visit each country's postal service to understand how to format their addresses. My data is almost all US and Canada addresses so I treat the address as US, CA, or International. US and CA are (loosely) validated, International addresses are not. So I have methods to get state or province list depending if US or CA is selected. Commented Sep 6, 2011 at 21:58

1 Answer 1

1

If you want to store the lookup data in the database, then they need to be entities. Otherwise, it is up to you. If you do, I suggest marking them as immutable and putting them in a read only 2nd-layer cache.

If you store them as values, and they have multiple fields like Abbrevation, Name, Coordinates, etc. then you can save the id as a value in the data store, and have the lookup data hard-coded as a plain C# class. You'll just retrieve the id value from NHibernate, and then your calling code will have to run the lookup methods on the class. Not as elegant, but simplifies from the NHibernate/database perspective.

Either method is acceptable--it more depends on how you plan on using them: who is maintaining and using the code at each level, where you want the caching and/or lookup code, if you control the calling code or not, etc.

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

1 Comment

Thanks for your answer, I went with simple method as usual: both State and Country are string and users select them from dropdown lists that are populated from hard-coded lookup lists.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.