カスタムファクトリを使用する

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

Commerce Connectで使用されるすべてのエンティティは、エンティティ ファクトリを使用してカスタマイズできます。エンティティ ファクトリはFactoryデザイン パターンに基づいており、デフォルトの実装は標準のSitecore Factoryに基づいています。

別のファクトリ、依存関係の挿入 (DI)、または制御の反転 (IOC) の実装を使用する場合は、既定の実装を置き換えることができます。

カスタムファクトリを使用するには:

  1. 新しいカスタム・ファクトリー・クラスを作成し、IEntityFactoryインターフェースを実装します。

    インターフェイスには、インスタンス化するエンティティの名前を含む文字列を受け入れる1つのCreateメソッドがあります。

    namespace Sitecore.Commerce.Entities
    {
      /// <summary>
      /// Creates an entity by entity name. The IEnityFactiry allows to substitute the default entity with the extended one.
      /// </summary>
      public interface IEntityFactory
      {
        /// <summary>
        /// Creates the specified entity by name.
        /// </summary>
        /// <param name="entityName">Name of the entity.</param>
        /// <returns>The entity.</returns>
        [NotNull]
        object Create([NotNull] string entityName);
      }
    }
  2. カスタムのEntityFactoryクラスをSitecore.Commerce.configファイルに登録します。これを行うには、entityFactory要素のtype属性値をカスタムEntityFactory型に変更します。

    <!--  ENTITY FACTORY
              Creates an entity by entity name. Allows to substitute default entity with extended one.
        -->
        <entityFactory type=" Sitecore.Commerce.Entities.EntityFactory, Sitecore.Commerce” singleInstance="true" />

    デフォルトの実装では、設定でインスタンス化する実際の型が検索されます。各サービス レイヤーには、commerce.Entitiesと呼ばれる独自のセクションがあります。たとえば、Cartsのデフォルトエンティティは次のとおりです。

    <!--  COMMERCE ENTITIES
              Contains all the Commerce Connect cart entities.
              The configuration can be used to substitute the default entity implementation with extended one.
        -->
        <commerce.Entities>
          <CartBase type="Sitecore.Commerce.Entities.Carts.CartBase, Sitecore.Commerce” />
          <Cart type="Sitecore.Commerce.Entities.Carts.Cart, Sitecore.Commerce” />
          <CartAdjustment type="Sitecore.Commerce.Entities.Carts.CartAdjustment, Sitecore.Commerce” />
          <CartLine type="Sitecore.Commerce.Entities.Carts.CartLine, Sitecore.Commerce” />
          <CartProduct type="Sitecore.Commerce.Entities.Carts.CartProduct, Sitecore.Commerce” />
          <CartOption type="Sitecore.Commerce.Entities.Carts.CartOption, Sitecore.Commerce” />
        </commerce.Entities>
  3. ユーザー定義エンティティを使用するには、新しいEntityクラスを作成します。

  4. commerce.Entities構成セクションにカスタムEntityクラスを登録するには、entityFactory要素の属性値をカスタムEntityFactoryタイプに変更します。

この記事を改善するための提案がある場合は、 お知らせください!