什么" MyProject.Repository.EF.AccountRepository'没有实现接口成员"实际意思?

时间:2013-01-17 01:51:44

标签: c# .net interface visual-studio-2012

我正在尝试使用Abstract类为大多数方法实现子接口,并尝试使用此抽象类的子类来实现缺少的方法。但是编译器不断抛出这些错误:“Foo.Bar”没有实现接口成员“。 经过长时间盯着代码并在StackOverflow上阅读大量类似问题后,我一直没有看到问题所在。 :S

这些是我所拥有的代码中的一些“无数”错误,我并不是真的看到发生了什么。

Error 2   
'MyProject.Repository.EF.AccountRepository' does not implement interface member 'MyProject.Entities.IRepository<MyProject.Entities.Account,int>.FindAll()'. 
'MyProject.Repository.EF.RepositoryBase<MyProject.Repository.EF.Account,int>.FindAll()' cannot implement 'MyProject.Entities.IRepository<MyProject.Entities.Account,int>.FindAll()' because it does not have the matching return type of 'System.Collections.Generic.IEnumerable<MyProject.Entities.Account>'.  
D:\Projects\WebProjects\MyProject\MyProject.Repository.EF\AccountRepository.cs  5   18  MyProject.Repository.EF

//

这是代码

//

//存储库界面

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace MyProject.Entities 
{
    public interface IRepository<T, EntityKey> {
        void Save();
        void Add(T entity);
        void Remove(T entity);

        T FindBy(EntityKey id);
        IEnumerable<T> FindBy(Expression<Func<T, bool>> query);
        IEnumerable<T> FindBy(Expression<Func<T, bool>> query, int index, int count);
        IEnumerable<T> FindAll();
    }
}

//

//“儿童”界面

namespace MyProject.Entities 
{
    public interface IAccountRepository : IRepository<Account, int> {
    }
}

//

//从接口实现几乎所有方法的抽象类,并声明其中两个抽象由子类实现

using MyProject.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace MyProject.Repository.EF 
{
    public abstract class RepositoryBase<T, EntityKey> : IRepository<T, EntityKey> where T : class {

        private IQueryable<T> objectSet;
        protected MyProjectEntitiesContext Context;

        public abstract string GetEntitySetName();
        public abstract T FindBy(EntityKey id);

        public IQueryable<T> ObjectSet
        {
            get { return objectSet; }
            set { objectSet = value; }
        }

        public void Save()
        {
            this.Context.SaveChanges();
        }

        public void Add(T entity)
        {
            this.Context.AddObject(this.GetEntitySetName(), entity);
        }

        public void Remove(T entity)
        {
            this.Context.DeleteObject(entity);
        }

        public IEnumerable<T> FindAll()
        {
            return this.ObjectSet;
        }

        public IEnumerable<T> FindBy(Expression<Func<T, bool>> query)
        {
            return this.ObjectSet.Where(query);
        }

        public IEnumerable<T> FindBy(Expression<Func<T, bool>> query, int index, int count)
        {
            return this.FindBy(query).Skip(index).Take(count);
        }
    }
}

//

//此处正在实施两种缺失的方法

using System.Linq;
using MyProject.Entities;

namespace MyProject.Repository.EF {
    public class AccountRepository : RepositoryBase<Account, int>, IAccountRepository {
        public AccountRepository()
        {
            this.Context = MyProjectEntitiesFactory.GetDatacontext();
            this.ObjectSet = this.Context.Accounts;
        }

        public override string GetEntitySetName()
        {
            return this.Context.Accounts.EntitySet.Name;
        }

        public override Account FindBy(int id)
        {
            return this.ObjectSet.FirstOrDefault(i => i.Id == id);
        }
    }
}

0 个答案:

没有答案
相关问题