2014-10-20 51 views
-2

请帮助我更正此代码。在接口中使用通用方法

我在班上

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);不允许我有指定为具体型别等级有编译器错误。

我该怎么做?

感谢您的帮助。

+0

确实评级实施IGenericSaveRepository? – 2014-10-20 11:30:36

+0

GenericRepository 和GenericSaveRepository 之间是否有区别或者是否是拼写错误? 此外,评级的代码缺失,请proivde。 – mindfxxxedCoder 2014-10-20 11:34:09

+0

不,它不执行它@ Selman22 – Seun 2014-10-20 11:35:15

回答

1

这应该删除您的编译错误..见将军的实施,如果你不希望评级必须实现IGenericSaveRepository

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 
    { 
    } 
+0

您未在GenericSaveRepository类中实现IGenericSaveRepository。 Save方法需要具体实现在GenericSaveRepository类 – Seun 2014-10-20 11:43:42

+0

对不起然后你没有选择我亲爱的朋友,但实施IGenericSaveRepository评级类.. – ThomasBecker 2014-10-20 11:45:43

+0

好的非常感谢 – Seun 2014-10-20 11:53:03

1

Rating必须执行IGenericSaveRepository为了编译。

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

+0

我不希望Rating来实现IGenericSaveRepository。任何方式 – Seun 2014-10-20 11:34:16

+0

但你想这个:gen = new GenericSaveRepository (_context);编译.. – 2014-10-20 11:35:24

+0

我认为埃米尔答案的意图丢失: 其中TEntity:类,IGenericSaveRepository 是错误的,它应该只是TEntity:类 顺便说一下我尽量避免与只有“类”的定义做仿制药,怎么样也许定义一个IEntity类型,例如提供关键字段,至少这是我在回购中经常需要的。那么它也更加清楚,通用领域的真正含义是什么。 – mindfxxxedCoder 2014-10-20 11:43:54