簡體   English   中英

C#,MVC,IoC,依賴注入

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM