【问题标题】:Using Ninject in BaseController in ASP.NET MVC在 ASP.NET MVC 的 BaseController 中使用 Ninject
【发布时间】:2016-07-07 06:27:47
【问题描述】:

我正在开发一个 Asp.Net Mvc 项目。在我的项目中,我所有的控制器都继承自 BaseController。我在 BaseCotroller 中做最常见的事情。我正在使用 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;
        }
    }

如您所见,我必须在不使用 Ninject 的情况下在构造函数中启动 CurrencyRepo 的实例。

我想要的构造函数是这样的

 public BaseController(ICurrencyRepo repoParam)
            {
                this.currencyRepo = repoParam;
            }

但是如果我这样做并运行我的项目,它会给我如下错误。

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

【问题讨论】:

标签: asp.net-mvc dependency-injection ninject ninject.web.mvc


【解决方案1】:

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

类似:

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

见MSDN

【讨论】:

  • 所以没有办法注入 BaseController 因为我必须在每个控制器中注入 currentRepo,即使他们不使用它。?
  • 使用我的答案中的代码,Ninject 将为您注入 ICurrencyRepo,WishListController(或每个派生类型)将使用 base(currencyRepo) 部分传递该参数,您就可以开始了.
  • @WaiYanHein,如果您要使用构造函数注入,恐怕“我必须在每个控制器中注入 currentRepo,即使他们不使用它”的答案是肯定的。但是,你必须意识到(从 OOP 的角度来看)自从WishListController is BaseController 他们实际上正在使用它。
  • 非常感谢您的帮助和分享您的知识。我就是这样用的。
【解决方案2】:

为什么要在 BaseController 构造函数中进行依赖注入。 正确的方法是在要使用的Controller的构造函数中进行。

从 BaseController 中删除它

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

把这个protected ICurrencyRepo currencyRepo;改成这个protected ICurrencyRepo CurrencyRepo;

并在继承自 BaseController 的 Controller 处添加:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    相关资源
    最近更新 更多