Event when the entity is materialized

Posts   
 
    
Posts: 95
Joined: 31-Mar-2012
# Posted on: 27-Apr-2012 16:09:11   

Hi *,

is there some event I can hook up to, that fires whenever the entity is materialized from database? I'd like to perform some actions with it before it's returned to caller.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 27-Apr-2012 19:03:08   

Do you use adapter or selfservicing? V3.5?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 95
Joined: 31-Mar-2012
# Posted on: 27-Apr-2012 19:20:44   

Yes. latest 3.5 with adapter.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Apr-2012 22:11:15   

On Adapter you could subscribe to EntityBase2.EntityContentsChanged, though this is not really only for a fetch action. Adapter entities are not aware of the persistence fetch, all they know is that something instantiate them and fill with values.

You could also override DataAccessAdatper.OnFetchEntity and rise your own event. Your event could be declared at CommonEntityBase.

David Elizondo | LLBLGen Support Team
Posts: 95
Joined: 31-Mar-2012
# Posted on: 28-Apr-2012 07:06:14   

Looks good. How I'll get the entity instance itself from OnFetchEntity/OnFetchEntityComplete? And is this event fired for any entity (IEntity2) (even nested) coming from LINQ queries?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Apr-2012 21:55:34   

From OnFetchEntity/OnFetchEntityComplete you only have access to the IEntityFields. Looking closely I think a better place to you is the EntityCore.OnValidateEntityAfterLoad. You can override it in the CommonEntityBase:

public abstract partial class CommonEntityBase 
{
    protected override void OnValidateEntityAfterLoad()
    {
        base.OnValidateEntityAfterLoad(); 
        
        // raise your event
    }
}

... and yes, if you use LINQ2LLBL to materialize entities, that method will be called as well.

David Elizondo | LLBLGen Support Team
Posts: 95
Joined: 31-Mar-2012
# Posted on: 30-Apr-2012 08:45:41   

Umm, I need something on DataAccessAdapter, because though it I'm getting the entities.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Apr-2012 20:35:57   

I don't understand your last comment.

Posts: 95
Joined: 31-Mar-2012
# Posted on: 30-Apr-2012 20:49:19   

The event I'd like to find should be raised on DataAccessAdapter (because this is "the" object being used to access entities [1]) rather than on entity itself. The action I'm doing is actually DataAccessAdapter dependent.

[1] And I'm guessing that it is (or any class inside it) also doing the materialization.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-May-2012 06:57:55   

The IDataAccessAdapter.OnFetchEntity/Complete methods receives the IEntityFields2 fieldsToFetch parameter which is kind of agnostic of the containing parent entity, because the method is used by both the entity fetches and the dynamic list fetches. So you can't get the involved entity at that point. I don't see any other method to override or event to subscribe on IDataAccessAdapter for this purpose.

I would recommend going to IEntityCore.OnValidateEntityAfterLoad. This is not on DataAccessAdapter but, at the end you will get a common place to intercept the materialization activity.

David Elizondo | LLBLGen Support Team
Posts: 95
Joined: 31-Mar-2012
# Posted on: 01-May-2012 07:00:10   

Then I have to find another way. I need a reference to IDataAccessAdapter that did the fetching and I don't like the idea of doing this in entity, as it's DAL related and not DTO/Entities related. Bad separation of concerns.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 01-May-2012 10:30:04   

The only way is through the OnValidateEntityAfterLoad method in the entity, not on the adapter. The OnFetch... methods are not called for each entity fetched when you fetch a collection.

What kind of work do want to do in that method? Perhaps there's another way to accomplish it.

Frans Bouma | Lead developer LLBLGen Pro