【问题标题】:Working with Generic Methods in Interfaces在接口中使用泛型方法
【发布时间】:2014-12-15 08:55:45
【问题描述】:

请帮我更正这段代码。

我在类中遇到编译器错误

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&lt;Rating&gt;(_context); 这一行不允许我将 Rating 指定为具体类型。

我该怎么做?

感谢您的帮助。

【问题讨论】:

  • Rating 是否实现了 IGenericSaveRepository ?
  • GenericRepository 和 GenericSaveRepository 之间有区别还是这是一个错字?还缺少 Rating 类的代码,请提供。
  • 不,它没有实现它@Selman22
  • 那你为什么希望这段代码可以工作?
  • 是的,它们之间存在差异@user3046593

标签: c# generics inheritance methods interface


【解决方案1】:

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

【讨论】:

  • 您没有从 GenericSaveRepository 类实现 IGenericSaveRepository。 Save 方法需要在 GenericSaveRepository 类中具体实现
  • 对不起,亲爱的朋友,你别无选择,只能在 Rating 类中实现 IGenericSaveRepository ..
  • 问题是 Rating 必须可转换为 IGenericSaveRepository 才能将其用作泛型类 GenericSaveRepository 中的参数 TEntity,因此要么在 rating 类中实现接口,要么直接将其删除。我希望这能解释会有所帮助:)
  • 感谢我发现我过度设计了.. 我会使用 KISS
【解决方案2】:

Rating 必须实现 IGenericSaveRepository 才能编译。

public class GenericSaveRepository&lt;TEntity&gt; where TEntity : 类,IGenericSaveRepository

【讨论】:

  • 我不希望 Rating 实现 IGenericSaveRepository。任何方式
  • 但你想要这个:gen = new GenericSaveRepository(_context);编译..
  • 我认为 Amirs 回答的意图丢失了:其中 TEntity : class,IGenericSaveRepository 是错误的,它应该只是 TEntity: class Btw 我尽量避免只使用“类”定义来做泛型,怎么样也许定义一个 IEntity 类型,例如提供 key 字段,至少这是我在 repos 中经常需要的。然后也更清楚了,泛型字段的真正含义是什么。
  • 是的,我愿意。那我该怎么办
  • 当你有这行时它不起作用: public class GenericSaveRepository where TEntity : class ???如果没有,请发布 lart 2 类的代码
猜你喜欢
  • 1970-01-01
  • 2021-04-17
  • 2011-03-14
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多