Just pondering out loud here - what about having an Interceptor that  
overrides, eg, OnFlushDirty in which you detect if the entity is an  
Aggregate Root (eg, check for a custom attribute) and if so, use an  
implementation of something like  
Nhibernate.Event.Default.AbstractVisitor to walk the Aggregate's graph  
and detect if any contained entities/components are dirty  
(TypeFactory.FindDirty might help here too). If so, then return the  
Aggregate as dirty so it's version will be updated.

Performance may be an issue for large graphs, and you'd be doubling  
the dirty checks, so not really sure how workable it is. Also just  
noticed that AbstractVisitor is mostly internal, so you'd need to roll  
your own - looks like it's fairly straightforward though.

Looking forward to seeing how you progress with this, regardless of  
the road you take.

Cheers,

Michael

On 28/04/2009, at 2:42 AM, Greg Young wrote:

>
> We can always start sprinkling code like this around but how do we
> rationalize that its actually happening?
>
> One question ... here you seem to have order and customer as two
> aggregate roots, I am more worried about what happens when its
> something within our aggregate root. Again we could use the locking
> but is that something that we want to have to do ourselves?
>
> consider
>
> Customer
>    Contact
>          Address
>
> for the sake of discussion let's imagine that these are all  
> entities ..
>
> Customer c = CustomerRepo.Fetch(id);
> c.Contact.Address.Street = "testing";
> UnitOfWork.Commit();
>
>
> Basically I want nhibernate to trigger my AR for validation of the
> whole AR (and versioning of the whole AR) when an sub-piece is
> changed. We could use locking etc as you mention but that strikes me
> as bug prone, what if someone forgets to sprinkle that 1 line of code
> somewhere?
>
> Greg
>
> On Mon, Apr 27, 2009 at 12:31 PM, Peter Morris <mrpmor...@gmail.com>  
> wrote:
>>
>>>>
>> with AOP the problem is finding the root (it might be 5 levels deep).
>> I was looking at Peter's suggestion but don't like what it  
>> requires. I
>> am currently looking at a way to do this only in proxies but the  
>> trick
>> is in finding out what aggregate root we are under while not having
>> the id carried by the domain objects ... One thing I see is having  
>> the
>> IHaveParentId be implemented by the proxy ... then have an
>> IAggregateRoot for the actual parent ... so you can just keep walking
>> up the path but none fo the objects carry the ids themselves. As I go
>> through this though I keep hearing "too much magic" being whispered  
>> in
>> my ear.
>> <<
>>
>> There is something I am not understanding about your problem, the  
>> problem I
>> see is entirely different.
>>
>> Here is how I see your scenario where a new order is raised (the  
>> agg-root
>> being order) and checking that the customer's limit isn't exceeded.
>>
>>
>> using (var session = SessionFactory.OpenSession())
>> using (var transaction = session.BeginTransaction())
>> {
>>    var customer = CustomerRepository.Load(customerID,  
>> LockMode.Write);
>> //Fetch a proxy only***
>>    var newOrder = new Order(customer, ...................);
>>    //continue setting up the order
>>
>>    ValidateTheOrder;
>>    customer.AcceptOrder(newOrder);
>>    session.Save(newOrder);
>>    transaction.Commit();
>> }
>>
>> The Order is validated, and the Customer is certain to not have  
>> been updated
>> by another user.
>>
>> The part I have marked *** is the bit I see as the problem, and  
>> that is "How
>> do I ensure nobody updates the customer?".  Typically I would make  
>> sure the
>> Customer is updated too but it seems in NH you can't force a  
>> version update
>> on an object that is not dirty, so I am guessing that the LockMode  
>> is the
>> solution, however I believe that is DB dependent and not all  
>> databases will
>> support it.  If it is in fact possible to update a non-dirty entity  
>> (to
>> ensure agg-parts are valid) then you would add  
>> Session.Update(customer) too,
>> but I suspect that is not possible.
>>
>> Is there something about your problem that I am missing?
>>
>>
>>
>> Pete
>>
>>
>>>
>>
>
>
>
> -- 
> It is the mark of an educated mind to be able to entertain a thought
> without accepting it.
>
> >


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

Reply via email to