Execute Custom Operations on Endpoint Requests(在端点请求上执行自定义操作 )
You can add custom logic to methods that process HTTP requests. For this purpose, modify the built-in Data Service and register your custom implementation. This topic describes the basic steps you need to take:
您可以将自定义逻辑添加到处理HTTP请求的方法中。为此,修改内置数据服务并注册您的自定义实现。本主题描述了您需要采取的基本步骤:
- Create a Custom Data Service(创建自定义数据服务)
- Override Endpoint Methods(覆盖端点方法)
- Register Your Custom Data Service(注册您的自定义数据服务)
Create a Custom Data Service(创建自定义数据服务)
To implement a custom data service, create a class that extends the standard DataService class available in the following namespace: DevExpress.ExpressApp.WebApi.Services.
要实现自定义数据服务,请创建一个类来扩展以下命名空间中可用的标准DataService类:DevExpress.ExpressApp.WebApi.Services。
C#
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Core;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.WebApi.Services;
// ...
public class CustomDataService : DataService {
public CustomDataService(IObjectSpaceFactory objectSpaceFactory,
ITypesInfo typesInfo) : base(objectSpaceFactory, typesInfo) { }
protected override T CreateObject<T>(IObjectDelta<T> delta,
IObjectSpace objectSpace) {
return base.CreateObject(delta, objectSpace);
}
}
Override Endpoint Methods(覆盖端点方法)
To customize an endpoint method in your custom data service, override the corresponding protected method of the base class and add your custom logic to the method’s implementation:
要在自定义数据服务中自定义端点方法,请覆盖基类的相应受保护方法并将自定义逻辑添加到方法的实现中:
C#
// GET
protected override IQueryable<T> GetObjectsQuery<T>(IObjectSpace objectSpace) {
if(typeof(T) == typeof(ApplicationUser)) {
// Custom logic
}
return base.GetObjectsQuery<T>();
}
// PATCH
protected override T PatchObject<T>(string key, IObjectDelta<T> delta,
IObjectSpace objectSpace) where T : class {
var original = GetObjectByKey<T>(key);
// Custom logic before modifications
delta.Patch(original);
// Custom logic after modifications
objectSpace.CommitChanges();
return original;
}
// ...
*Access Object Space(*访问对象空间)
The base DataService class manages Object Spaces internally and passes their correct instances to overridden methods based on the generic type parameter:
基本DataService类在内部管理对象空间,并根据泛型类型参数将其正确的实例传递给被覆盖的方法:
C#
// The method receives an Object Space for the type T
protected override IQueryable<T> GetObjectsQuery<T>(IObjectSpace objectSpace) { /** **/ }
To get an Object Space for a type that is unrelated to the current method, call the base class’s GetObjectSpace method:
要获取与当前方法无关的类型的对象空间,请调用基类的GetObjectSpace方法:
C#
var userObjectSpace = GetObjectSpace<User>();
Keep in mind that you should not manually dispose of Object Spaces obtained through GetObjectSpace. The data service correctly disposes of them in the base Dispose method implementation.
请记住,您不应该手动处置通过GetObjectSpace获得的对象空间。数据服务在基本Dispose方法实现中正确处置它们。
If required, you can also use the IObjectSpaceFactory available through the data service class constructor to create a new Object Space. In this case, it is your responsibility to dispose of the created Object Space. To do it safely, we recommend that you override the data service’s base Dispose method and use it to call the Dispose method for all manually created Object Spaces.
如果需要,您还可以使用通过数据服务类构造函数提供的IObjectSpaceFactory来创建新的对象空间。在这种情况下,您有责任处理创建的对象空间。为了安全,我们建议您覆盖数据服务的基本Dispose方法,并使用它来调用所有手动创建的对象空间的Dispose方法。
C#
public class CustomDataService : DataService {
private readonly IObjectSpace myTypeObjectSpace;
public CustomDataService(IObjectSpaceFactory objectSpaceFactory, ITypesInfo typesInfo) : base(objectSpaceFactory, typesInfo) {
myTypeObjectSpace = objectSpaceFactory.CreateObjectSpace(typeof(MyType));
}
// ...
public override void Dispose() {
base.Dispose();
myTypeObjectSpace.Dispose();
}
}
Register Your Custom Data Service(注册您的自定义数据服务)
Use the code below to register your custom data service. Add the registration line to the ConfigureServices method, after the AddXafWebApi() method call.
使用下面的代码注册您的自定义数据服务。在AddXafWebApi()方法调用之后,将注册行添加到配置服务方法。
File: MySolution.WebApi\Startup.cs (MySolution.Blazor.Server\Startup.cs)
C#
namespace MySolution.WebApi {
public class Startup {
// ...
public void ConfigureServices(IServiceCollection services) {
services.AddXafWebApi(builder => {
// In XPO applications, uncomment the following line:
// builder.AddXpoServices();
// ...
}, Configuration);
services.AddScoped<IDataService, CustomDataService>();
}
// ...
}
}