1

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.

enter image description here

So how can I inject dependency using ninject in BaseController?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • Possible duplicate of [Questions about using Ninject](http://stackoverflow.com/questions/36221865/questions-about-using-ninject) – NightOwl888 Jul 07 '16 at 08:36

2 Answers2

2

You have to change your derived types (WishListController, CartController etc...) constructor to pass the required parameter (ICurrencyRepo) to the base controller constructor.

Something like:

public class WishListController : BaseController
{
    public WishListController(ICurrencyRepo currencyRepo) : base(currencyRepo)
    {
    }
}

See MSDN

haim770
  • 48,394
  • 7
  • 105
  • 133
  • So there is no way to inject in BaseController cause I have to inject currentRepo in every controller even they does not use it.? – Wai Yan Hein Jul 07 '16 at 06:57
  • Using the code in my answer, Ninject will inject `ICurrencyRepo` for you and `WishListController` (or each derive type) will pass that argument using the `base(currencyRepo)` portion and you're good to go. – haim770 Jul 07 '16 at 06:59
  • 1
    @WaiYanHein, If you're going with constructor injection, I'm afraid the answer to "I have to inject `currentRepo` in every controller even they does not use it" is yes. But, you have to realize (from OOP perspective) that since `WishListController is BaseController` they actually *are* using it. – haim770 Jul 07 '16 at 07:03
  • Thanks so much for helping and sharing me your great knowledge. I used this way. – Wai Yan Hein Jul 07 '16 at 07:09
0

Why you do your Dependency Injection In your BaseController Constructor. The proper way is to do it in the Constructor of the Controller where you want to use it.

Delete this from BaseController

public BaseController()
        {
            this.currencyRepo = new CurrencyRepo();
        }

change this protected ICurrencyRepo currencyRepo; to this protected ICurrencyRepo CurrencyRepo;

And add at a Controller who inherits from the BaseController this:

public WishListController(ICurrencyRepo currencyRepo)
        {
            CurrencyRepo = currencyRepo;
        }
Give IT
  • 200
  • 1
  • 3
  • 19