Skip to content

Commit

Permalink
Modified ReferenceComponentMapping to pass visitors through to the un…
Browse files Browse the repository at this point in the history
…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
paulbatum committed Jun 27, 2010
1 parent 4c96c43 commit 35609a0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/FluentNHibernate.Specs/Conventions/PropertyConventionSpecs.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
}
1 change: 1 addition & 0 deletions src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using FluentNHibernate.Visitors;
using System.Collections.Generic;

namespace FluentNHibernate.MappingModel.ClassBased
{
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 35609a0

Please sign in to comment.