Private collection mapping in fluent nhibernate

前端 未结 3 427
日久生厌
日久生厌 2020-12-17 01:22

How can I map this:

public class Customer
{
   private IList _orders;
   public IEnumerable 
   GetAllOrders()
   {
      return _o         


        
相关标签:
3条回答
  • 2020-12-17 01:23

    The easiest solution is to expose your collection as a public property Orders instead of the GetAllOrders() method. Then your mapping is

    HasMany(x => x.Orders)
        .Access.AsCamelCaseField(Prefix.Underscore);
    

    and your class is

    public class Customer
    {
        private IList<Order> _orders = new List<Order>();
    
        public IEnumerable<Order> Orders 
        { 
            get { return _orders; }
        }
    }
    

    If that doesn't work for you, it is possible to map private properties using Fluent NHibernate's Reveal mapping.

    Edited to add: Having just done this, the correct answer is:

    HasMany<Order>(Reveal.Property<Customer>("_orders")) etc.
    

    The collection must be exposed as a protected virtual property to allow proxying:

    protected virtual IList<Order> _orders { get; set; }
    

    This answer put me on the right track.

    0 讨论(0)
  • 2020-12-17 01:28

    You can map a completely private collection using Reveal.Member(), but it has a specific and non-obvious restriction: the Expression that HasMany() accepts has to return either IEnumerable<TReferenced> or object.

    For your class:

    public class Customer
    {
        private IList<Order> _orders;
        public IEnumerable<Order> GetAllOrders()
        {
            return _orders;     
        }
    }
    

    the following line will populate the _orders collection:

    HasMany(Reveal.Member<Customer, IEnumerable<Order>>("_orders"));
        //additional mapping behaviors
    

    For completeness - the following line gives a compiler error:

    HasMany(Reveal.Member<Customer, IList<Order>>("_orders"));
    
    0 讨论(0)
  • 2020-12-17 01:47

    Thanks. Your solution is fine. However, there could be situations(hypotetical) when you dont want to reveal your private collection. This mapping scenario is not explained in your linked post because there is difference between mapping simple propertie as descibed in that post and collection mapping. My attempt to use HasMany(x => Reveal.Propertie("_orders")) failed because of raised exception.

    0 讨论(0)
提交回复
热议问题