Cyclic reference issue ?

Issue #37 closed
Laurent Noudohounsi created an issue

Hi !

First I would like to thank you for writing this extension which is really useful for me.

I create a topic because I don't really understand your example and I have the feeling that something goes wrong in the building of my architecture. You give the following code

public class Stock
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [MaxLength(8)]
    public string Symbol { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]      // One to many relationship with Valuation
    public List<Valuation> Valuations { get; set; }
}

public class Valuation
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Stock))]     // Specify the foreign key
    public int StockId { get; set; }
    public DateTime Time { get; set; }
    public decimal Price { get; set; }

    [ManyToOne]      // Many to one relationship with Stock
    public Stock Stock { get; set; }
}

So if I create a stock then I have a list of Valuation but a Valuation have a reference to Stock and stock have a list of Valuation and so on...

Is it not a cyclic reference?

I created some objects and when I get an object with GetWithChildren() I have in my debugger an endless list of: ObjectA -> list of ObjectB -> ObjectA -> and so on...

So I'm a little afraid about this and I wonder if it was normal?

Comments (6)

  1. Guillermo Gutiérrez

    Yes, as soon as you include a inverse relationship you create a circular reference, but there's no issue with that. SQLite-Net Extensions handles these references correctly. What are you afraid of?

  2. Laurent Noudohounsi reporter

    Thank you for the very quick reply!

    Ok if there is any issue with that I can continue without being worried.

    I don't really know to be honest, I presumed memory leak. When I noticed this thing and I did researches for "cyclic reference" everybody was "it must be avoid", "it a bad building" " it's the DEVIL" and other things like this and some people talked about create interface to avoid it. That's why I started to be worry about that.

    But as you said "SQLite-Net Extensions handles these references correctly." so it's ok.

    Thank you for your help.

  3. Guillermo Gutiérrez

    Garbage collector will correctly release these objects correctly even if they reference each other, so no memory issue there. The other comments are still valid but it doesn't really apply when designing entity model classes. They are still valid when creating other classes like Services or ViewModels.

  4. Laurent Noudohounsi reporter

    Thank you for the precision, I understand better now.

    You provide great code and great service for user, Keep it up!

  5. dkboxfine

    a question when making an insertion of a record CatalogPreferencesValue with a foreign key that does not exist should I mark an error ?, is that it does not show me error when inserting class1.PNGclass2.PNG

  6. Log in to comment