0

我正在尝试使用抽象类为大多数方法实现一个子接口,并使用这个抽象类的一个子类来实现缺少的方法。但是编译器不断抛出这些错误:“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);
        }
    }
}
4

0 回答 0