Initializing Values and Adding related data.

Posts   
 
    
BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 24-Jul-2006 23:08:55   

Sometimes when a new record is created in one of our tables, if it’s a new record, we not only have initial values, but we also add records into other tables.

I first thought to put this code in the OnInitialized Event, but it does not work correctly here. When you instantiate an Entity the Fields.State is new, which makes sense. Then when I FetchEntity the entity by setting its primarykey and using an adapter, the related record I added is still there. Which makes sense. But I want to remove the added records if someone tries to Fetch the Entity. Any suggestions?

Basically is there a command called OnFetched. I looked and I did not see it. But I figured I would ask. If there was I could then clear the Entity of any related records.

If there is not a event maybe there is a place in the code template. Then is there a command to clear all? Meaning something like this. Entity.ClearAll, which clears and resets its data, as well as removes all related records/entities?

Thanks



      Protected Overrides Sub OnInitialized()

         MyBase.OnInitialized()

         If Fields.State = EntityState.[New] Then

            MyBase.ObjectType = EnumObjectType.User

            With Me
               .Username = ""
               .Password = ""
            End With

        ‘ This Function Adds A related record.
            Me.AddParentRelation(198, EnumObjectType.Group)

         End If

      End Sub


mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 24-Jul-2006 23:25:11   

OnFetchEntity, OnFetchEntityComplete in the DataAccessAdapter.

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 25-Jul-2006 00:36:03   

mikeg22 wrote:

OnFetchEntity, OnFetchEntityComplete in the DataAccessAdapter.

The related tables are Enitity specific. Is there an event for each Entity?

mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 25-Jul-2006 02:09:11   

BringerOD wrote:

mikeg22 wrote:

OnFetchEntity, OnFetchEntityComplete in the DataAccessAdapter.

The related tables are Enitity specific. Is there an event for each Entity?

The virtual sub OnFetchEntityComplete is called for each entity fetch. OnFetcheEntityComplete's second parameter is an EntityFields2 object called 'fieldsToFetch'. To get the entity name, you can call:


fieldsToFetch(0).ContainingObjectName

So, inside your overridden OnFetchEntityComplete you can check to see if the fetched entity was of a specific type, then do whatever you need to do from there.

I'm not really sure I understand what you are trying to do. Why would you want to delete records from other tables when an entity is fetched?

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 25-Jul-2006 17:11:47   

I guess this is a specific behaviour you don't need for all your classes, so I wouldn't change the generated code if I were you.

Modifying the code outside of the user regions implies that you should either modify the templates or be careful with the next generations, as they will overwrite your changes.

You may instead add a few methods specifically to the Entities, for which you need that behaviour, either in dedicated classes, or in the regions of your entity classes that won't get overriden on next generations.

Maybe it makes it clearer to abstract the additional operation in a business controller, and only call that controller for entity creation / entity fetches.

To add related entities, you should use the generated entity/entitycollection properties:

ex:

    Public Function CreateNewCustomer() As CustomerEntity

        Dim toReturn As New CustomerEntity
        Dim myCustomerAddress As New CustomerAddressEntity
        '... add the data
        'related entity, fk side, readonly in CustomerEntity need to be attached that way (same for collection properies)
        myCustomerAddress.Customer = toReturn
        'relating entity, pk side, can be manipulated directly in the main entity
        toReturn.SalesTerritory = New SalesTerritoryEntity
        '...add the salesterritorydata
        Return toReturn

    End Function

That way, the related entities will get saved when the main one is persisted.

Similarly, you can abstract the fetching logic into an intermediate function which does the deleting job.

If you wish to keep with the original method, then overwrite/overload the constructor with something that performs the related entities additions, and attach the deleting stuff to one of the events suggested at entity creation.

BringerOD
User
Posts: 70
Joined: 15-Jul-2006
# Posted on: 25-Jul-2006 20:09:48   

Just wanted to say thanks for all the feedback. I don't know exactly what I will end up doing. But they where all good ideas.

Thanks again.

Bryan

pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 01-Jun-2007 18:38:45   

BringerOD wrote:

mikeg22 wrote:

OnFetchEntity, OnFetchEntityComplete in the DataAccessAdapter.

The related tables are Enitity specific. Is there an event for each Entity?

You could override OnValidateEntityAfterLoad in your entity: protected override void OnValidateEntityAfterLoad()

This method will be called when all properties are fetched for the entity.

It took me a few hours searching to find this one as protected override void OnInitialized() (which I thought would be the right one) will get called before the properties are actually filled from the database.

Hope this helps Patrick