Set Field Value before save entity

Posts   
 
    
daycrom
User
Posts: 18
Joined: 04-Dec-2007
# Posted on: 02-May-2008 17:52:31   

LLBLGen Pro 2.5 Adapter template

Hello:

I have created an Entity OrderDetail which has among other fields, idProcess and ProcessName.

IdProcess has a ForeignKey reference to process Table but I save the ProcessName because the name of process in Process table can go varying with time.

When the user selects the process in Combobox, this automatically assigns the ID from Process Entity to IDProcess in OrderDetail entity, but also desire that assign the Process.Name to OrderDetail.ProcessName automatically.

I was occurring 3 options:

-To do so with a trigger from the table OrderDetail.

-To do so by code outside the logic of the entity.

-To do so with custom code inside the logic of the entity. (Desired)

I would do so in as possible with the third option but is not that event should override already that do not see a BeforeSaveEntity event. My idea is that within the logic of the entity OrderDetail, prior to save check the name of the Process entity assigned and it be allocated to field OrderDetail.ProcessName

Sorry by the translation.

Regards

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-May-2008 03:46:45   

Hi daycrom.

You could use OnBeforeEntitySave() overridable method:

namespace yourNamespace
{

    public partial class OrderDetailEntity
    {
        protected override void OnBeforeEntitySave()
        {
            // your logic.
            if (Process != null)
           {
                 ProcessName = Process.Name;
           }

            base.OnBeforeEntitySave();
        }
    }   
}

Note that to do that you should set the related entity outside (at you GUI) before save, not only the ProcessId. If you don't want to do that you could call some kind of logic inside OnBeforeEntitySave to retrieve the Process Name from another place.

David Elizondo | LLBLGen Support Team
daycrom
User
Posts: 18
Joined: 04-Dec-2007
# Posted on: 03-May-2008 05:55:12   

Thank you very much David!!!

Now I feel that I have another problem!!confused

I just only assign the IdProcess value, not Process entity (), as do:

Note that to do that you should set the related entity outside (at you GUI) before save, not only the ProcessId. If you don't want to do that you could call some kind of logic inside OnBeforeEntitySave to retrieve the Process Name from another place.

I was going to implement something like this in the OnBeforeEntitySave:

DataAccessAdapter adapter = new DataAccessAdapter();
ProcessEntity process = new ProcessEntity(IdProcess);
adapter.FetchEntity(process);
ProcessName = process.Name

But I have no access to DataAccessAdapter from OrderDetailEntity!!!! As I get the name of the process from Process Entity without use dataaccessadapter?

It is good practice referencing to DatabaseSpecific to have access to dataaccessadapter?

Regards!!

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 03-May-2008 20:12:56   

It is good practice referencing to DatabaseSpecific to have access to dataaccessadapter?

it is not, at least to my opinion. I'd rather go with David's first suggestion:

you should set the related entity outside (at you GUI) before save, not only the ProcessId

daycrom
User
Posts: 18
Joined: 04-Dec-2007
# Posted on: 04-May-2008 01:02:11   

Thanks Guys!!

Yes, we have decided to implement the reference to the entity process outside.

Regards!!