Thank you, thank you. Now i can intercept lazy initialization and set
additional operations, flags etc on my implementation of the
PersistentBag. Every relationship in my classes is annotated with a
RelationshipAttribute that I use to do auto-synchronization of the bi-
directional relationships.
[Relationship("lineItems", RelationshipType.Aggregation,
Cardinality.OneOrMore, inverseProperty = "invoice")]
public virtual IBag<LineItem> lineItems
{
//Set up relationship synchronization on the bag after collection has
been initialized.
namespace MC.NHibernate.Collections
{
public class InitializeCollectionEventListener :
IInitializeCollectionEventListener
{
#region Implementation of IInitializeCollectionEventListener
private readonly DefaultInitializeCollectionEventListener
defaultListener;
public InitializeCollectionEventListener()
{
defaultListener = new
DefaultInitializeCollectionEventListener();
}
public void OnInitializeCollection(InitializeCollectionEvent
@event)
{
IPersistentCollection collection = @event.Collection;
ISessionImplementor source = @event.Session;
CollectionEntry ce =
source.PersistenceContext.GetCollectionEntry(collection);
bool wasInitialized = collection.WasInitialized;
defaultListener.OnInitializeCollection(@event);
if (!wasInitialized)
{
string role = ce.LoadedPersister.Role;
string propertyName = role.Substring(role.LastIndexOf
(".") + 1);
Type ownerType = collection.Owner.GetType();
PropertyInfo pi = ownerType.GetProperty(propertyName);
object[] attributes = pi.GetCustomAttributes(typeof
(RelationshipAttribute), true);
if (attributes.Length == 1)
{
RelationshipAttribute ra = (RelationshipAttribute)
attributes[0];
if (String.IsNullOrEmpty(ra.inverseProperty) ==
false)
{
MethodInfo mi = pi.PropertyType.GetMethod
("SetUpRelationshipSynch");
//get a proxy class so that references
object owner = @event.Session.Load
(collection.Owner.GetType(), ((PersistentObject)
collection.Owner).RecordId);
mi.Invoke(collection, new[] { owner,
ra.relationshipName, ra.inverseProperty });
}
}
}
}
#endregion
On Nov 25, 4:43 am, "Richard \(Google\)" <[EMAIL PROTECTED]>
wrote:
> Hi epitka,
>
> If you re-load the object from the session, you'll get the proxy back (and
> it won't hit the database). In the past I've added a 'This' property on my
> domain class to do this:
>
> private Pepe This
> {
> get
> {
> return Session.Load<Pepe>(typeof(Pepe), Id);
> }
>
> }
>
> http://forum.hibernate.org/viewtopic.php?t=982610&highlight=
>
> Is that what you are after?
>
> Cheers,
> Richard
>
> --------------------------------------------------
> From: "epitka" <[EMAIL PROTECTED]>
> Sent: Saturday, November 22, 2008 6:51 PM
> To: "nhusers" <[email protected]>
> Subject: [nhusers] Re: How to get a reference to proxy if I have reference
> to ...
>
>
>
> > I have a "pepe' and I know it is not a proxy, but there is a proxy
> > created for "pepe", and I need that proxy that was created for pepe.
> > In the DafaultCollectionInitializeEventListener, I have a non-proxy
> > through collection.owner, now I need to get the proxy that was already
> > created for this object.
>
> > On Nov 22, 11:41 am, "Fabio Maulo" <[EMAIL PROTECTED]> wrote:
> >> I don't understand what you are looking for.You have an object instance
> >> named "pepe".
> >> To know if pepe is a proxy: pepe as INHibernateProxy and etc. etc.
> >> If pepe is a proxy you have the ILazyInitializer to know various things
> >> including the underlining instance the proxy are proxying.
> >> /// <summary>
> >> /// Return the Underlying Persistent Object, initializing if necessary.
> >> /// </summary>
> >> /// <returns>The Persistent Object this proxy is Proxying.</returns>
> >> object GetImplementation();
>
> >> /// <summary>
> >> /// Return the Underlying Persistent Object in a given <see
> >> cref="ISession"/>, or null.
> >> /// </summary>
> >> /// <param name="s">The Session to get the object from.</param>
> >> /// <returns>The Persistent Object this proxy is Proxying, or <see
> >> langword="null" />.</returns>
> >> object GetImplementation(ISessionImplementor s);
>
> >> 2008/11/22 epitka <[EMAIL PROTECTED]>
>
> >> > Well, I am leading a project of generating complete MVC type web
> >> > application with POCO domain layer and NH and ORM from domain
> >> > developed using Intelliun's VE Enterprise (www.intelliun.com). One of
> >> > the features of this platform is auto synchronizing of bidirectional
> >> > relationships.
> >> > Anyway, I looked at the Abstract, Basic and Casle initializer, yet I
> >> > don't see a way to get proxy from proxied class, only to get
> >> > underlying persistent object from the proxy. Am I missing something?
>
> >> > On Nov 22, 10:57 am, "Fabio Maulo" <[EMAIL PROTECTED]> wrote:
> >> > > 2008/11/22 Ken Egozi <[EMAIL PROTECTED]>
>
> >> > > > I'd think that ReferenceEquels is not appropriate here.
> >> > > > 'equality' in terms of persisted objects, is either through the PK
> >> > > > (for
> >> > > > entities) or the combination of all properties (for Value Objects).
> >> > > > It's perfectly allright imo for a DAL of any kind to represent the
> >> > exact
> >> > > > same DB row in two actual in-memory objects.
>
> >> > > POID only where available, or "natural id" where the POID is not
> >> > available
> >> > > is acceptable too ;)
> >> > > --
> >> > > Fabio Maulo
>
> >> --
> >> Fabio Maulo
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---