views:

95

answers:

2

Hi,

I want to track a list of root objects which are not contained by any element. I want the following pseudo code to work:

using (session1 = [...]) {
  IList<FavoriteItem>list = session1.Linq<FavoriteItem>().ToList();
}

list.Add(item1);
list.Add(item2);
list.Remove(item3);
list.Remove(item4);
var item5 = list.First(i => i.Name = "Foo");
item5.Name = "Bar";

using (session2 = [...]) {
  session2.Save(list);
}

This should automatically insert item1 and item2, delete item3 and item3 and update item5 (i.e. I don't want to call sesssion.SaveOrUpdate() for all items separately.

Is it possible to define a pseudo entity that is not associated with a table? For example I want to define the class Favorites and map 2 collection properties of it and than I want to write code like this:

using (session1 = [...]) {
   var favs = session1.Linq<Favorites>();
}
favs.FavoriteColors.Add(new FavoriteColor(...));
favs.FavoriteMovies.Add(new FavoriteMovie(...));

 using (session2 = [...]) {
  session.SaveOrUpdate(favs);  
}

FavoriteColors and FavoriteMovies are the only properties of the Favorites class and are of type IList and IList. I do only want to persist the these two collection properties but not the Favorites class.

Actually I want a IPersistentCollection object that tracks adds and removes that belongs to no parent entity and stands for itself (the same stuff that happens to collection properties of entities, only in my case I have no parent entity). This works perfectly well if the collections belong to an entity in which case I can add and remove items between two sessions.

Any help is much appreciated.

A: 

A simpler solution than a pseudo entity would be to wrap the list in an object that manages the things you want.

public class FavoriteList : IEnumerable
{
  private List<FavoriteItem> list;
  private ISession session;
  public FavoriteList(ISession session) 
  {
    list = session.Linq<FavoriteItem>().ToList();
    this.session = session;
  }

  public void Add(FavoriteItem item)
  {
    session.SaveOrUpdate(item); 
    list.Add(item);
  }

  public void Remove(FavoriteItem item)
  {
     session.Delete(item); //or something like that
     list.Remove(item);
  }

  public IEnumerator GetEnumerator()
  {
     return (list as IEnumerable).GetEnumerator();
  }
}
Jan Willem B
But that doesn't work if the loading and saving happens in two different sessions and the adds and removes happen between these sessions.Actually I want a IPersistentCollection object that tracks adds and removes that belongs to no parent entity and stands for itself (the same stuff that happens to collection properties of entities, only in my case I have no parent entity). This works perfectly well if the collections belong to an entity in which case I can add and remove items between two sessions.
Daniel
A: 

I still have not found a real solution to this problem. My work around so far is that I have added the collection as a child collection property to another entity from which only one instance exists so far. But this solution breaks if there will be more instances of this entity and it has the disadvantage that the version of it is incremented every time a item is added or removed.

The other work around would have been to create a pseudo entity with no properties/columns (except an ID).

The third alternative I could think of is recreating the whole collection every time which is quite slow and does not work if other entities are referencing one of the items.

The last alternative would be to reimplement the dirty checking functionality myself but this would add some complexity and code duplication.

If somebody knows better alternatives I would be glad for any comments.

Daniel