2010-06-29 4 views
1

와 저장소 패턴이 지금 가지고있는 인터페이스/클래스 구조입니다 :Ninject에와 인터페이스

BaseContentObject 추상 클래스

public abstract class BaseContentObject : IEquatable<BaseContentObject> 
{ 
... 
} 

페이지 구상 클래스

public class Page : BaseContentObject 
{ 
... 
} 

저장소 인터페이스

public interface IContentRepository<T> 
    { 
     // common methods for all content types 
     void Insert(T o); 
     void Update(T o); 
     void Delete(string slug); 
     void Delete(ContentType contentType, string slug); 
     IEnumerable<T> GetInstances(); 
     T GetInstance(ContentType contentType, string slug); 
     T GetInstance(string contentType, string slug); 
     T GetInstance(string slug); 
     IEnumerable<string> GetSlugsForContentType(int limit = 0, string query = ""); 
     ContentList GetContentItems(); 
     bool IsUniqueSlug(string slug); 
     string ObjectPersistanceFolder { get; set; } 
    } 

공통 인터페이스 구현

public class XmlRepository<T> : IContentRepository<BaseContentObject> 
    { 
     public string ObjectPersistanceFolder { get; set; } 

     public XmlRepository() 
     { 
      ObjectPersistanceFolder = Path.Combine(XmlProvider.DataStorePhysicalPath, typeof(T).Name); 
      if (!Directory.Exists(ObjectPersistanceFolder)) 
       Directory.CreateDirectory(ObjectPersistanceFolder); 
     } 
... 
} 

내용 특정 저장소 (BaseContentObject 클래스를 상속 모든 콘텐츠 클래스)

public class XmlPagesRepository : XmlRepository<Page> { } 
global.asax.cs에서

Ninject에 규칙

Bind<IContentRepository<Page>>().To<XmlPagesRepository>(); 

다음을 제공하는 컴파일 시간 오류 :

*The type 'Namespace.XmlPagesRepository' cannot be used as type parameter 'TImplementation' in the generic type or method 'Ninject.Syntax.IBindingToSyntax<T>.To<TImplementation>()'. There is no implicit reference conversion from 'Namespace.XmlPagesRepository' to 'Namespace.IContentRepository<Namespace.Page>'.* 

저는 비즈니스 요구를 지원하기 위해 클래스와 인터페이스 구조를 파악하는 데 많은 시간을 투자했습니다. 이제 나는 그 Ninject 오류를 피하는 법을 모른다.

나는 다음과 같은 ASP.NET MVC 컨트롤러에서이 구조를 사용하려면 :

public IContentRepository<Page> ContentRepository { get; private set; } 

    public PageController(IContentRepository<Page> repository) 
    { 
     ContentRepository = repository; 
    } 
+0

wouldnt가 완전히 당신이 뭘 하려는지 이해없이, 이것은 내가 저장소를 설계 할 방법입니다 컴파일 시간 오류를주는 상처 - 그것은이 문제를 치고 큰 문제로 간주하지 않고 그것을 해결 한 사람들에게 이것을 열어 놓을 것입니다. –

답변

2

난 당신이 구체적인 클래스를 사용하여 테스트 케이스를 작성하는 경우, 당신은 실제로 당신이 암시 적으로 변환 할 수 없습니다 찾을 수있을 거라 생각 XmlPagesRepository에서 IContentRepository<Page>까지 그것은 따라 어렵지만, 그 변환이 가능하다면 그때는 ToMethod를 사용하여 바인딩 생각 :

Bind<IContentRepository<Page>>().ToMethod(x => (IContentRepository<Page>)kernel.Get<XmlPagesRepository>()); 

편집 : 좀 더, 변환 할 수없는이보고에서. XmlRepository<Page>IContentRepository<BaseContentObject>이 아니며 IContentRepository<Page>을 구현합니다. Page가 BaseContentObject이면 상관 없습니다. 캐스트는 불가능합니다. 이것은 의도 한대로 작동하지 않습니다.

편집 2 : "구현"은 인터페이스를 구현하는 것을 의미합니다. 인터페이스를 구현하고 클래스를 상속 (또는 확장)합니다.

public interface IPageRepository : IContentRepository<Page> 
{ 
} 

public interface XmlPageRepository : IPageRepository 
{ 
    // implementation 
} 

이제 IPageRepository의 여러 구현을 가지고 사용하여 적절한 하나를 바인딩 할 수 있습니다 Ninject에 :

Bind<IPageRepository>().To<XmlPageRepository>(); 
+0

내 의미 (semantically)가 필요한 것을 이해할 수 있다면 대체 구현을 제안 할 수 있습니까? – mare

+0

동일한 경로를 계속 진행하려면 'IContentRepository '구현을 만들면됩니다. 나는 그것을 구현하는'XmlPageRepository'에 의해 구현 된'IPageRepository' (아마도'IContentRepository '을 구현)을 정의 할 것을 제안합니다. –

+0

구현 된 단어를 사용하고 몇 시간을 구현했기 때문에 정확히 무슨 뜻인지 이해하는 데 어려움을 겪고 있습니다. 당신이 의미하는 것을 시각화 할 수있는 레벨에 대한 대답에서 코드를 명확히하거나 더 나은 코드를 수정할 수 있다면, 나는 당신의 대답을 받아 들일 것입니다. – mare