IRepository<TObject>
을 상속하는 IRepository 인터페이스가 있습니다. 또한 IRepository<TObject>
구현하는 S QLRepository<TObject>
, 상속하는 SqlRepository 클래스가 있습니다. IRepository로 SqlRepository의 인스턴스를 인스턴스화 할 수없는 이유는 무엇입니까? 다음 오류 메시지와 함께, Controller 클래스에 _repository하기 위해 새로운 SqlRepository를 할당 할 때일반 클래스를 일반 인터페이스로 변환 할 수없는 이유는 무엇입니까?
public class MyObject : IObject {
...
}
public interface IRepository<TObject> where TObject : IObject, new() {
...
}
public interface IRepository : IRepository<MyObject> { }
public class SqlRepository<TObject> : IRepository<TObject> where TObject : IObject, new() {
...
}
public class SqlRepository : SqlRepository<MyObject> { }
public class Controller {
private IRepository _repository;
public Controller() {
_repository = new SqlRepository();
}
}
위의 예는 실패합니다.
Argument '1': cannot convert from 'SqlRepository' to 'IRepository'
내가 이해하지 못한 상속의 기본 원칙은 무엇입니까?
나는 SqlRespository를 IRepository (공개 클래스 SqlRepository : SqlRepository, IRepository {})를 구현함으로써 문제를 해결했는데, 이는 다소 불필요한 것으로 보이지만 논리를 이해하고 있다고 생각합니다. –
kim3er