I am developing an Asp.Net Mvc project. In my project, all my controllers inherit from a BaseController. I do most common stuffs in BaseCotroller. I am using Ninject for dependency injection. But I am having a problem with injecting dependency to BaseController.
This is my BaseController
public class BaseController : Controller
{
protected ICurrencyRepo currencyRepo;
public Currency Currency { get; set; }
public BaseController()
{
this.currencyRepo = new CurrencyRepo();
}
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
Currency cur = null;
base.Initialize(requestContext);
Url = new UrlHelperAdaptor(base.Url);
string currencyIdString = HttpContext.Application["currency"].ToString();
if(string.IsNullOrEmpty(currencyIdString))
{
cur = currencyRepo.Currencies.FirstOrDefault(x => x.Default);
}
else
{
int id = Convert.ToInt32(currencyIdString);
cur = currencyRepo.Currencies.FirstOrDefault(x => x.Id == id);
}
if (cur == null)
{
cur = currencyRepo.Currencies.FirstOrDefault();
}
if(cur!=null)
{
AppConfig.CurrentCurrencyUnit = cur.Unit;
AppConfig.CurrentCurrencyMmUnit = cur.MmUnit;
}
Currency = cur;
}
}
As you can see, I have to initiate the instance of CurrencyRepo in the constructor without using Ninject.
What I want constructor is to be like this
public BaseController(ICurrencyRepo repoParam)
{
this.currencyRepo = repoParam;
}
But if I do that way and run my project, it gives me error as follow.
So how can I inject dependency using ninject in BaseController?