Permalink
Browse files

Modified ReferenceComponentMapping to pass visitors through to the un…

…derlying ExternalComponentMapping. Also modified ExternalComponentMapping to prevent it from running a visitor more than once.

This combination of changes should allow conventions to be applied to members of a component that was mapped via ComponentMap.
  • Loading branch information...
1 parent 4c96c43 commit 35609a02b40102de4e114f0ac2e1dac014603beb @paulbatum paulbatum committed Jun 27, 2010
@@ -0,0 +1,75 @@
+using System;
+using System.Linq;
+using FluentNHibernate.Conventions;
+using FluentNHibernate.Conventions.AcceptanceCriteria;
+using FluentNHibernate.Conventions.Helpers;
+using FluentNHibernate.Conventions.Inspections;
+using FluentNHibernate.Conventions.Instances;
+using FluentNHibernate.Mapping;
+using FluentNHibernate.MappingModel.ClassBased;
+using FluentNHibernate.Specs.Conventions.Fixtures;
+using Machine.Specifications;
+
+namespace FluentNHibernate.Specs.Conventions
+{
+ public class when_a_property_convention_is_applied_to_a_component_mapping
+ {
+ Establish context = () =>
+ {
+ model = new FluentNHibernate.PersistenceModel();
+ model.Add(new TestClassMap());
+ model.Add(new MyComponentMap());
+ model.Conventions.Add(new MyPropertyConvention());
+ };
+
+ Because of = () =>
+ mapping = model.BuildMappingFor<TestClass>();
+
+ It should_apply_the_convention_to_any_properties_that_match_the_acceptance_criteria = () =>
+ mapping.Components.Single(x => x.Name == "Value").Properties.Single().Columns.Single().SqlType.ShouldEqual("money");
+
+ static FluentNHibernate.PersistenceModel model;
+ static ClassMapping mapping;
+
+ private class TestClass
+ {
+ public int Id { get; set; }
+ public MyComponent Value { get; set; }
+ }
+
+ private class MyComponent
+ {
+ public decimal Price { get; set; }
+ }
+
+ private class TestClassMap : ClassMap<TestClass>
+ {
+ public TestClassMap()
+ {
+ Id(x => x.Id);
+ Component(x => x.Value);
+ }
+ }
+
+ private class MyComponentMap : ComponentMap<MyComponent>
+ {
+ public MyComponentMap()
+ {
+ Map(x => x.Price);
+ }
+ }
+
+ private class MyPropertyConvention : IPropertyConvention, IPropertyConventionAcceptance
+ {
+ public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
+ {
+ criteria.Expect(x => x.Type == typeof(decimal));
+ }
+
+ public void Apply(IPropertyInstance instance)
+ {
+ instance.CustomSqlType("money");
+ }
+ }
+ }
+}
@@ -135,6 +135,7 @@
<Compile Include="Automapping\OverrideSpecs.cs" />
<Compile Include="Conventions\BiDirectionalKeysSpecs.cs" />
<Compile Include="Conventions\Fixtures\Parent.cs" />
+ <Compile Include="Conventions\PropertyConventionSpecs.cs" />
<Compile Include="PersistenceModel\Fixtures\UnionChildEntity.cs" />
<Compile Include="PersistenceModel\Fixtures\UnionChildEntityMap.cs" />
<Compile Include="PersistenceModel\Fixtures\UnionEntity.cs" />
@@ -1,4 +1,6 @@
using System;
+using FluentNHibernate.Visitors;
+using System.Collections.Generic;
namespace FluentNHibernate.MappingModel.ClassBased
{
@@ -15,5 +17,18 @@ public ExternalComponentMapping(ComponentType componentType)
public ExternalComponentMapping(ComponentType componentType, AttributeStore underlyingStore)
: base(componentType, underlyingStore)
{}
+
+ private readonly IList<IMappingModelVisitor> previousVisitors = new List<IMappingModelVisitor>();
+
+ public override void AcceptVisitor(IMappingModelVisitor visitor)
+ {
+ // This external mapping might be shared amongst several ReferenceComponentMappings. Allowing a particular visitor to be applied to this component mapping
+ // more than once can cause issues, so lets ensure that doesn't happen.
+ if(previousVisitors.Contains(visitor))
+ return;
+
+ previousVisitors.Add(visitor);
+ base.AcceptVisitor(visitor);
+ }
}
}
@@ -32,6 +32,9 @@ public ReferenceComponentMapping(ComponentType componentType, Member property, T
public void AcceptVisitor(IMappingModelVisitor visitor)
{
visitor.ProcessComponent(this);
+
+ if(mergedComponent != null)
+ visitor.Visit(mergedComponent);
}
public bool IsSpecified(string name)

0 comments on commit 35609a0

Please sign in to comment.