Asp.net mvc 在ASP.NET MVC中使用BaseController中的Ninject

Asp.net mvc 在ASP.NET MVC中使用BaseController中的Ninject,asp.net-mvc,dependency-injection,ninject,ninject.web.mvc,Asp.net Mvc,Dependency Injection,Ninject,Ninject.web.mvc,我正在开发一个Asp.NETMVC项目。在我的项目中,我的所有控制器都从BaseController继承。我在棒球场做最普通的事情。我正在使用Ninject进行依赖注入。但是我在向BaseController注入依赖项方面遇到了问题 这是我的基站控制器 public class BaseController : Controller { protected ICurrencyRepo currencyRepo; public Currency Curren

我正在开发一个Asp.NETMVC项目。在我的项目中,我的所有控制器都从BaseController继承。我在棒球场做最普通的事情。我正在使用Ninject进行依赖注入。但是我在向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;
        }
    }
public BaseController()
        {
            this.currencyRepo = new CurrencyRepo();
        }
如您所见,我必须在构造函数中启动CurrencyRepo实例,而不使用Ninject

我想要的是这样

 public BaseController(ICurrencyRepo repoParam)
            {
                this.currencyRepo = repoParam;
            }
但如果我这样做并运行我的项目,它会给我以下错误


那么,如何在BaseController中使用ninject注入依赖项呢?

您必须更改派生类型(
WishListController
CartController
等)构造函数,以将所需参数(
ICurrencyRepo
)传递给基本控制器构造函数

比如:

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

请参见为什么在BaseController构造函数中进行依赖项注入。 正确的方法是在要使用它的控制器的构造函数中执行

从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;
        }
    }
public BaseController()
        {
            this.currencyRepo = new CurrencyRepo();
        }
更改此
受保护的ICurrencyRepo currencyRepo
受保护的ICurrencyRepo CurrencyRepo

并在从BaseController继承的控制器上添加以下内容:

public WishListController(ICurrencyRepo currencyRepo)
        {
            CurrencyRepo = currencyRepo;
        }

因此,没有办法注入BaseController,因为我必须在每个控制器中注入currentRepo,即使它们不使用它。使用我回答中的代码,Ninject将为您注入
ICurrencyRepo
,并且
WishListController
(或每个派生类型)将使用
基(currencyRepo)传递该参数
部分,您可以继续。@WaiYanHein,如果您使用构造函数注入,恐怕“我必须在每个控制器中注入
currentRepo
,即使它们不使用它”的答案是肯定的。但是,你必须意识到(从面向对象的角度来看),由于
WishListController是BaseController
,所以他们实际上正在使用它。非常感谢你的帮助和分享。我用这种方法。可能是重复的