This is a migrated thread and some comments may be shown as answers.

Fluent Mapping, Association, IList Initialization

1 Answer 63 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
IT
Top achievements
Rank 1
IT asked on 05 Apr 2011, 03:20 AM
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...
Customer.Orders.Add(order);

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

1 Answer, 1 is accepted

Sort by
0
Serge
Telerik team
answered on 06 Apr 2011, 11:55 AM
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,
Serge
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
Tags
Development (API, general questions)
Asked by
IT
Top achievements
Rank 1
Answers by
Serge
Telerik team
Share this question
or