Ninject and repository pattern with interfaces

Ninject and repository pattern with interfaces

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


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

Deploy ASP.NET website alongside ASP.NET MVC website


style problem with data returned from action method
I think if you create a test case using the concrete classes you'll find this indeed you can't implicitly convert from XmlPagesRepository to IContentRepository<Page>. Email errors in a mvc applicationIt's hard to follow, although if this conversion is possible then I think bind it using ToMethod:. ASP.NET MVC: First access after some minutes slow, then every following request is fast
Bind<IContentRepository<Page>>().ToMethod(x => (IContentRepository<Page>)kernel.Get<XmlPagesRepository>()); 
Edit: In looking at this any more, the conversion isn't possible. How to get all input controls which are of type file in jqueryXmlRepository<Page> implementsIContentRepository<BaseContentObject> not IContentRepository<Page>. Razor view engine - exception when calling Any FunctionIt doesn't matter this Page is a BaseContentObject, the cast isn't possible. Write css for individual page in mvc3 projectThis isn't going to job as you intended.. WebForms app that runs MVC in a subfolder Edit2: "Implement" refers to implementing an interface; you implement an interface and inherit from (or extend) a class. Without fully understanding what you're endeavor to do, this is how I would design the repositories:.
public interface IPageRepository : IContentRepository<Page> { }  public interface XmlPageRepository : IPageRepository {     // implementation } 
You must now have multiple implementations of IPageRepository and bind the appropriate one using Ninject:.

64 out of 100 based on 49 user ratings 524 reviews