4

编辑:在@mattytommo 帮助隔离错误的根本原因后,我发现 IStatementRepository.cs 文件未包含在项目中。将它包含在项目中解决了这种情况。

我正在尝试在我的控制器上实现一个存储库(使用一些依赖注入),但我撞到了墙上。我定义了 IStatementRepository,但是,当我尝试使用 IStatementRepository 参数创建构造函数以用于 DI 目的时,出现以下错误:

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 回答