編集:@mattytommoがエラーの原因を特定するのに役立った後、IStatementRepository.csファイルがプロジェクトに含まれていないことを発見しました。それをプロジェクトに含めることで、このケースは解決しました。
コントローラにリポジトリを実装しようとしていますが(依存性注入がスローされています)、壁にぶつかっています。IStatementRepositoryを定義しましたが、DIの目的でIStatementRepositoryパラメーターを使用してコンストラクターを作成しようとすると、次のエラーが発生します。
The type or namespace name 'IStatementRepository' does not
exist in the namespace 'StatementsApplication.Models' (are
you missing an assembly reference?)
The type or namespace name 'IStatementRepository' could
not be found (are you missing a using directive or an
assembly reference?)
'StatementsApplication.Controllers.StatementController'
does not contain a definition for 'IStatementRepository'
and no extension method 'IStatementRepository' accepting a
first argument of type
'StatementsApplication.Controllers.StatementController'
could be found (are you missing a using directive or an
assembly reference?)
エラーが生成されるコードのブロックは次のとおりです。
using StatementsApplication.Models;
namespace StatementsApplication.Controllers
{
public class StatementController : Controller
{
public StatementsApplication.Models.IStatementRepository _repo;
private DALEntities db = new DALEntities();
public StatementController(IStatementRepository repository)
{
this.IStatementRepository = repository;
}
// additional controller actions here
}
}
そして、これがIStatementRepository.csの全内容です。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StatementsApplication.DAL;
namespace StatementsApplication.Models
{
public interface IStatementRepository {
IEnumerable<Statement> findAll();
IEnumerable<Statement> findByMerchantID(int id);
Statement findByID(int id);
Statement createStatement(Statement stmt);
int saveChanges();
void deleteStatement(int id);
}
}
ここでインターフェースを利用できない理由がわかりません。私がフォローしているすべての例は、この一般的なパターンを使用しているように見えるので、単純なものが欠けているだけだと思います。
ご意見をお待ちしております。