简体   繁体   English

C#,MVC,IoC,依赖注入

[英]C#, MVC, IoC, Dependency Injection

Here is my BaseController : 这是我的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();
         } 
      }

And here is my HomeController which inherits from BaseController : 这是我的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();
        }
    }
}

The Uow is a unit of work where I have all my Repositories and I want it to be available in every controller. Uow是一个工作单元,我拥有所有存储库,并且希望每个控制器都可以使用它。 The thing is that I am not using the Ioc correctly and I get this error when I run the application 问题是我没有正确使用Ioc,运行应用程序时出现此错误

An error occurred when trying to create a controller of type 'Ferrero.Web.Controllers.HomeController'. 尝试创建类型为“ Ferrero.Web.Controllers.HomeController”的控制器时发生错误。 Make sure that the controller has a parameterless public constructor. 确保控制器具有无参数的公共构造函数。

Can anyone help me please? 谁能帮我吗?

Sometimes you do not need a container at all :) You just need to know the Composition Root. 有时您根本不需要容器:)您只需要知道“合成根”。 Example of how to do this in WebApi and MVC without using a container AKA Pure DI: 在不使用容器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);
    }
}

I found my solution here !!! 我在这里找到了解决方案! Thanks to this guy! 感谢这个家伙! Anyone who wants to implement Ninject to mvc this is the best example. 任何想将Ninject实现为mvc的人都是最好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM