次の方法で共有


データ アクションの上書き

この記事では、Dynamics 365 Commerce でカスタム データ アクションを使用して既定のデータ アクションをオーバーライドする方法について説明します。

Dynamics 365 Commerce ソフトウェア開発キット (SDK) モジュール ライブラリに含まれるモジュールは、既存のデータ アクションを使用して、使用するモジュールのデータを取り込みます。 カスタム データ アクションを使用してビジネス ロジックを変更する場合に、そのモジュールで表示される UX を変更しないので、モジュールを複製しない場合があります。 データ アクションのオーバーライドにより、同じアクション ID を持つ新しいアクションを作成することによって、登録されているアクションをオーバーライドすることができます。 データ アクションをオーバーライドすると、以前のすべてのデータ アクションが使用され、モジュール definition.json ファイルをインポートまたは含めて、新しいデータ アクションを使用します。

データ アクション ID の名前付け規則

各データ アクションには、createObservableDataAction メソッドが "ID" プロパティを通して呼び出される場合に使用されるデータ アクション ID があります。 次の例のように、データ アクション ID は、[パッケージ名]/[モジュール名]/[アクション名] の名前付け規則に従っている必要があります。

export default createObservableDataAction({
    id: '@msdyn365-commerce-modules/retail-actions/get-address',
    action: <IAction<Address[]>>getAddressAction,
    input: <(args: ICreateActionContext) => IActionInput>createGetAddressInput
});

データ アクションのオーバーライドの例

get-address データ アクションをオーバーライドするには、/src/actions ディレクトリで get-address ID を使用する新しいデータ アクションを作成する必要があります。

次のコマンドにより、\src\actions ディレクトリに新しいデータ アクションが作成されます。

yarn msdyn365 add-data-action custom-get-address

次に、既定のテンプレート コードをユーザーのコードで置き換えます。 次の例において、データ アクション ID は ...\Msdyn365.Commerce.Online\node_modules@msdyn365-commerce-modules\retail-actions\dist\lib\get-address.js データ アクション @msdyn365-commerce-modules/retail-actions/get-address で使われているものと同じです。

import { CacheType, IAction, IActionContext, IActionInput, ICommerceApiSettings } from '@msdyn365-commerce/core';
import { createObservableDataAction, ICreateActionContext } from '@msdyn365-commerce/core';
import { Address } from '@msdyn365-commerce/retail-proxy';

import { readAsync } from '@msdyn365-commerce/retail-proxy/dist/DataActions/CustomersDataActions.g';

/**
 *  Input class for the getAddress data action
 */
export class AddressInput implements IActionInput {
    public userAccountNumber: string;
    private apiSettings: ICommerceApiSettings;

    constructor(userAccountNumber: string, apiSettings: ICommerceApiSettings) {
        this.userAccountNumber = userAccountNumber;
        this.apiSettings = apiSettings;
    }

    public getCacheKey = () => this.userAccountNumber
    public getCacheObjectType = () => 'GetAddress';
    public dataCacheType = (): CacheType => 'request';
}

/**
 * createInput method for the getAddress method
 * @param inputData The input data passed to the createInput method
 */
export const createGetAddressInput = (inputData: ICreateActionContext): IActionInput => {
    const { requestContext } = inputData;
    if (!requestContext.user.isAuthenticated || !requestContext.user.customerAccountNumber) {
        throw new Error('Unable to create address input.  User is not authenticated.');
    }

    return new AddressInput(requestContext.user.customerAccountNumber, inputData.requestContext.apiSettings);
};

/**
 * The action method for the getAddress data action
 * @param input The action input
 * @param ctx The action context
 */
export async function getAddressAction(input: AddressInput, ctx: IActionContext): Promise<Address[]> {

    const customer = await readAsync({ callerContext: ctx }, input.userAccountNumber);

    if (!customer) {
        throw new Error('Not able to get customer');
    }

    // fetch customer address from external source
    return customer.Addresses || [];
}

/**
 * The getAddress data action
 * Gets a customers information via the read RetailServer API
 * Returns address information associated with the retrieved customer
 */
export default createObservableDataAction<Address[]>({
    id: '@msdyn365-commerce-modules/retail-actions/get-address',
    action: <IAction<Address[]>>getAddressAction,
    input: <(args: ICreateActionContext) => IActionInput>createGetAddressInput
});

追加リソース

チェーン データ アクション

バッチ データ アクション

監視可能なデータ アクションの作成

モジュール間での状態の共有

データ アクション キャッシュの設定

データ アクションのフック