Hi Andy - if you want to avoid hitting your Repository and indirectly
your session and database, then it hardly seems fair to ask the
Repository for the Order.  ;-)

Why not ask the Aggregate Root for the Order instead?
Since you cannot be sure that an Id is generated for the Order,  you
probably need to ask the aggregate root for the Order using the
"business key" of the Order.

Anyway, you might try to the following trick.
On the mapping for Order, set the generator class to "assigned" and
add an integer based Version property with unsaved-value="0".
Take the code for the guid.comb generator and use it to create a new
primary key for the Order, when you add it to the Customers.
The reason that you probably need the Version property is that NH
needs a hint as to wether or not it should do an insert or an update
when it flushes the session.

I am not really sure that I would do this myself, but if you really
want to avoid hitting the Session/Database then it might be an idea.
Generally I would not hesitate to flush the new Order into the
database, and then query the base again for the Customer and the Root.

Hope this helps.
Thomas






On 1 Dec., 15:08, Andyk <[email protected]> wrote:
> Hi,
> Im currently working on a code base that avoids Repository.Add() as
> much as possible.
> Instead, we use an aggregate root along with cascade all mapping. (eg:
> to add something, we simply do customer.Orders.Add(...))
> We're also using sequential guids generated in the code.
>
> The problem Ive noticed is that when I perform something like
> customer.Orders.Add(...), Nhibernate doesnt seem to be aware of the
> new order. Its internal list of entities stored in the session (by
> unique identifier) isnt yet aware of the new order.
> Therefore later in code (still during the transaction),
> Repository.GetById(...) fails, returning null.
> (We're using a CQRS structure so application event handlers deal with
> functionality later down the process)
>
> Is there any way of adding a new entity to the current session when
> collection.Add occurs, and avoiding Repository.Add()?
>
> regards
>
> Andy

-- 
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