4

編集:@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);

    }
}

ここでインターフェースを利用できない理由がわかりません。私がフォローしているすべての例は、この一般的なパターンを使用しているように見えるので、単純なものが欠けているだけだと思います。

ご意見をお待ちしております。

4

3 に答える 3

6

あなたのコンストラクターは少しずれています、あなたはやろうとしていますthis.IStatementRepository、それでも変数はthis._repoです。IStatementRepositoryこれらのエラーは、Visual Studioが内部(コントローラー)と呼ばれる変数がないと言っているためですthis:)。

これを試して:

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._repo = repository;
        }

        // additional controller actions here
    }
}
于 2012-05-29T19:22:49.083 に答える
0

usingディレクティブまたはアセンブリ参照がありませんか?

インターフェイスが別のアセンブリにある場合、それはマークされていpublicますか?

インターフェイスファイルのビルドアクションがコンパイルに設定されていますか?

于 2012-05-29T19:08:17.910 に答える
-1

これは私のために働いた:

using System.Net.NetworkInformation; 
于 2015-06-02T12:03:22.897 に答える