views:

221

answers:

1

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;
    }
+1  A: 

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>();
Jamie Ide
If you can understand what my case (semantically) requires, can you suggest a alternative implementation?
mare
If you want to continue down the same path you can just create a an `IContentRepository<Page>` implementation. I suggest defining `IPageRepository` (perhaps implementing `IContentRepository<Page>`) implemented by a `XmlPageRepository` implementing it.
Jamie Ide
I'm having hard time understanding what you mean exactly because you use words implemented and implementing a couple of times. If you can clarify it or better yet modify my code in your answer to the level that I can visualize what you mean, I will accept your answer.
mare