[英]C#, MVC, IoC, Dependency Injection
这是我的BaseController
:
using Ferrero.Data;
using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Web.Mvc;
namespace Ferrero.Web.Controllers
{
public abstract class BaseController : Controller
{
protected IFerreroUow Uow { get; set; }
public MasterLayoutView masterlayout = new MasterLayoutView();
}
}
这是我的HomeController
,它继承自BaseController
:
using Ferrero.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ferrero.Model;
namespace Ferrero.Web.Controllers
{
public class HomeController : BaseController
{
public HomeController(IFerreroUow uow)
{
Uow = uow;
}
public ActionResult Index()
{
ViewBag.Title = "Home Page";
masterlayout.employees_list = Uow.Employees.GetAll().ToList();
return View();
}
}
}
Uow是一个工作单元,我拥有所有存储库,并且希望每个控制器都可以使用它。 问题是我没有正确使用Ioc,运行应用程序时出现此错误
尝试创建类型为“ Ferrero.Web.Controllers.HomeController”的控制器时发生错误。 确保控制器具有无参数的公共构造函数。
谁能帮我吗?
有时您根本不需要容器:)您只需要知道“合成根”。 在不使用容器AKA Pure DI的情况下如何在WebApi和MVC中执行此操作的示例:
public interface ISingleton : IDisposable { }
public class TransientDependency { }
public class Singleton : ISingleton
{
public void Dispose() { }
}
// IHttpControllerActivator is for WebApi, IControllerFactory for MVC
public class CompositionRoot : IDisposable, IHttpControllerActivator, IControllerFactory
{
private readonly ISingleton _singleton;
// pass in any true singletons i.e. cross application instance singletons
public CompositionRoot()
{
// intitialise any application instance singletons
_singleton = new Singleton();
}
public void Dispose()
{
_singleton.Dispose();
}
// Web API
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
// Per-Request-scoped services are declared and initialized here
if (controllerType == typeof(SomeApiController))
{
// Transient services are created and directly injected here
return new SomeApiController(_singleton, new TransientDependency());
}
var argumentException = new ArgumentException(@"Unexpected controller type! " + controllerType.Name,
nameof(controllerType));
Log.Error(argumentException, "don't know how to instantiate API controller: {controllerType}", controllerType.Name);
throw argumentException;
}
// MVC
public IController CreateController(RequestContext requestContext, string controllerName)
{
if (controllerName.ToLower() == "home")
{
return new HomeController(_singleton, new TransientDependency());
}
var argumentException = new ArgumentException(@"Unexpected controller! " + controllerName);
Log.Error("don't know how to instantiate MVC controller: {controllerType}. redirecting to help", controllerName);
throw argumentException; // Alternatively would return some default Page Not Found placeholder controller;
}
// MVC
public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
{
return SessionStateBehavior.Default;
}
// MVC
public void ReleaseController(IController controller)
{
// anything to clean up?
}
}
public static class DependencyInjection
{
public static void WireUp()
{
var compositionRoot = new CompositionRoot();
System.Web.Mvc.ControllerBuilder.Current.SetControllerFactory(compositionRoot);
System.Web.Http.GlobalConfiguration.Configuration.Services.Replace(typeof (IHttpControllerActivator), compositionRoot);
}
}
我在这里找到了解决方案! 感谢这个家伙! 任何想将Ninject实现为mvc的人都是最好的例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.