Skip to main content
A newer version of this page is available. .
All docs
V22.1

Execute Custom Operations on Endpoint Requests

  • 2 minutes to read

You can execute custom code when a Web API service processes HTTP requests.

Create a custom IDataService class. This class should contain a corresponding method for each endpoint.

using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Core;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.WebApi.Services;
// ...
public class CustomDataService : IDataService {
    private readonly DataService internalDataService;
    public CustomDataService(IObjectSpaceFactory objectSpaceFactory, ITypesInfo typesInfo) {
        internalDataService = new DataService(objectSpaceFactory, typesInfo);
    }
    // POST
    public T CreateObject<T>(IObjectDelta<T> delta) where T : class {
        return internalDataService.CreateObject<T>(delta);
    }
    // POST with /key and associated property
    public void CreateRef<T>(string key, string navigationProperty, string relatedKey) {
        internalDataService.CreateRef<T>(key, navigationProperty, relatedKey);
    }
    // DELETE
    public T DeleteObject<T>(string key) {
        return internalDataService.DeleteObject<T>(key);
    }
    // DELETE with /key and associated property
    public void DeleteRef<TEntity>(string key, string navigationProperty, string relatedKey = null) {
        internalDataService.DeleteRef<TEntity>(key, navigationProperty, relatedKey);
    }
    // For internal use.
    public IEnumerable<T> GetObjects<T>(CriteriaOperator criteriaOperator) {
        return internalDataService.GetObjects<T>(criteriaOperator);
    }
    // GET
    public IQueryable<T> GetObjectsQuery<T>() {
        return internalDataService.GetObjectsQuery<T>();
    }
    // GET with /key
    public T GetObjectByKey<T>(string key) {
        return internalDataService.GetObjectByKey<T>(key);
    }
    // GET with /key and associated property
    public object GetRef<T>(string key, string navigationProperty) {
        return internalDataService.GetRef<T>(key, navigationProperty);
    }
    // PATCH
    public T PatchObject<T>(string key, IObjectDelta<T> delta) where T : class {
        return internalDataService.PatchObject<T>(key, delta);
    }
    // PUT
    public T UpdateObject<T>(string key, IObjectDelta<T> delta) where T : class {
        return internalDataService.UpdateObject<T>(key, delta);
    }
}

Implement the required logic in the endpoint’s method that you wish to change.

// GET
public IQueryable<T> GetObjectsQuery<T>() {
    if(typeof(T) == typeof(ApplicationUser)) {
        // Custom logic
    }
    return internalDataService.GetObjectsQuery<T>();
}

// PATCH
public T PatchObject<T>(string key, IObjectDelta<T> delta) where T : class {
    var original = GetObjectByKey<T>(key);
    // Custom logic before modifications
    delta.Patch(original);
    // Custom logic after modifications
    objectSpace.CommitChanges();
    return original;
}
// ...

Register CustomDataService in the ConfigureServices method after the services.AddXafWebApi method call.

File: MySolution.WebApi\Startup.cs (MySolution.Blazor.Server\Startup.cs)

namespace MySolution.WebApi {
    public class Startup {
        // ...
        public void ConfigureServices(IServiceCollection services) {
            // ...
            services.AddXafWebApi(Configuration, options => {
                // ...
            })
            // in XPO applications, uncomment the following line
            // .AddXpoServices(); 
            services.AddScoped<IDataService, CustomDataService>();
        }
        // ...    
    }
}