Convention for collection with backing field prefixed with underscore

Chris Richards's Avatar

Chris Richards

28 Oct, 2010 01:58 PM via web

How do I map the collection of Parts using a convention

public class Part
{
  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
}

public class Car
{
  private readonly List<Part> _parts = new List<Part>();

  public virtual int Id { get; set; }

  public virtual IList<Part> Parts
  {
    get { return _parts.AsReadOnly(); }
  }
}

I have tried this convention but it always expects the field name to not have an underscore prefix:

public class HasManyConvention : IHasManyConvention
{
  #region IHasManyConvention Members

  public void Apply(IOneToManyCollectionInstance instance)
  {
    instance.Access.ReadOnlyPropertyThroughCamelCaseField(CamelCasePrefix.Underscore);
  }

  #endregion
}

I've tried it with the 1.10.685 release and also the latest build 1.1.0.695, with the same result:

"Could not find field 'parts' in class 'TestFluentNHibernate.Car'"

See the attached program which reproduces this problem.

  1. 2 Posted by Mark Bryant on 10 Nov, 2010 12:39 PM

    Mark Bryant's Avatar

    Remove CamelCasePrefix.Underscore. There is an overload that takes no arguments.

  2. 3 Posted by Mark Bryant on 10 Nov, 2010 01:21 PM

    Mark Bryant's Avatar

    apologies, I had another look at your post and realised I have missed the point.

  3. 4 Posted by Chris Richards on 26 Jan, 2011 10:30 AM

    Chris Richards's Avatar

    Problem still exists with 1.2.0.694 and also 2.0.0.698. The attached Visual Studio 2010 project uses the same code as above to demonstrate this problem.

  4. 5 Posted by mark.bryant on 02 Feb, 2011 11:40 AM

    's Avatar

    I have had some success with inheriting from DefaultAutomappingConfiguration

    then:

    public override FluentNHibernate.Mapping.Access GetAccessStrategyForReadOnlyProperty(FluentNHibernate.Member member)

    You can then apply a different strategy based on the collection type.

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is the opposite of down?