2016-09-27 3 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 ut 나는 이것이 왜 있는지 전혀 모른다. 내 데코레이터를 제대로 설정하지 않았습니까?

올바른 숫자이므로 IValidator 등록이 포함되지 않았습니다. 이 오류는 우리가 IValidator<EntityDTO>을 인스턴스화 할 수없는 이유는 EntityReaderExcludeDefaultEntitiesDecorator이 종속성 문제를 가지고 있기 때문이라고 말하고있는 것 같습니다.

다른 것이 필요한 경우 알려주십시오.

+0

귀하의 질문에 혼란이 있습니다. Exception message는'IValidator '에 관한 것이고 코드는'AbstractValidator '을 보여주고 등록시 IEntityReader를 등록합니다. 나는 길을 잃었다. – Steven

+0

@Steven 죄송합니다. 나는 그것을 조금 업데이트했다. 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

관련 문제