I have a Product class for which I need IList<Item> Items property.
However, I can only get Item from a legacy external repository (web
service) by string id. So I came with these solutions:

1. Use IList<string> ItemIds and do itemRepository.Get(id) when needed
in my client code (controllers). Too manual and not very nice. And it
would be nice to hide the fact that Item is not part of the persisten
store... what if in future we'll finally move Items to the database?
Rewrite all the code? Don't want that.

2. Use IUserType and load/save object inside NullSafeSet/Get:
      public object NullSafeGet(IDataReader rs, string[] names, object
owner)
      {
         return new ItemRepository().Get((string)
NHibernateUtil.String.NullSafeGet(rs, names[0]));
      }
      public void NullSafeSet(IDbCommand cmd, object value, int index)
      {
         new ItemRepository().Save((Item)value);
         cmd.Parameters[index] = ((Item)value).Id;
      }
Above (is simplifed but) should probably work very well (though I'm
not expert in IUserType). There's one issue, though... what do I do if
an error occured and I need to rollback changes to Items? There's no
notion of transactions in IUserType.
There's problem with "new ItemRepository()" which is not nice, see (4)
for why.

3. So I can do this work in the place where I have access to
transactions: ProductRepository.
public Product BeginTransaction() // actually
DbContext.BeginTransaction() but it doesn't matter
{
    itemRepository.BeginTransaction();
}
public Product Get(int id)
{
    var product = Session.Get(id);
    foreach (var item in product.Items)
         itemRepository.SaveOrUpdate(item);
}
public void Rollback() // actually DbContext.Rollback() but it doesn't
matter
{
   itemRepository.Rollback();
}
Notice that it also makes it easier to couple ProductRepository/
ItemRepository together since I can inject IItemRepository into
ProductRepository constructor, not just use "new ItemRepository()"
inside IUserType.
But, this way is a bit harder because I

4. Somehow combine these ways? From what I see, the (4) does not
conflict with (3), i.e. if ProductRepository correctly align its
transactions with ItemRepository, we're safe? So, IUserType will be
responsible for loading/saving Item instances (no foreach(item)
needed), while ProductRepository will deal with transaction failure
and align calls to itemRepository.BeginTransaction()/Rollback()? Will
it work? I think NO because inside IUserType I use "new ItemRepository
()"... so unless I have singlton factory IUserType and
ProductRepository will have different ItemRepository instances/
sessions/transactions...

So, did anyone experienced the same problem? What is the best solution/
practices here?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to