views:

128

answers:

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?

+2  A: 

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

Alec Whittington
+2  A: 

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
+1  A: 

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

For more information on that, see this SO question:

http://stackoverflow.com/questions/884028/automapping-a-composite-element-in-fluent-nhibernate

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

Martin Aatmaa

related questions