2016-09-27 6 views
1

リファクタリング中に、私はベースリポジトリからいくつかのデコレータに多くの機能を移動しようとしていました。何らかの理由で循環的な依存エラーが発生しています。完全なエラーは次のとおりです。シンプルインジェクタ:条件付きデコレータの周期的依存性エラー

設定が無効です。タイプIValidator<EntityDTO>のインスタンスを作成できませんでした。構成が無効です。タイプEntityReaderExcludeDefaultEntitiesDecoratorは、それ自体に直接的または間接的に依存します。ここで

ここに私のIValidator実装(流暢検証)

public class EntityValidator : IValidator<EntityDTO> 
{ 
    private readonly Data.IEntityReader _repo; 

    public EntityValidator(Data.IEntityReader repo) 
    { 
     _repo = repo; 

     RuleFor(e => e.GroupId).NotEqual(Guid.Empty) 
      .WithMessage("The selected group is invalid"); 
     // more rules 
    } 
} 

は私のデコレータの実装が

public class EntityReaderExcludeDefaultEntitiesDecorator : IEntityReader 
{ 
    private readonly IEntityReader _reader; 

    public EntityReaderExcludeDefaultEntitiesDecorator(IEntityReader reader) 
    { 
     _reader = reader; 
    } 

    public EntityDTO FindById(Guid id) 
    { 
     var entity = _reader.FindById(id); 
     if (entity.Name.Equals(DocumentConstants.DEFAULT_ENTITY_NAME)) return null; 
     return entity; 
    } 

    // more methods 
} 

されており、ここに私の設定は、私は

container.RegisterConditional(typeof(IEntityWriter), 
    typeof(Service.Decorators.EntityWriterValidationDecorator), 
    context => context.Consumer.ServiceType != typeof(IGroupWriter)); 

// Do not use the decorator in the Document Writer (We need to find the 'None' entity 
container.RegisterConditional(typeof(IEntityReader), 
    typeof(Service.Decorators.EntityReaderExcludeDefaultEntitiesDecorator), 
    context => context.Consumer.ServiceType != typeof(IDocumentWriter)); 

container.RegisterConditional<IEntityWriter, DocumentEntityWriter>(c => !c.Handled); 
container.RegisterConditional<IEntityReader, DocumentEntityReader>(c => !c.Handled); 

デコレータのためですより多くの情報を与えるだろうb私はこれがなぜなのか分かりません。デコレータを正しくセットアップしなかったのですか?

登録が正しく行われたため、IValidatorの登録が含まれていませんでした。このエラーは、IValidator<EntityDTO>をインスタンス化できない理由は、EntityReaderExcludeDefaultEntitiesDecoratorに依存関係の問題があることを示しているようです。

他に必要な場合は、教えてください。

+0

あなたの質問は混乱しています。あなたのコードが 'AbstractValidator 'を示し、あなたの登録に 'IEntityReader'を登録している間、例外メッセージは' IValidator 'について話します。迷っています。 – Steven

+0

@スティーブン申し訳ありません。私は少しそれを更新しました。しかしIValidatorの登録は正しいです。その例外はデコレータに問題があると言われているので、IValidatorの登録はしていません。それは問題に関係しているようには見えなかった。 – Carson

答えて

0

私は自分自身にデコレータを注入していないことを確認するためにチェックを追加して修正しました。これはどうすればいいのでしょうか?

// Do not validate when adding from a group 
container.RegisterConditional(typeof(IEntityWriter), 
    typeof(Decorators.EntityWriterValidationDecorator), 
    context => context.Consumer.ServiceType != typeof(IGroupWriter) 
     && context.Consumer.ImplementationType != context.ImplementationType); 

// Do not use the decorator in the Document Writer (We need to find the 'None' entity 
container.RegisterConditional(typeof(IEntityReader), 
    typeof(Service.Decorators.EntityReaderExcludeDefaultEntitiesDecorator), 
    context => context.Consumer.ServiceType != typeof(IDocumentWriter) 
     && context.Consumer.ImplementationType != context.ImplementationType); 
+0

あなたがしていることは正しいです。 'context.ImplementationType'は単に' typeof(EntityWriterValidationDecorator) 'を返すので、あなたの' context.Consumer.ImplementationType!= context.ImplementationType'ステートメントは単に "コンシューマ型は型自体ではありません"と尋ねます。ステートメントを 'context.Consumer.ImplementationType!= typeof(EntityWriterValidationDecorator)'に変更すると、もう少し自明です。 – Steven

+0

@スティーブン私は同意します。この声明をより明示的に変更する予定です。ありがとう! – Carson

関連する問題