I'm trying to implement an IUserType for states and country codes that will allow me to access both the two-letter code (what's stored in the database) as well as the full name. I'm following the example in the NHibernate 3.0 Cookbook (p. 225), but my problem is that my StreetAddress class is currently mapped as a component in my automapping configuration:
public override bool IsComponent(Type type)
{
return type == typeof(StreetAddress);
}
With this class identified as a component, I don't know how I can use an IUserType for the component class's property, since that class isn't explicitly mapped. There's nowhere that I could tell fluent NHibernate to use the IUserType specification.
@Firo was close, but there turned out to be a much easier solution. There were two steps here. First, I had to tell Fluent NHibernate not to map the
StateandCountryclasses, which reside in my domain layer:Next, I simply had to create the conventions for the
IUserTypeclasses. This turned out to be easier than @Firo suggested:The definition of those
IUserTypeswas pulled out of the cookbook referenced in the original question, but in case you don't want to read it:And that derives from
GenericWellKnownInstanceType:The repositories for these classes are just a pair of
ReadOnlyCollectionof theStateandCountryobjects. Again, from the cookbook:Hopefully this helps someone out there.