views:

114

answers:

2

I am using a simple repository pattern and have objects with a LazyList such as:

public class Promotion
{
    public int Id { get; set; }
    public string Name { get; set;}
    public LazyList<Site> TargetSites { get; internal set; } // implemented as a LazyList
}

This works great for getting the items but I'm wondering what is usual to do for saving the items?

To persist a promotion I then need to save the list of TargetSites only if they were loaded in the first place. Is this the usual pattern with lazy loaded items? I can find lots of info on lazy loading but very little on persisting the resulting modifications to the lazy loaded objects.

+1  A: 

What are you using as your persistence mechanism? Maybe something like below would help?

 
    public class PromotionSaver 
    {
         public void SavePromotion(Promotion promotion)
         {
             //save to the Promotion table
             if(promotion.HasTargetSites) //add a property to Promotion that gives you whether the list is loaded or not
             {
                 foreach(Site site in promotion.TargetSites)
                 {
                      //save to the TargeSites table.
                 }
             }
         }
    }
 
Amir
+1  A: 

As long as the relationship exists in your data model a call to context.SubmitChanges should save the parent object as well as the child objects if they are loaded and have changed. That's the beauty of L2S (and other orms).

TGnat