Fluent Mapping, Association, IList Initialization

Thread is closed for posting
2 posts, 0 answers
  1. IT
    IT avatar
    41 posts
    Member since:
    Jan 2010

    Posted 04 Apr 2011 Link to this post

    Previously in forward-only mapping, I would have a private field and public property for a relationship:-

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

    Now with the Fluent API, if I take the example here I would do away with the field, and have just the property:-

    public class Customer
        public IList<Order> Orders { get; set; }

    Which a) forces me to use the set accessor (which I don't need), and b) takes away the initialization of the list.

    The example in the link above defines a one-to-many in just this way, however in the usage example it is used...

    // like this...
    Order.Customer = customer;
    // instead of...

    Which is how I previously used it (as a managed list). Trying to use this method now throws a NullReferenceException because the Orders property the customer is not initialised if it is empty.

    I can't find any info on the correct way to initialise this using the new method.

    Thanks in advance for any help that can be provided
  2. Serge
    Serge avatar
    375 posts

    Posted 06 Apr 2011 Link to this post

    Hi Aleks,

     As I am looking trough the getting started example that I assume you are referring to I see that it should be modified a bit. Indeed it is absolutely necessary for a list to be initialized and if one is only using properties this should be done in the constructor. 

    However there is another point here, while it is true that the example suggest the use of auto defined properties the Fluent Mapping API fully support the use both fields and properties. You can have a look at this article in order to see how one can modify or control the field names for properties.

    We will of course update the getting started tutorial with the initialization of the lists.

    I hope this is helpful and thank you for spotting this matter.

    Kind regards,
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top