3

I'm using Sharp Architecture and have a number of situations where Value Objects are used in an Entity. Here is an obvious simple example:

public class Person : Entity
{
    protected Person(){}

    public Person(string personName)
    {
        this.PersonName = personName;
    }

    public virtual string PersonName { get; protected set;}
    public virtual StreetAddress MailingAddress { get; set; }
}

public class StreetAddress : ValueObject
{
    protected StreetAddress(){}

    public StreetAddress(string address1, string address2, string city, string state, string postalCode, string country )
    {
        this.Address1 = address1;
        this.Address2 = address2;
        this.City = city;
        this.State = state;
        this.PostalCode = postalCode;
        this.Country = country;
    }

    public virtual string Address1 { get; protected set; }
    public virtual string Address2 { get; protected set; }
    public virtual string City { get; protected set; }
    public virtual string State { get; protected set; }
    public virtual string PostalCode { get; protected set; }
    public virtual string Country { get; protected set; }
}

This of course throws:

An association from the table Person refers to an unmapped class: Project.Domain.StreetAddress
because the the AutoPersistenceModelGenerator only includes classes with type IEntityWithTypedId<>. Its not clear how Sharp Architecture expects this common condition to be implemented. Does this have to be handled with a bazillion overrides?
tlum
  • 913
  • 3
  • 13
  • 30

3 Answers3

4

You could change the GetSetup() method in AutoPersistenceModelGenerator to something like:

private Action<AutoMappingExpressions> GetSetup()
    {
        return c =>
                   {
                       c.IsComponentType = type => type.BaseType == typeof (ValueObject);
                   };
    }

I'll try to get the blogpost I saw covering this posted for credit.

Felipe Leusin
  • 19,991
  • 2
  • 26
  • 19
3

You would want to map this as a component. You can use the mapping overrides in Fluent NHibernate to accomplish this.

Alec
  • 691
  • 4
  • 12
1

I agree with Alec. I would map this as a component.

For more information on that, see this SO question:

AutoMapping a Composite Element in Fluent Nhibernate

There, you'll also find info on how to map a collection of composite elements.

Community
  • 1
  • 1
Martin Suchanek
  • 3,006
  • 6
  • 31
  • 31