这是我现在拥有的接口/类结构:
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;
}