Castle Windsor가 매개 변수가없는 생성자를 호출하는 방법은 무엇입니까?
-
03-07-2019 - |
문제
현재 다음과 같은 수업이 있습니다.
public class MyClass : IMyClass
{
public MyClass()
{
//...
}
public MyClass(IMyRepository repository)
{
//...
}
}
내 구성 파일에는 imyclass가 등록되었지만 iMyRepository는 없습니다. 내 의도는 Windsor가 매개 변수를 사용하지 않는 생성자를 사용하는 것입니다. 그러나이 메시지를 받고 있습니다.
만족할 종속성이 있으므로 구성 요소 'myclass'를 만들 수 없습니다. MyClass는 다음의 종속성을 기다리고 있습니다.
서비스 : - 등록되지 않은 Namespace.imyrepository.
나는 찾았다 다른 게시물 즉, 컨테이너는 만족할 수있는 가장 많은 주장을 제작자에게 호출 할 것이라고 말합니다. 그렇다면 왜 만족하는 방법을 모른다는 주장으로 생성자를 호출하려고 하는가?
해결책
아마도 당신은 오래된 버전의 Windsor를 사용하고있을 것입니다 ... 이것은 나에게 잘 작동합니다.
[TestFixture]
public class WindsorTests {
public interface ISomeInterface {}
public class AService {
public int Id { get; private set; }
public AService() {
Id = 1;
}
public AService(ISomeInterface s) {
Id = 2;
}
}
[Test]
public void Parameters() {
var container = new WindsorContainer();
container.AddComponent<AService>();
var service = container.Resolve<AService>();
Assert.AreEqual(1, service.Id);
}
}
제휴하지 않습니다 StackOverflow