views:

51

answers:

2

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?

A: 

I would help you if you had acknowledged the last time I answered your question, even though you obviously liked the code I gave you there enough to use it.

Or at least if you hadn't whined that part of your question remained unanswered and then sarcastically implied your question wasn't understood!

Seriously dude, this is a community resource that you'll get more out of by acting like a solid citizen. That includes making more of an effort to define what you're trying to do in this post, and what it is you find strange about the mapped result.

Berryl

Berryl
well, you are too offensive. First, the two questions are so different and second your prev answer is not correct. I tried what u suggested there but could not get it work so new question was born. If you are looking for reputation points I cannot help you. As you said this is a community and if someone else with the same problem sees your incorrect answer marked as "Answer"... Come on...Peace
mynkow
No, I am not looking for your points, and yes, Peace. Part of the reason FNH gets complicated to automap is that you need to get all of the pieces right, including your conventions, NHibernate properties, startup configuration, and potentially user types and map overrides. And your basic C# code too! That answer is in fact, correct. And you *would* get <bag access="field.camelcase-underscore" IF you had a setter. Look at your property and code more closely - you have NO SETTER with your getter. You aren't even creating the list anywhere! Do you see?
Berryl
ok, I am not creating the list because I dont use it. I want just to map it. If I write Override for this class I got access="field.camelcase-underscore" even I do not have setter. This is strange for me.
mynkow
And if I wanted to be offensive I would demerit your comment. I am being honest here. Reread your answer on the last post and ask yourself if a 50% acceptance rate implies laziness or not while you are at it. I am actually trying to help you out here.
Berryl
A: 

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
Nice work. I guess that code for a collection convention access strategy wasn't too bad after all!
Berryl
Yes, it is good, but it is not an answer for the prev question simply because the mapping generated for ISet<T> collection is <bag> instead of <set>. And I still cannot handle it.
mynkow
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
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

related questions