5

I am currently using component maps like this:

public class UserMapping
{
         public UserMapping()
         {
            Id(c => c.Id).GeneratedBy.HiLo("100");
            Map(c => c.UserName);
            Component(c => c.Country, CountryComponentMapping.Map);
         }
}


public sealed class CountryComponentMapping
{
    public static void Map(ComponentPart<Country> part)
    {
        part.Map(x => x.CountryName)
        part.Map(x => x.CountryAlpha2)
    }
}

I like this becuase I only have to define the mapping for the component/value object in one place.

How would I go about using the same semantics for a collection of the component? (e.g. lets assume we wanted to change this to a collection of countries on the user entity)

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
  • Components are part of the parent entity, think the basic example in the Fluent docs where Address is a component of Customer, but the table it lives as Customer_City, Customer_ZIP etc – Francisco Aquino Feb 25 '11 at 15:40

1 Answers1

7

You can map this as a Component Collection. Unfortunately there is no overload to HasMany().Component() in Fluent NHibernate that allows you to specify that you want to use a derived class of ComponentMap. You can use a modification of your technique above though.

public sealed class UserMap : ClassMap<User> {
    public UserMap() {
        Id(c => c.Id).GeneratedBy.HiLo("100");
        Map(x => x.Name);
        HasMany(x => x.Countries).Component(CountryComponentMapping.Map);
    }
}

public sealed class CountryComponentMapping {
    public static void Map(CompositeElementBuilder<Country> part) {
        part.Map(x => x.CountryName);
        part.Map(x => x.CountryAlpha2)
    }
}
James Kovacs
  • 11,549
  • 40
  • 44