2010-06-29 74 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; } 
    } 

通用接口实现(继承BaseContentObject类中的所有内容类)

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); 
     } 
... 
} 

内容库特定

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

难道不伤害到编译时错误 - 它会向那些遇到这个问题并解决它的人开放,甚至没有考虑到它是一个大问题。 – 2010-06-30 08:22:25

回答

2

我认为,如果你使用的具体类创建一个测试情况下,你会发现,你确实不能隐式转换从XmlPagesRepositoryIContentRepository<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

如果你能理解我的情况(语义)需要什么,你能建议一个替代实现吗? – mare 2010-06-30 16:46:16

+0

如果您想继续使用相同的路径,您可以创建一个'IContentRepository '实现。我建议定义'IPageRepository'(可能实现'IContentRepository '),由实现它的'XmlPageRepository'实现。 – 2010-06-30 23:01:03

+0

我很难理解你的意思,因为你使用了实现并执行了几次的单词。如果你可以澄清它或更好,但修改我的代码在答案中的水平,我可以想象你的意思,我会接受你的答案。 – mare 2010-07-02 00:05:07