Search code examples
c#asp.net-coretype-conversionaspnetboilerplateconventions

Unable to inject IRepository<TEntity> due to "no implicit reference conversion"


Started off and trying to get familiar with the ASP.NET Boilerplate template for .NET Core.

Everything was going smoothly until I needed to inject an IRepository with a generic Entity into a class constructor.

In the DepartmentManager class, when I tried to create a private read-only field to inject into the constructor, I get the IntelliSense error that:

There is no implicit reference conversion from Department to Abp.Domain.Entities.IEntity The Type Department cannot be used as type parameter TEntity in the generic type or method 'IRepository, primary key'

I have tried to research to see if I could find similar problems online with some solutions, but to no avail.

Below is a simple code to illustrate the challenge.

// Class 
public class Department
{
    public long Id { get; set; }

    public string DepartmentName { get; set; }
}

// IDepartment Manager
public interface IDepartmentManager
{
    Task<Department> CreateDepartment(Department entity);
    Task Update(Department entity);
    Task Delete(long id);
    Task<IEnumerable<Department>> GetAll();
}

// Department Manager    
private readonly IRepository<Department, long> _departmentRepo;
public DepartmentManager(IRepository<Department, long> departmentRepo)
{
    _departmentRepo = departmentRepo
}

Any help is well appreciated on what I am not doing right.


Solution

  • Make Department implement Entity<long> or IEntity<long> to use its repository.

    public class Department : Entity<long>
    {
        public string DepartmentName { get; set; }
    }
    

    References