Skip to main content
.NET 8.0+

CloneObjectViewController.CustomCloneObject Event

Occurs before the default cloning process begins.

Namespace: DevExpress.ExpressApp.CloneObject

Assembly: DevExpress.ExpressApp.CloneObject.v25.1.dll

Declaration

public event EventHandler<CustomCloneObjectEventArgs> CustomCloneObject

Event Data

The CustomCloneObject event's data class is CustomCloneObjectEventArgs. The following properties provide information specific to this event:

Property Description
ClonedObject The target object of the cloning process.
SourceObject Gets the object to be cloned.
TargetObjectSpace Specifies the Object Space of the target object.
TargetType Gets the type of the target object.

The event data class exposes the following methods:

Method Description
CreateDefaultTargetObjectSpace() Returns an Object Space for the CustomCloneObjectEventArgs.ClonedObject.

Remarks

When you handle this event and the handler’s ClonedObject parameter is initialized, the object passed via this parameter becomes the target object of the cloning process. Otherwise, XAF performs default cloning.

Handle this event to implement custom cloning logic. Pass the custom cloned object via the handler’s ClonedObject parameter. Pass the target object’s Object Space via the TargetObjectSpace parameter.

To manually clone a source object, handle this event and assign a custom cloned object to the CustomCloneObjectEventArgs.ClonedObject parameter. If the event is not handled or the ClonedObject parameter is null, XAF performs the default cloning is performed.

The CustomCloneObjectEventArgs.TargetObjectSpace parameter refers to an IObjectSpace object. Initialize the TargetObjectSpace property to use custom Object Space type object to clone an object.

Note

XAF raises an exception when you pass the handler’s ClonedObject parameter if the TargetObjectSpace parameter is null.

You can implement a custom DevExpress.Persistent.Base.Cloner class descendant to customize the cloning process and then call its CloneTo method in the CustomCloneObject event handler.

The following snippet overrides the Cloner.CopyMemberValue method to skip association properties during cloning.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.CloneObject;
using DevExpress.Persistent.BaseImpl.EF;
using Microsoft.EntityFrameworkCore.Metadata;

namespace YourApplicationName.Module.Controllers;

public class CustomizeCloneObjectController: ObjectViewController {
    protected override void OnActivated() {
        base.OnActivated();
        var cloneObjectController = Frame.GetController<CloneObjectViewController>();
        if(cloneObjectController != null) {
            cloneObjectController.CustomCloneObject += cloneObjectController_CustomCloneObject;
        }
    }
    void cloneObjectController_CustomCloneObject(object sender, CustomCloneObjectEventArgs e) {
        e.TargetObjectSpace = e.CreateDefaultTargetObjectSpace();
        var cloner = new MyCloner(e.TargetObjectSpace);
        object objectFromTargetObjectSpace = e.TargetObjectSpace.GetObject(e.SourceObject);
        e.ClonedObject = cloner.CloneTo(objectFromTargetObjectSpace, e.TargetType);
    }
}

public class MyCloner: Cloner {
    public MyCloner(IObjectSpace objectSpace) : base(objectSpace) {
    }
    public override void CopyPropertyValue(
        IPropertyBase property, object sourceObject, object targetObject) {
        if(!(property is IReadOnlyNavigationBase)) {
            base.CopyPropertyValue(property, sourceObject, targetObject);
        }
    }
}
See Also