10

私の質問は次のとおりです。ApplicationControllerというベースコントローラー(ASP.Net MVCコントローラー)があり、すべてのコントローラーがそれを継承するようにします。このベースコントローラーには、[Dependency]属性でマークされたILoggerプロパティがあります。(はい、コンストラクターインジェクションを使用する必要があることはわかっています。この属性について知りたいだけです)。

コンテナを作成し、タイプを登録し、デフォルトのファクトリを変更しました。すべて問題ありません。問題は、派生コントローラーでLoggerプロパティを使用しようとしても、解決されないことです。

私は何が間違っているのですか?派生コントローラーを作成するときに、コンテナーが基本クラスの依存関係を解決しないのはなぜですか?

コードサンプル:


ApplicationController:

public class ApplicationController : Controller
{
    [Dependency]
    protected ILogger _logger { get; set; }

}

派生コントローラー:

public class HomeController : ApplicationController
{
    public HomeController()
    {

    }
    public ActionResult Index()
    {
        _logger.Log("Home controller constructor started.");
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

Unityコントローラーファクトリー:

public class UnityControllerFactory : DefaultControllerFactory
{
    private readonly IUnityContainer _container;
    public UnityControllerFactory(IUnityContainer container)
    {
        _container = container;
    }

    protected override IController GetControllerInstance(Type controllerType)
    {
        return _container.Resolve(controllerType) as IController;
    }
}

Global.asax.csサンプル:

protected void Application_Start()
    {
        _container = new UnityContainer();
        _container.RegisterType<ILogger, Logger.Logger>();
        UnityControllerFactory factory = new UnityControllerFactory(_container);
        ControllerBuilder.Current.SetControllerFactory(factory);

        RegisterRoutes(RouteTable.Routes);
    }

私はUnityを初めて使用するので、何か間違ったことをしたのかもしれません。

ありがとう、亜美。

4

4 に答える 4

21

私の知る限り、Unity はパブリック プロパティのみを解決します。したがって、保護されたプロパティは解決されません。

于 2009-06-17T14:48:41.873 に答える
0

I'm not sure if this is related, but usually, I avoid having namespaces and classes with the same name (in your case, Logger.Logger), for I had problems with this in the past. But that may be not the problem.

I'm also not sure if the [Dependency] attribute works for derived types. If you change it for constructor injection, does this still not work? Something like:

public class ApplicationController : Controller
{
    protected ILogger _logger { get; set; }


    public ApplicationController(ILogger logger)
    {
         this._logger = logger;

    }
}

and

public class HomeController : ApplicationController
{
    public HomeController(ILogger logger) : base(logger)
    {

    }
    public ActionResult Index()
    {
        _logger.Log("Home controller constructor started.");
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

and the rest just the same. The code looks ok.

于 2009-06-17T14:37:46.870 に答える
0

I'm fairly unexperienced with unity as well, but I think you need to register your HomeController with the contsaner, not the logger.

于 2009-06-17T14:39:23.530 に答える
0

同じ問題があり、ILogger を public に変更して修正しました。これは、VS2010、.NET 4 の ASP.NET MVC2 プロジェクトを使用したものです。Unity はプロキシ クラスなどを作成していないため、論理的には理にかなっています。アクセスできるプロパティを設定しているだけで、マッピングがあります-したがって、公開のみです。

于 2009-11-21T06:42:18.330 に答える