I have a class that implements an interface and legacy from other class.
I don't know why my code is bad.
The error I get is:
CS0738 'SizeTypeService' does not implement interface member 'IGenericService<ItemType>.GetAll()'.
'GenericService<SizeType>.GetAll()' cannot implement 'IGenericService<ItemType>.GetAll()' because it does not have the matching return type of 'Task<List<ItemType>>'.
I think that my interface and class are ok. I don't know why VS is throwing an error.
public interface IGenericService<TEntity> where TEntity : class
{
Task<List<TEntity>> GetAll();
Task<TEntity> GetById(int id);
Task<TEntity> Insert(TEntity entity);
Task<TEntity> Update(TEntity entity);
Task Delete(int id);
}
public class GenericService<TEntity> : IGenericService<TEntity> where TEntity : class
{
private IGenericRepository<TEntity> genericRepository;
public GenericService(IGenericRepository<TEntity> genericRepository)
{
this.genericRepository = genericRepository;
}
public async Task Delete(int id)
{
await genericRepository.Delete(id);
}
public async Task<List<TEntity>> GetAll()
{
return await genericRepository.GetAll();
}
public async Task<TEntity> GetById(int id)
{
return await genericRepository.GetById(id);
}
public async Task<TEntity> Insert(TEntity entity)
{
return await genericRepository.Insert(entity);
}
public async Task<TEntity> Update(TEntity entity)
{
return await genericRepository.Update(entity);
}
}
public interface ISizeTypeService : IGenericService<ItemType>
{
Task<List<SizeType>> GetAllSizeTypeActives(int id);
Task LogicDelete(int id);
}
public class SizeTypeService : GenericService<SizeType>, ISizeTypeService
{
private ISizeTypeRepository sizeTypeRepository;
public SizeTypeService(ISizeTypeRepository sizeTypeRepository) : base(sizeTypeRepository)
{
this.sizeTypeRepository = sizeTypeRepository;
}
public Task<List<SizeType>> GetAllSizeTypeActives(int id)
{
return sizeTypeRepository.GetAllSizeTypeActives(id);
}
public Task LogicDelete(int id)
{
return sizeTypeRepository.LogicDelete(id);
}
}
CodePudding user response:
You have defined your ServiceTypeClass as
public class SizeTypeService : GenericService<SizeType>, ISizeTypeService
{
...
}
So you are telling the compiler that ServiceTypeService uses the base class GenericService with TEntity defined as SizeType. BUT, the ISizeTypeService interface it also derives from is telling the compiler that this class also implements the IGenericService interface where TEntity is a ItemType class. But nowhere in the code is class that implements the method GenericService<ItemType>.GetAll().
So, the error is saying "hey - I found the method matching GenericService<SizeType>.GetAll() - where TEntity is SizeType, but I cannot find one that matches IGenericService<ItemType>.GetAll()"
To fix you need to change the ISizeTypeService definition to use the same class as GenericService for TEntity
e.g.
public interface ISizeTypeService : IGenericService<SizeType>
{
...
}
NOTE: The generic parameter is now SizeType which matches the GenericService<SizeType> base class that SizeTypeService derives from.
CodePudding user response:
Simply Ged explained clearly. As code in your ISizeTypeService
public interface ISizeTypeService: IGenericService<ItemType>
{
}
Because you get Task<List> IGenericRepository.GetAll() in GenericService. So
I also got a similar error, then I added the following code in SizeTypeService.cs and the error went away
Task<List<ItemType>> IGenericService<ItemType>.GetAll()
{
throw new NotImplementedException();
}

