简体   繁体   English

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

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

I'm using a generic repository. 我正在使用通用存储库。 My service layer talks to my repository and maps entities to domain models with automapper. 我的服务层与我的存储库进行通信,并使用automapper将实体映射到域模型。 My controllers talk to my service layer and know nothing of entities or the repository. 我的控制器与我的服务层对话,却不了解实体或存储库。

I am trying to create a generic service class for all the basic CRUDs. 我正在尝试为所有基本CRUD创建一个通用服务类。

My generic service looks like this (cut down): 我的通用服务看起来像这样(减少):

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)); }
}

My Student service: 我的学生服务:

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;
    }
}

And my controller 而我的控制器

public class StudentController
{
    private readonly IStudentService _studentService;

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

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

I get the following when calling add from my controller (line marked with ERROR above). 从我的控制器调用add时,我得到以下内容(上面标有ERROR的行)。

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

I understand the reason for the error but didn't think it would be a problem as my service accepts and returns models only and doesn't need to know about entities? 我理解错误的原因,但不认为这会是一个问题,因为我的服务接受并仅返回模型而不需要了解实体?

Is there another way to accomplish what I want so I can keep from referencing entities in my controller class? 有没有其他方法可以实现我想要的东西,所以我可以避免在我的控制器类中引用实体?

For completeness, I should probably make this as an answer. 为了完整起见,我应该把它作为答案。

Just change the Service interface not to take the Entity Type Parameter: 只需更改服务界面即可获取实体类型参数:

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

and keep the type parameter on the abstract class. 并在抽象类上保留type参数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在未引用的程序集中定义类型“ x”。 VS 2010 C# - The type “x” is defined in an assembly that is not referenced. VS 2010 C# 类型“ IReportServerCredentials”在未引用的程序集中定义。 - type 'IReportServerCredentials' is defined in an assembly that is not referenced. 类型&#39;IAsyncOperationWithProgress &lt;,&gt;&#39;在未引用的程序集中定义。 - The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced. “IEnumerable &lt;&gt;”类型在未引用的程序集中定义。 System.Runtime - The type 'IEnumerable<>' is defined in an assembly that is not referenced. System.Runtime 类型“ DbConnection”在未引用的程序集中定义。 我无法添加参考? - The type 'DbConnection' is defined in an assembly that is not referenced. I cannot add the reference? 在未引用的程序集中定义的 C# 类型 - C# type defined in an assembly that is not referenced c# 类型在未引用的程序集中定义 - c# The type is defined in an assembly that is not referenced “MarshalByRefObject”类型是在未引用的程序集中定义的。 您必须添加对程序集 &#39;mscorlib, Version=4.0.0.0 的引用 - The type 'MarshalByRefObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0 类型“ System.Data.Common.DbTransaction”在未引用的程序集中定义。 您必须添加对程序集的引用 - The type 'System.Data.Common.DbTransaction' is defined in an assembly that is not referenced. You must add a reference to assembly 为什么我得到:“IThirdParty”类型是在未引用的程序集中定义的。 您必须添加对程序集“ThirdPartyAssembly”的引用吗? - Why am I getting: The type 'IThirdParty' is defined in an assembly that is not referenced. You must add a reference to assembly 'ThirdPartyAssembly'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM