This project has moved and is read-only. For the latest updates, please go here.

Beginner Object Database Question

Feb 23, 2011 at 10:11 PM

Hi All,

Traditionally I have been using relational databases for storing data, so I am having a little trouble getting my head around the object database and the structure of my classes in code.

Say I have two classes; ClassA and ClassB. ClassA can contain an instance of ClassB.

public class ClassA
    public ClassB classBInstance { get; set; }

public class ClassB

If I have a list of ClassB classes in my application, won't I end up duplicating ClassB objects when I serialise my ClassA and ClassB list instances to the database?

Instead of instances of ClassB in ClassA objects, should I just store a ClassB key as a property in the ClassA?

Cheers for your help,


Feb 24, 2011 at 3:33 AM

Sterling will handle this automatically.

If you define a table for both Class A and Class B, then any time you save Class A, Sterling will simply write the key for Class B instead of the full class. When you load class A, Sterling will first load the class, then get the key for class B and load it based on that key. Therefore, if you update Class B somewhere else, it will automatically reflect in Class A when you load it.

For example, I'm creating a sample application that has recipes with a collection of ingredients. The recipe has this:

public class Recipe 
    public int Id { get; set; }
    public List<Ingredient> { get; set; }

And the ingredient has this:

public class Ingredient 
   public int Id { get; set; }
   public Recipe IngredientRecipe { get; set; }
   public string Food { get; set; }
   public Amount FoodAmount { get; set; }

So I can have a recipe with a list of 3 ingredients, and save it, and Sterling will generate 4 files: 1 recipe file, and 3 ingredient files. Even though the ingredients have a circular reference to Recipe, Sterling has no problem handling this. Why even have Recipe on the ingredient object? Because then I can query ingredients directly based on food, for example ("give me all recipes that have chicken") and when I load the ingredient, I automatically load the associated recipe.

Feb 24, 2011 at 1:18 PM

Hi Jeremy,

Thank you very much for the helpful info!

Best Regards,