使用实现通用接口的存储库在aspnet核心控制器中转换问题

时间:2017-05-17 14:42:20

标签: c# asp.net-core asp.net-core-mvc

实际问题在于:

[Produces("application/json")]
[Route("api/[controller]")]
public class OrgController : Controller
{
    private readonly IGenericRepository<OrgRepository> _repository;
    public OrgController(IGenericRepository<OrgRepository> repo)
   {
        _repository = repo;
    }
    [HttpGet]
    public async Task<IEnumerable<Org>> Get()
    {
        return await _repository.GetAllRecords();
    }
}
}

GetAllRecords()行告诉我:

Error CS0029: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<P5NCore.Repos itories.OrgRepository>>' to 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<P5NCore.Models.Org>>'(CS0029) (P5NCore)

GetallRecords()是实际返回正确类型的存储库代码:

public class OrgRepository : IGenericRepository<Org>
    {
     private readonly OrgContext _context = null;
     public OrgRepository(IOptions<Settings> settings)
     {
         _context = new OrgContext(settings);
     }
     public async Task<IEnumerable<Org>> GetAllRecords(){
         try{
             return await _context.Orgs.Find(_=> true).ToListAsync();
         }
         catch (Exception ex){
             throw ex;
         }
     }
...
}

Repo正在实施通用接口:

public interface IGenericRepository<T> where T:class
    {
        Task<IEnumerable<T>> GetAllRecords();
        Task<T> GetRecord(string id);
        Task AddRecord(T item);
    }

我注册了这样的事情:

services.AddTransient<IGenericRepository<Org>, OrgRepository>();

我忽略了什么吗?

0 个答案:

没有答案