SeanKilleen
SeanKilleen

Reputation: 8977

Error on implementing interface

This will likely (hopefully?) be an "amateur hour" type question. :)

I'm new to interfaces, etc. and doing things correctly, but I have an MVC 3 project that references a C# library project.

In the C# library project, I have the following code:

    public interface IRepositoryAddable<T>
    {
        void Add(T entity);
    }

I repeat this sort of code for other types of repository so that I can make things easier and standard across all my repositories.

For example, I have a repository for anything of type "ISkill":

public interface ISkillRepository : IRepositoryAddable<ISkill>, IRepositoryDeleteable<ISkill>, IRepositoryDeleteableByID<ISkill, int>, IRepositoryGettableByID<ISkill, int>, IRepositoryListable<ISkill>, IRepositorySavable<ISkill>
{ }

Then, in my actual repository, I have:

    public class SkillRepository : ISkillRepository
    {
        public void Add(Skill skillToAdd)
        {
            return;
        }
}

As far as I'm aware, this should suffice to implement the ISkillRepositoryListable interface. However, I receive the following error:

Error 5 'DakotaSkills.MVC.Models.Repository.SkillRepository' does not implement interface member 'DakotaSkills.Lib.Interfaces.Repository.IRepositoryAddable.Add(DakotaSkills.Lib.Interfaces.ISkill)' C:\Users\Sean\Projects\Web\DakotaSkills\src\DakotaSkills.MVC\Models\Repository\SkillRepository.cs 11 18 DakotaSkills.MVC

Other interface methods for the repository have shown as being implemented fine and I'm not quite sure what I'm doing wrong. For the record, my "Skill" type implements "ISkill" and I receive no error on its implementation.

Help?

Thanks!

Upvotes: 1

Views: 1127

Answers (5)

Adam Straughan
Adam Straughan

Reputation: 2848

I'd need to see your call site, i.e. the calling code

However this sounds too complicated. Read this and have a think http://ayende.com/Blog/archive/2011/03/24/the-wages-of-sin-hit-that-database-one-more-timehellip.aspx

Upvotes: -1

KOTJMF
KOTJMF

Reputation: 991

Skill is a more specific type than ISkill, so I think you need to change your implementation of Add to accept ISkill, rather than Skill:

    public void Add(ISkill skillToAdd)
    {
        return;
    }

Upvotes: 1

Mark Brackett
Mark Brackett

Reputation: 85645

You've defined the interface as having:

void Add(ISkill skillToAdd)

but, you're implementing

void Add(Skill skillToAdd)

which means they don't match. If I have a different ISkill implementation, I can't use your SkillRepository.

You either need to make SkillRepository work with any ISkill, or change your ISkillRepoistory interface to use a concrete Skill as its type parameters.

Upvotes: 2

John Farrell
John Farrell

Reputation: 24754

Should be:

public class SkillRepository : ISkillRepository
{
    public void Add(ISkill skillToAdd)
    {
        return;
    }
}

Upvotes: 1

Kate Gregory
Kate Gregory

Reputation: 18944

Your Add would not accept some other class that also implemented ISkill. You need to take an ISkill, not just a Skill. That also suggests that your implementation of Add, should it ever do anything, should do so through ISkill only and not by calling other Skill methods you may know about.

Upvotes: 4

Related Questions