学习009-08-02 Execute Custom Operations on Endpoint Requests(在端点请求上执行自定义操作 )

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>();
        }
        // ...
    }
}
  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值