Saved Event is not providing the property values after object save.

Saved Event is not providing the property values after object save.

Old forum URL: forums.lhotka.net/forums/t/11290.aspx


msrs_it posted on Tuesday, April 10, 2012

Hi,

In our project we have a company object of BusinessBase<Company>. We have to do some operations after the user saves his company details. (like sending an email, mobile notification, etc...)

I've hooked the saved event in my business in the private constructor and the OnDeserialized methods

private Company()

{

this.Saved += new EventHandler<Csla.Core.SavedEventArgs>(Company_Saved);

 [Transactional(TransactionalTypes.TransactionScope)]

protected override void DataPortal_Insert()

{

    using (var ctx = Dal.DalFactory.GetManager())

    {

          var dal = ctx.GetProvider<Dal.ICompanyDal>();

          using (BypassPropertyChecks)

          {

                var item = new Dal.CompanyDto

                {

                     CompanyTypeId = this.CompanyTypeId,

                     CompanyName = this.CompanyName,

                     Address = this.Address,

                     City = this.City,

                     StateId = this.StateId,

                     Zip = this.Zip,

                     TimeZoneId = this.TimeZoneId,

                     BusinessPhone = this.BusinessPhone.ToString(),

                     BusinessTIN = this.BusinessTin,

                     FirstName = this.FirstName,

                     LastName = this.LastName,

                     Email = this.Email,

                     CellPhone = this.CellPhone.ToString(),

                     CellProviderId = this.CellProviderId,

                     AccountTypeId = this.AccountTypeId,

                     AccountStatusId = this.AccountStatusId,

                     AcceptedTnC = this.TermsOfService,

                     IsDeleted = IsDeleted,

                     IsNew = IsNew,

                     DateofRegistration = DateTime.UtcNow,

                     ModifiedDate = DateTime.UtcNow

                };

                dal.Update(item);

                this.CompanyId = item.CompanyId;

                this.TimeStamp = item.LastChanged;

           }

           FieldManager.UpdateChildren(this);

     }

 }

protected override void OnDeserialized(System.Runtime.Serialization.StreamingContext context)

        {

            base.OnDeserialized(context);

            this.Saved += new EventHandler<Csla.Core.SavedEventArgs>(Company_Saved);

        }

private void Company_Saved(object sender, SavedEventArgs e)

{

Debug.Print(string.Format("Saved Company Id: {0}",this.CompanyId));

// Output "Saved Company Id: 0"

}

protected override void DataPortal_OnDataPortalInvokeComplete(DataPortalEventArgs e)

        {

  Debug.Print(string.Format("Saved Company Id: {0}",this.CompanyId));

            // Output: "Saved Company Id: 19"

        }

 

Dal is successfully saving the data and returning the id through item.CompanyId and assigning to this.CompanyId. But it remains 0 in the Company_Saved event. But, the data is storing in the database and the Id is returning to the UI.

I'm unable to figure out why the CompanyId remains 0 even after saving the object and assigning the vaule to the CompanyId Property. I'm able to get the new Company Id value in DataPortal_OnDataPortalInvokeComplete method.

And also i've the following doubts about the events and override methods

1. How can we access the updated values in the Company_Saved method.

2. I observed that the override method DataPortal_OnDataPortalInvokeComplete is firing two times (one is before saving and the other is after saving the object). Why this is firing two times.

3. I've gone through the Using CSLA 4 ebook series. But, Rocky is not discussed about the events or overridden methods in them. Where can i get the extra stuff about these methods or events.

4. When to use the Saved Event / DataPortal_OnDataPortalInvokeComplete

5. There is no enum value in DataPortalOperations to determine the Insert operation. Is there any other way to determine the Insert operation?

Thanks and Regards,

SreeRam.

 

Marjon1 replied on Tuesday, April 10, 2012

I can only answer your first question, because you get a new object back when you perform a save, this object is passed into the SavedEventArgs as the NewObject parameter, so something like this:

DirectCast(e.NewObject, Company).CompanyId will get the new value correctly.

 

msrs_it replied on Wednesday, April 11, 2012

Hi Marjon,

Thanks for your reply. We are able to continue our work using the Saved Event.

once again Thanks for your response.

Regards

JonnyBee replied on Wednesday, April 11, 2012

The first issue to check out is which objects are these events raised on.

I do believe the OnSaved should be virtual just like the other OnXYZ methods.

Added to bugtracker: http://www.lhotka.net/cslabugs/edit_bug.aspx?id=1040

Copyright (c) Marimer LLC