该类型在未引用的程序集中定义。 c#、通用存储库模式、URF

The type is defined in an assembly that is not referenced. c#, Generic Repository Pattern, URF

我使用的是通用存储库。我的服务层与我的存储库对话,并使用自动映射器将实体映射到领域模型。我的控制器与我的服务层对话,对实体或存储库一无所知。

我正在尝试为所有基本 CRUD 创建通用服务 class。

我的通用服务如下所示(缩减):

public interface IService<TModel, TEntity>
{
    void Add(TModel model)
}

public abstract class Service<TModel, TEntity> : IService<TModel, TEntity>
{
    private readonly IGenericRepository<TEntity> _repository;

    protected Service(IGenericRepository<TEntity> repository) { _repository = repository; }

    public virtual void Add(TModel model) { _repository.Add(AutoMapper.Mapper.Map<TEntity>(model)); }
}

我的学生服务:

public interface IStudentService : IService<Model.Student, Entity.Student>
{ }

public class StudentService : Service<Model.Student, Entity.Student>, IStudentService 
{
    private readonly IGenericRepository<Entity.Student> _repository;

    public StudentService (IGenericRepository<Entity.Student> repository) : base(repository)
    {
        _repository = repository;
    }
}

还有我的控制器

public class StudentController
{
    private readonly IStudentService _studentService;

    public StudentController(IStudentService studentService)
    {
        _studentService = studentService;
    }

    public ActionResult AddStudent(Student model)
    {
        _studentService.Add(model); //ERROR
    }
}

从我的控制器调用添加时,我得到以下信息(上面标有 ERROR 的行)。

The type is defined in an assembly that is not referenced. You must add a reference to MyProject.Entities

我理解错误的原因,但认为这不会成为问题,因为我的服务仅接受 returns 模型并且不需要了解实体?

是否有另一种方法可以完成我想要的,这样我就可以避免在我的控制器中引用实体 class?

为了完整起见,我可能应该将其作为答案。

只需更改服务接口不带实体类型参数:

public interface IService<TModel> {
    // ...
}

并在抽象中保留类型参数 class。

public abstract class Service<TModel, TEntity> : IService<TModel> {
    // ...
}