Created
February 19, 2013 21:49
-
-
Save chester89/4990411 to your computer and use it in GitHub Desktop.
mapping components using convention in Fluent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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