私は汎用リポジトリを使用しています。私のサービス層は私のリポジトリと話し、エンティティをオートマトンを使ってドメインモデルにマッピングします。私のコントローラは私のサービス層と話し、エンティティやリポジトリは何も知らない。タイプは、参照されていないアセンブリで定義されています。 C#、汎用リポジトリパターン、URF
すべての基本的なCRUD用の汎用サービスクラスを作成しようとしています。追加呼び出すときに、私は次の取得
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
}
}
:
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)); }
}
私の学生サービス:
私の一般的なサービスは、この(削減)のように見えます私のコントローラから(上記のERRORのマークが付けられた行)。
The type is defined in an assembly that is not referenced. You must add a reference to MyProject.Entities
私は、エラーの理由を理解するが、私のサービスが受け入れるだけのモデルを返し、実体について知る必要がないので、それが問題になることとは思いませんでしたか?
コントローラクラスのエンティティを参照しないようにするために、別の方法がありますか?
本当にサービスインターフェイスにエンティティタイプパラメータが必要ですか?抽象クラスは、それは大丈夫ですが、あなたの例ではInterfaceでは全く使われていません。さらに、あなたのサービスのクライアントはエンティティについて知らないので、実際には彼らが使用する契約(すなわちインタフェース)にあることは意味がありません。 – moreON
プロジェクト参照を追加する必要がありますが、コントローラに余分な使い方を追加する必要はありません。 – Thomas
@moreONあなたは正しいですか?シンプルなので、私はそれを逃した方法を知りません! – garethb