在接口中使用泛型方法

本文关键字:泛型方法 接口 | 更新日期: 2023-09-27 18:27:05

请帮助我更正此代码。

我在类中有一个编译器错误

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);不允许将Rating指定为具体类型。

我该怎么做?

谢谢你的帮助。

在接口中使用泛型方法

这应该会消除编译错误。。如果您不希望Rating必须实现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
    {
    }

Rating必须实现IGenericSaveRepository才能编译。

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