Twitter LinkedIn Github

JetBrains

The AutoPersistenceModel in Fluent NHibernate doesn't support values types automatically (yet), but getting them to work isn't that difficult. Take the following example:

 

   1: public class Company
   2: {   
   3:     public virtual int Id { get; set; }   
   4:     public virtual string Name { get; set; }   
   5:     public virtual ICollection<Contact> Contacts { get; set; }   
   6:     public virtual ICollection<Address> Addresses { get; set; }   
   7: }   
   8:  
   9: public class Contact   
  10: {  
  11:     public virtual string NameFirst { get; set; }  
  12:     public virtual string NameLast { get; set; }  
  13:     public virtual string Telephone { get; set; }  
  14:     public virtual string Email { get; set; }  
  15: }  
  16:  
  17: public class Address   
  18: {  
  19:     public virtual string Street { get; set; }  
  20:     public virtual string City  { get; set; }  
  21:     public virtual string State { get; set; }  
  22:     public virtual string Country { get; set; }  
  23: }  

 

Contact and Address are value types, that is, the only way to get to them is via the aggregate root, which is Company. Here's the AutoPersistenceModel of Fluent NHibernate:

 

   1: persistenceModel.AddEntityAssembly(assembly)
   2:                 .Where(entity => entity.Namespace == "iMeta.Examples.Domain.Entities" && entity.GetProperty("Id") != null)
   3:                 .WithConvention(convention => convention.GetForeignKeyName = p => p.Name + "Id")
   4:                 
   5:  
   6:             .ForTypesThatDeriveFrom<Company>(map =>
   7:                                                {
   8:                                                    map.HasMany<Contact>(t => t.Contacts).Component( c =>
   9:                                                                                     {
  10:                                                                                         c.Map(t => t.NameFirst);
  11:                                                                                         c.Map(t => t.NameLast);
  12:                                                                                         c.Map(t => t.Telephone);
  13:                                                                                         c.Map(t => t.Email);
  14:                                                                                     }).AsList();
  15:                                                    map.HasMany<Address>(t => t.Addresses).Component(c =>
  16:                                                                                    {
  17:                                                                                        c.Map(t => t.Street);
  18:                                                                                        c.Map(t => t.City);
  19:                                                                                        c.Map(t => t.State);
  20:                                                                                        c.Map(t => t.Country);
  21:                                                                                    })
  22:                                                        .AsList();
  23:                                                    
  24:                                                })
  30:                .Configure(config);

 

We are using telling the AutoPersistenceModel that for types that derive from Company (currently there is no support for inheritance but its named like this for future support), map the Contacts and Addresses properties to a collection of components. That simple!

I'm assuming, and hoping, that in future versions this will be supported automatically so there won't be a need for this.