Reputation: 13083
This is the interfaces/class structure I have now:
BaseContentObject abstract class
public abstract class BaseContentObject : IEquatable<BaseContentObject>
{
...
}
Page concrete class
public class Page : BaseContentObject
{
...
}
Repository interface
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; }
}
Common interface implementation (for all content classes that inherit BaseContentObject class)
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);
}
...
}
Content specific repository
public class XmlPagesRepository : XmlRepository<Page> { }
Ninject rule in global.asax.cs
Bind<IContentRepository<Page>>().To<XmlPagesRepository>();
that gives the following compile time error:
*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>'.*
I've spent quite some time figuring out my classes and interfaces structure to support my business needs. Now I don't know how to get past that Ninject error.
I want to use this structure in ASP.NET MVC controllers like this:
public IContentRepository<Page> ContentRepository { get; private set; }
public PageController(IContentRepository<Page> repository)
{
ContentRepository = repository;
}
Upvotes: 1
Views: 2824
Reputation: 49251
I think if you create a test case using the concrete classes you'll find that indeed you can't implicitly convert from XmlPagesRepository
to IContentRepository<Page>
. It's hard to follow, but if that conversion is possible then I think bind it using ToMethod
:
Bind<IContentRepository<Page>>().ToMethod(x => (IContentRepository<Page>)kernel.Get<XmlPagesRepository>());
Edit: In looking at this some more, the conversion isn't possible. XmlRepository<Page>
implementsIContentRepository<BaseContentObject>
not IContentRepository<Page>
. It doesn't matter that Page is a BaseContentObject, the cast isn't possible. This isn't going to work as you intended.
Edit2: "Implement" refers to implementing an interface; you implement an interface and inherit from (or extend) a class. Without fully understanding what you're trying to do, this is how I would design the repositories:
public interface IPageRepository : IContentRepository<Page>
{
}
public interface XmlPageRepository : IPageRepository
{
// implementation
}
You can now have multiple implementations of IPageRepository and bind the appropriate one using Ninject:
Bind<IPageRepository>().To<XmlPageRepository>();
Upvotes: 2