CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

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

rated by 0 users
Answered (Verified) This post has 1 verified answer | 3 Replies | 2 Followers

Top 500 Contributor
40 Posts
SreeRamaSaran Mullapudi posted on Tue, Apr 10 2012 1:28 PM

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.

 

Answered (Verified) Verified Answer

Top 50 Contributor
161 Posts
Verified by RockfordLhotka

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.

 

Don't forget to verify the post(s) that best answers your question!

.NET Developer @ http://www.sybiz.com.au

All Replies

Top 50 Contributor
161 Posts
Verified by RockfordLhotka

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.

 

Don't forget to verify the post(s) that best answers your question!

.NET Developer @ http://www.sybiz.com.au

Top 10 Contributor
1,772 Posts

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

Jonny Bekkum, Norway CslaContrib Coordinator

Top 500 Contributor
40 Posts

Hi Marjon,

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

once again Thanks for your response.

Regards

Page 1 of 1 (4 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems