Skip to content

Instantly share code, notes, and snippets.

@chester89
Created February 19, 2013 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chester89/4990411 to your computer and use it in GitHub Desktop.
Save chester89/4990411 to your computer and use it in GitHub Desktop.
mapping components using convention in Fluent
public class when_specifying_component_convention
{
Establish context = () =>
{
model = new FluentNHibernate.PersistenceModel();
model.Conventions.Add<ComponentConvention>();
model.Add(new EntityWithComponentMap());
model.Add(new AddressMap());
};
Because of = () =>
{
mapping = model.BuildMappingFor<EntityWithComponent>();
};
It should_be_able_to_specify_column_name = () =>
{
mapping.Components.First()
.Properties.Single(x => x.Name == "Count")
.Columns.FirstOrDefault().Name.ShouldEqual("different");
};
static FluentNHibernate.PersistenceModel model;
static ClassMapping mapping;
}
public class EntityWithComponent
{
public int Id { get; set; }
public string Description { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Count { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
}
class AddressMap: ComponentMap<Address>
{
public AddressMap()
{
Map(x => x.Line1);
Map(x => x.Line2);
Map(x => x.Count);
}
}
public class ComponentConvention: IComponentConvention
{
public void Apply(IComponentInstance instance)
{
if (instance.Type == typeof(Address))
{
instance.Properties.First(p => p.Type.GetType() == typeof(int))
.Column("different");
}
}
}
public class EntityWithComponentMap: ClassMap<EntityWithComponent>
{
public EntityWithComponentMap()
{
Id(x => x.Id);
Map(x => x.Description);
Component(x => x.Address);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment