14

I have a 'User' Entity that contains an 'Address' Value Object. I have this mapping ok using FNH's Component concept. However, the Address VO also contains a Country which is another value object. I had assumed that this should be just nested as another component, but this doesn't seem to work. Can anyone tell me how I should solve this?

Code for mapping is below...

Thanks!

public UserMapping()
        {
            Table("Users");
            Id(c => c.Id).GeneratedBy.HiLo("100");
            Map(c => c.UserName).Not.Nullable().Length(64);
            Map(c => c.Email).Not.Nullable().Length(128);
            Map(c => c.Password).Not.Nullable().Length(256);
            Map(c => c.Roles).Length(64);
            Map(c => c.FirstName).Not.Nullable().Length(64);
            Map(c => c.LastName).Not.Nullable().Length(64);
            Map(c => c.BirthDate).Not.Nullable();
            //Address
            Component(x => x.Address, m =>
            {
                m.Map(x => x.AddressLine1).Not.Nullable();
                m.Map(x => x.AddressLine2);
                m.Map(x => x.City).Not.Nullable();
                m.Map(x => x.Region);
                m.Map(x => x.PostalCode).Not.Nullable();
                //*****Country Here********
                // country has Name and Code

            });
        }
UpTheCreek
  • 31,444
  • 34
  • 152
  • 221

2 Answers2

14

Ah, Jimmy Bogard from the FNH mailing list showed me - it's quite straightforward. I don't know what I was doing before! Anyway, for anyone else who's interested:

Component(c => c.Address, m =>
{
    m.Component(cp => cp.Country, m2 =>
    {
        m2.Map(x => x.Name); //etc
    } 
UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
  • 1
    Amazing. I did not realize you could nest them like this. I have been working on doing this for a week. – Nathan Lee Aug 27 '10 at 23:26
  • What if I have IdCountry in Address for country instead of Name and Code (provided that IdCountry is the primary key of Country)? – Apocatastasis Mar 07 '13 at 18:19
0

I would create a map for Country and use m.References(x => x.Country).

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 3
    But then wouldn't that be treating country as an Entity rather than a value object? Maybe there's no downside with doing that though. – UpTheCreek Oct 01 '09 at 15:06
  • 2
    There is a downside. Entities have their own "life" (what affects all selecting and updating queries) and we do not want here to have an `Address` which live on its own. – NOtherDev Apr 17 '11 at 10:37