---
title: "カスタム工場を使いましょう"
slug: "use-a-custom-factory"
abstract: ""
---
# カスタム工場を使いましょう

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

もし別の工場、依存注入(DI)や制御の反転(IOC)実装を好みますが、デフォルトの実装を置き換えることが可能です。

カスタム工場を使うには:

1.  新しいカスタムファクトリークラスを作成し、IEntityFactoryインターフェースを実装します。
    
    インターフェースには、インスタンス化するエンティティ名を含む文字列を受け入れるCreateメソッドが1つあります。
    
    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.  Sitecore.Commerce.configファイルにカスタムEntityFactoryクラスを登録します。これを行うには、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と呼ばれる独自のセクションがあります。例えば、以下はカートのデフォルトエンティティです:
    
    <!-- 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.  カスタムエンティティを使用するには、新しいエンティティクラスを作成します。
    
4.  entityFactory要素の属性値をカスタムEntityFactory型に変更して、commerce.Entities設定セクションでカスタムエンティティクラスを登録します。