Reputation: 4170
EDIT: After @mattytommo's help in isolating the root-cause of the error, I discovered that the IStatementRepository.cs file was not included in the project. Including it in the project resolved this case.
I am trying to implement a repository on my controller ( with some Dependency Injection thrown in), but I'm bumping into a wall. I have an IStatementRepository defined, But, when I try to create a constructor with an IStatementRepository parameter for DI purposes, I get the following errors:
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?)
Here's the block of code where the errors are generated:
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
}
}
And here is the entire contents of 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);
}
}
I can't see why I can't utilize an interface here. All the examples I'm following seem to be using this general patter, so I expect I'm just missing something simple.
I will be very grateful for your input.
Upvotes: 4
Views: 3135
Reputation: 46018
Are you missing a using directive or an assembly reference?
If your interface is in a different assembly, is it marked as public
?
Is the build action of your interface file set to Compile?
Upvotes: 0
Reputation: 56459
Your constructor is a little off, you're trying to do this.IStatementRepository
, yet the variable is this._repo
. Those errors are because Visual Studio is telling you there isn't a variable called IStatementRepository
inside this
(your controller) :).
Try 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
}
}
Upvotes: 6