rei*_*den 4 .net c# asp.net-mvc asp.net-mvc-3
编辑:在@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?)
Run Code Online (Sandbox Code Playgroud)
这是生成错误的代码块:
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
}
}
Run Code Online (Sandbox Code Playgroud)
这是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);
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我不能在这里使用界面.我所关注的所有例子似乎都在使用这种一般模式,所以我希望我只是缺少一些简单的东西.
我将非常感谢您的意见.
你的构造函数有点偏,你试图做this.IStatementRepository,但变量是this._repo.这些错误是因为Visual Studio是告诉你,没有一个所谓的可变IStatementRepository内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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1848 次 |
| 最近记录: |