Ticket Q423413
Visible to All Users

Override Save, Delete functions

created 13 years ago

Hello,

I'm building an XAF application on legacy database.
I need to create, modify, delete the objects of some types using a API. How to override the functions such a save, delete?

I tried:
protected override void OnSaving()
{
//base.OnSaving();
}

protected override void OnSaved()
{
//base.OnSaved();
}

but Xaf still persists objects as normal - puts record into database.

I want to achieve a functionality like this:

  1. The user cliks save of the detail view.
  2. Instead of xaf saving, I use the API function to save object in the specific manner.

Where to put the code?

Answers approved by DevExpress Support

created 12 years ago (modified 12 years ago)

Hello Pawel,
I wrote the following controller to prevent the Detail objects from being saved:

C#
public class Q423413 : ObjectViewController<DetailView, Master> { List<Detail> detailObjectsCache = new List<Detail>(); protected override void OnActivated() { base.OnActivated(); View.ObjectSpace.CustomCommitChanges += ObjectSpace_CustomCommitChanges; View.ObjectSpace.Committing += ObjectSpace_Committing; View.ObjectSpace.Committed += ObjectSpace_Committed; View.ObjectSpace.Reloaded += ObjectSpace_Reloaded; } protected override void OnDeactivated() { View.ObjectSpace.CustomCommitChanges -= ObjectSpace_CustomCommitChanges; View.ObjectSpace.Committing -= ObjectSpace_Committing; View.ObjectSpace.Committed -= ObjectSpace_Committed; View.ObjectSpace.Reloaded -= ObjectSpace_Reloaded; base.OnDeactivated(); } void ObjectSpace_Reloaded(object sender, EventArgs e) { detailObjectsCache.Clear(); } void ObjectSpace_Committed(object sender, EventArgs e) { detailObjectsCache.Clear(); } void ObjectSpace_Committing(object sender, CancelEventArgs e) { IObjectSpace os = (IObjectSpace)sender; for (int i = os.ModifiedObjects.Count - 1; i >= 0; i--) { object item = os.ModifiedObjects[i]; if (typeof(Detail).IsAssignableFrom(item.GetType())) { detailObjectsCache.Add(item as Detail); os.RemoveFromModifiedObjects(item); } } } void ObjectSpace_CustomCommitChanges(object sender, HandledEventArgs e) { //Dennis: Implement a custom saving for your Detail objects here. foreach (Detail detailObject in detailObjectsCache) { //... } } }

I also recommend you handle the ObjectSpace.CustomCommitChanges event to plug-in your custom saving algorithm. It will be easier than maintaining your own ObjectSpace. Take special note that you can subscribe to the ObjectSpace events globally in the overridden CreateObjectSpaceCore method of your XafApplication descendant.
I hope you find this information helpful.

    Show previous comments (2)

      Hi Anatol !
      "I am afraid that the newly created record cannot be replaced with the existing record."
      O.K. I understand that.
      I would like only that only update an existing record with the newly created datas, so no insert.
      If you view the code above, if it simply disable the INSERT, so no save into database.
      Is there any flag to update ?
      Thanks, but it will simplify my project. I view your latest answer.
      Thanks

      Dennis Garavsky (DevExpress) 12 years ago

        Hello Zoltan,
        If I understand your task correctly, in the simplest case it is sufficient to deactivate the corresponding New Actions via a custom ViewController as shown at How to remove or hide (deactivate, disable) a button ( Action ) from tool bar.
        Otherwise, there are no any flags to disable for you. If the UI-level solution I suggested does not meet your needs, then please create a separate ticket and describe your requirements in greater detail.

          Hi Dennis !
          Thanks for the help, I will create the new ticket.
          Zoltan

          Other Answers

          created 13 years ago (modified 9 years ago)

          Hello Pawel,
          The OnSaving and OnSaved methods are only responsible for throwing the corresponding events. The only way to avoid the object saving via these methods is to throw an exception, but in this case the exception should be handled in your code. However, this is not the recommended way. It appears that the most appropriate solution for your situation is to override the Save action's behavior, as shown in the Override Controller Behavior help topic. Alternatively, implement a custom Object Space (IObjectSpace implementor) and perform the necessary actions in its CommitChanged method. To use the custom Object Space, either override the XafApplication.CreateObjectSpaceCore method, or implement a custom ObjectSpaceProvider and pass it via the XafApplication.CreateCustomObjectSpaceProvider event.

          You can learn more on this from the following XAF documentation:
          Ways to Implement Business Logic
          Create, Read, Update and Delete Data

            Show previous comments (8)
            Dennis Garavsky (DevExpress) 12 years ago

              Hello Pawel,
              I wrote the following controller to prevent the Detail objects from being saved:

              C#
              public class Q423413 : ObjectViewController<DetailView, Master> { List<Detail> detailObjectsCache = new List<Detail>(); protected override void OnActivated() { base.OnActivated(); View.ObjectSpace.CustomCommitChanges += ObjectSpace_CustomCommitChanges; View.ObjectSpace.Committing += ObjectSpace_Committing; View.ObjectSpace.Committed += ObjectSpace_Committed; View.ObjectSpace.Reloaded += ObjectSpace_Reloaded; } protected override void OnDeactivated() { View.ObjectSpace.CustomCommitChanges -= ObjectSpace_CustomCommitChanges; View.ObjectSpace.Committing -= ObjectSpace_Committing; View.ObjectSpace.Committed -= ObjectSpace_Committed; View.ObjectSpace.Reloaded -= ObjectSpace_Reloaded; base.OnDeactivated(); } void ObjectSpace_Reloaded(object sender, EventArgs e) { detailObjectsCache.Clear(); } void ObjectSpace_Committed(object sender, EventArgs e) { detailObjectsCache.Clear(); } void ObjectSpace_Committing(object sender, CancelEventArgs e) { IObjectSpace os = (IObjectSpace)sender; for (int i = os.ModifiedObjects.Count - 1; i >= 0; i--) { object item = os.ModifiedObjects[i]; if (typeof(Detail).IsAssignableFrom(item.GetType())) { detailObjectsCache.Add(item as Detail); os.RemoveFromModifiedObjects(item); } } } void ObjectSpace_CustomCommitChanges(object sender, HandledEventArgs e) { //Dennis: Implement a custom saving for your Detail objects here. foreach (Detail detailObject in detailObjectsCache) { //... } } }

              I also recommend you handle the ObjectSpace.CustomCommitChanges event to plug-in your custom saving algorithm. It will be easier than maintaining your own ObjectSpace. Take special note that you can subscribe to the ObjectSpace events globally in the overridden CreateObjectSpaceCore method of your XafApplication descendant.
              I hope you find this information helpful.

                Thank you Dennis.
                I am using a modified version of your idea and it works.

                Dennis Garavsky (DevExpress) 12 years ago

                  Thank you for your confirmation, Pawel!

                  Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                  Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.