0

ok, I have difficult time with automapping collections using fluent nhibernate. This time I tried to apply a collection convention which simply says to use camelCaseField with underscore. Well I got the convention loaded and I hit the breakpoint in the method below FNH still produces strange mapping. What I am doing wrong?

public class Parent
{
    public virtual int Id { get; set; }
    private IList<Child> _testCollection;
    public virtual IList<Child> TestCollection
    {
        get
        {
            return _testCollection;
        }
    }
}
public class Child
{
    public virtual int Id { get; set; }
}

public class CollectionAccessConvention : ICollectionConvention
{
    public void Apply( ICollectionInstance instance )
    {
        instance.Access.CamelCaseField( CamelCasePrefix.Underscore );
    }
}

<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Test.Parent, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Parent`">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="Id" />
  <generator class="identity" />
</id>
<bag access="nosetter.camelcase" name="TestCollection" mutable="true">
  <key>
    <column name="Parent_id" />
  </key>
  <one-to-many class="Test.Child, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>

EDIT: @Bary: the strange thing is access="nosetter.camelcase". I think it should be access="field.camelcase-underscore". Any suggestions?

mynkow
  • 4,408
  • 4
  • 38
  • 65

1 Answers1

-3

I figure it out.

Conventions work little bit strange but over time I think I will fully realize it. When you want to apply a convention about certain property or anything it will be applied only if it is not configured/set/defined already. So, when fluent compiles the mappings it automatically sets the readonly properties to access="nosetter.camelcase". Fortunately there is a way to fix this.

The solution:

You have to define your own automapping configuration by extend DefaultAutomappingConfiguration class and then override the method public virtual Access GetAccessStrategyForReadOnlyProperty(Member member) OR just implement the IAutomappingConfiguration interface. After you are done you can add this configuration when you initialize the fluent configuration.

Fluently.Configure( Configuration )
                .Mappings( cfg =>
                {
                    cfg.AutoMappings.Add( *yourIAutomappingConfiguration* )
                }
mynkow
  • 4,408
  • 4
  • 38
  • 65
  • Nice work. I guess that code for a collection convention access strategy wasn't too bad after all! – Berryl Jul 14 '10 at 21:13
  • Yes, it is good, but it is not an answer for the prev question simply because the mapping generated for ISet collection is instead of . And I still cannot handle it. – mynkow Jul 14 '10 at 21:51
  • No, no, no. Look again at the convention. Ok, here is a hint; it is operating on an ICollection. Then look at your code again. If you still can't figure it out I will tell you how to generate a set *properly*. HTH – Berryl Jul 15 '10 at 00:49
  • Well, I tried so many things but I think I miss something fundamental here. If you can help me for this please posti it in the other thread. Thx – mynkow Jul 15 '10 at 06:17