在接口中使用通用方法

时间:2014-10-20 11:28:04

标签: c# generics inheritance methods interface

请帮我更正此代码。

我在课程中遇到编译错误

public interface IGenericSaveRepository
{
    void Save<TEntity>(int id, ICollection<TEntity> entities) where TEntity : class;
}


public class GenericSaveRepository<TEntity> where TEntity : class,IGenericSaveRepository
{
    private IUnitofWork<TEntity> _unitofWork;
    private NaijaSchoolsContext _context;
    public GenericSaveRepository(NaijaSchoolsContext context)
    {
        _context = context;
        _unitofWork = new UnitofWork<TEntity>(_context);
    }
    public void Save(int id, ICollection<TEntity> entities)
    {
        foreach (var entity1 in entities)
        {
            //entity.Insert(entity1);
            _unitofWork.Entity.Insert(entity1);
        }
    }
}


public class RatingRepo : GenericRepository<Rating>
{
    private IGenericSaveRepository gen;
    private readonly NaijaSchoolsContext _context;
    public RatingRepo(NaijaSchoolsContext context)
        : base(context)
    {
        _context = context;

    }

    public void Save(School school,Rating rating)
    {

        List<Rating> ratings = new List<Rating>();
        ratings.Add(rating);
        gen = new GenericSaveRepository<Rating>(_context);
        gen.Save(23, ratings);
    }
}

此行gen = new GenericSaveRepository<Rating>(_context);不允许我将评级指定为具体类型。

我该怎么做?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

Rating必须实现IGenericSaveRepository才能进行编译。

public class GenericSaveRepository<TEntity> 其中TEntity :class, IGenericSaveRepository

答案 1 :(得分:1)

这应该删除你的编译错误..如果你不想要评级必须实现IGenericSaveRepository,请参阅gen实现

  public class RatingRepo : GenericRepository<Rating>
        {
            private GenericSaveRepository<Rating> gen;
            private readonly NaijaSchoolsContext _context;
            public RatingRepo(NaijaSchoolsContext context)
                : base(context)
            {
                _context = context;

            }

            public void Save(School school, Rating rating)
            {

                List<Rating> ratings = new List<Rating>();
                ratings.Add(rating);
                gen = new GenericSaveRepository<Rating>(_context);
                gen.Save(23, ratings);
            }
        }

更新:完成实施

public interface IGenericSaveRepository
    {
        void Save<TEntity>(int id, ICollection<TEntity> entities) where TEntity : class;
    }


    public class GenericSaveRepository<TEntity> where TEntity : class
    {
        private UnitofWork<TEntity> _unitofWork;
        private NaijaSchoolsContext _context;
        public GenericSaveRepository(NaijaSchoolsContext context)
        {
            _context = context;
            _unitofWork = new UnitofWork<TEntity>(_context);
        }
        public void Save(int id, ICollection<TEntity> entities)
        {
            foreach (var entity1 in entities)
            {

            }
        }
    }

    public class UnitofWork<T>
    {
        public UnitofWork(NaijaSchoolsContext context)
        {
            throw new NotImplementedException();
        }
    }

    internal interface IUnitofWork<T>
    {
    }

    public class NaijaSchoolsContext
    {
    }

    public class GenericRepository<T>
    {
        protected GenericRepository(NaijaSchoolsContext context)
        {
            throw new NotImplementedException();
        }
    }

    public class Rating 
    {
    }

    public class School
    {
    }