3

Im working on a piece of code using DevExpress XAF, I noticed that if im using the event OnSaving that the code executes 2 times, how can i prevent that

protected override void OnSaving()
{
    if (PrestamoP != null)
    {
        PrestamoP.Prestado -= Monto;
        PrestamoP.Save();
     }
     else if (PrestamoG != null)
     {
         PrestamoG.Prestado -= Monto;
         PrestamoG.Save();
     }

     base.OnSaving();
}
Shwabster
  • 479
  • 1
  • 10
  • 27

3 Answers3

3

XPO does not guarantee that the OnSaving method is called once. See the corresponding note in the XPO Best Practices article.

I can see that you are changing the PrestamoP.Prestado property based on the value of the Monto property. This code is fine if you execute it only once and only when the Monto property is specified for the first time. This code is not fine if you:

  • Save this object without changing the Monto property;
  • Update the early specified Monto value.

So, it appears that a more complex logic is required for the PrestamoG.Prestado property. First, I would move it to the Monto property setter and take the previous value into account (do not forget to check the IsLoading property in this case). Second, I would consider calculating the Prestado value dynamically instead of storing its value. This will allow you to resolve issues with the duplicate business logic execution. See an example here: How to: Calculate a Property Value Based on Values from a Detail Collection.

Kill4kan
  • 46
  • 1
0

I can offer different methods for CRUD functions on onSaving method. IsNewObject, IsDeleted.

// insert
        if (Session.IsNewObject(this))
        {
            a = new a(Session);
            a.CreateReferencedProperties(this);
        }
        // delete
        else if (IsDeleted)
        {
            a= Session.FindObject<A>(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("A=?", this));
            if (a!= null)
                a.Delete();
        }
        // update
        else
        {
            a= Session.FindObject<A>(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse("A=?", this));
            if (a!= null)
                a.CreateReferencedProperties(this);
        }
Serdin Çelik
  • 165
  • 1
  • 4
0

You can use the code below to prevent xaf from entering on saving twice.

base.OnSaving();
SessionObjectLayer sessionObjectLayer = this.Session.ObjectLayer as SessionObjectLayer;
        if (sessionObjectLayer == null || sessionObjectLayer.ParentSession == null)
        {
            //Enter only once
        }
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Dec 31 '21 at 00:37