2009-11-17 7 views
6

처음으로 오랫동안 독자가 ​​여기 있습니다. 현재 Ninject를 사용하여 간단한 C# .NET 응용 프로그램 용 Castle Windsor의 최신 버전으로 전환하려고합니다.성 윈저 : 여러 생성자의 문제점

대부분의 경우 변환이 성공적으로 완료되었으며 컨테이너 구현이 완벽하게 실행되었습니다. 그러나 저장소 개체에 작은 문제가 있습니다.

<castle> 
    <components> 
     <component id="objectContextFactory" lifestyle="custom" 
        customLifestyleType="Common.Infrastructure.PerWebRequestLifestyleManager, Common.Castle" 
        service="Project.DAL.Context.IObjectContextFactory, Project.DAL.LINQ" 
        type="project.DAL.Context.ObjectContextFactory, Project.DAL.LINQ"> 
     </component> 
     <component id="userRepository" lifestyle="custom" 
        customLifestyleType="Common.Infrastructure.PerWebRequestLifestyleManager, Common.Castle" 
        service="Project.BL.Repository.IUserRepository, Project.BL" 
        type="Project.BL.Repository.UserRepository, Project.BL.LINQ"> 
      <parameters> 
       <factory>${objectContextFactory}</factory> 
      </parameters> 
     </component> 
    </components> 
</castle> 

:

public class UserRepository : IUserRepository { 
    public UserRepository(IObjectContext objectContext) { 
     // Check that the supplied arguments are valid. 
     Validate.Arguments.IsNotNull(objectContext, "objectContext"); 

     // Initialize the local fields. 
     ObjectContext = objectContext; 
    } 

    public UserRepository(IObjectContextFactory factory) 
     : this(factory.CreateObjectContext()) { 
    } 

    // ----------------------------------------------- 
    // Insert methods and properties... 
    // ----------------------------------------------- 
} 

내가 내 응용 프로그램의 구성 파일에서 다음 항목을 설정을 가지고,이 코드에 대응 :

나는 다음과 같은 방식으로 코딩 된 사용자 저장소 개체가 나에게 모든 것은해야하는 것처럼 보인다. IObjectContextFactory 서비스의 인스턴스를 분석하려고하면 ObjectContextFactory 객체를 검색합니다. 내 문제는 내가 시도하고 IUserRepository 서비스의 인스턴스를 해결할 때 온다. 나는 다음과 같은 유쾌한 예외에 대해 대우를받습니다.

'userRepository'구성 요소를 작성할 수 없으므로 만족스럽지 않습니다. userRepository는 다음과 같은 의존성을 기다리고 있습니다 :

서비스 :

- SandCastle.DAL.Context.IObjectContext which was not registered.

나는 이것에 생각할 수있는 모든 노력을했습니다. 그래서, 당신 stackoverflow 독자, 내가 말 : 어떤 아이디어있어?

답변

7

이것은 버그로 간주 될 수 있지만 (실제로는 고칠 수있는 경우와 같이) 문제가 될 수 있습니다.

윈저는 가장 만족스러운 생성자 (대부분의 매개 변수가있는)와 일치하도록 시도합니다.

그러나 귀하의 경우 가장 많은 수의 매개 변수를 가진 생성자가 두 개 있으므로 Windsor가 첫 번째 매개 변수를 선택하기 때문에 "첫 번째"수단은 정의되지 않습니다.

실제로 소스 코드에서 생성자의 순서를 전환하면 코드가 제대로 작동하지 않을 수 있지만 코드화되지 않은 동작에 의존하여 코드가 작동하기 시작합니다.

어디에서 시작해야할까요?

나는 가장 만족스러운 생성자가 없기 때문에 Windsor가 혼란 스럽다고 말했습니다.

public class UserRepository : IUserRepository { 
    public UserRepository(IObjectContext objectContext, object fakeIgnoreMe) { 
     // Check that the supplied arguments are valid. 
     Validate.Arguments.IsNotNull(objectContext, "objectContext"); 
     // ignoring fake additional argument 
     // Initialize the local fields. 
     ObjectContext = objectContext; 
    } 

    public UserRepository(IObjectContextFactory factory) 
     : this(factory.CreateObjectContext()) { 
    } 

    // ----------------------------------------------- 
    // Insert methods and properties... 
    // ----------------------------------------------- 
} 

은 그래서 것을 Castle users list 또는 straight to issue tracker에이 문제를 신고 해주세요 :

신속하고 잘 정의 된 수정은 매개 변수의 서로 다른 번호를 가질 수 있도록 일 생성자 중 하나에 가짜 매개 변수를 추가하는 것입니다 고정되다.

+0

gangbusters처럼 일했다! 감사! – highvoltage

+2

"Windsor는 가장 탐욕스러운 생성자 (가장 만족스러운 매개 변수를 가진 생성자)를 찾습니다." "해결할 수있는 매개 변수 만 포함하는 생성자 목록에서 가장 많은 매개 변수가있는 생성자가 선택되었습니다"라고 말하는 것이 더 정확하다고 생각합니다. – Steven

+0

@Steven 어때? –

0

윈저 3.2 이후.x

Castle.Core.DoNotSelectAttribute 특성이 생성자에 적용되면 다른 조건에도 불구하고 해당 특성이 선택되지 않습니다.

public class UserRepository : IUserRepository 
{ 
    [DoNotSelect] // This constructor will be ignored by Windsor 
    public UserRepository(IObjectContext objectContext) 
    { 
     // ... 
    } 

    public UserRepository(IObjectContextFactory factory) 
     : this(factory.CreateObjectContext()) {} 
} 

은 참조 : https://github.com/castleproject/Windsor/blob/86696989a7698c45b992eb6e7a67b765b48108b0/docs/how-constructor-is-selected.md