ASP.NET MVC3 - 使用DependencyResolver和温莎城堡:为什么? [英] ASP.NET MVC3 - Use DependencyResolver AND Windsor Castle: Why?

查看:165
本文介绍了ASP.NET MVC3 - 使用DependencyResolver和温莎城堡:为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以照亮一盏小灯给我吗?

Can somebody shine a little light for me?

我有我的网站上所有正在运行使用温莎城堡。我有一个控制器工厂和安装控制器和服务。所有好的。

I've got my website all running using Windsor Castle. I have a controller factory and installers for controllers and services. All nice.

现在我刚刚创建了一个名为的IDependencyResolver 实现类 WindsorDependencyResolver 与straigh转发实现:

Now I've just created a IDependencyResolver implementing class called WindsorDependencyResolver with a straigh-forward implementation:

public class WindsorDependencyResolver : System.Web.Mvc.IDependencyResolver
{
    private readonly IKernel _kernel;

    public WindsorDependencyResolver (IKernel kernel)
    {
        _kernel = kernel;
    }

    public object GetService(Type serviceType)
    {
        return _kernel.Resolve(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _kernel.ResolveAll(serviceType) as IEnumerable<object>;
    }
}

我知道了设置像这样(Global.asax中):

I've got it setup like so (global.asax):

DependencyResolver.SetResolver(new WindsorDependencyResolver(kernel));

而现在呢?当这种拿来主义?我应停止使用 kernel.Resolve(SOMETYPE)

And now what? When is this 'used'? Should I stop using kernel.Resolve(someType)?

推荐答案

我的理解是的IDependencyResolver是什么是内部使用MVC 3做控制的服务定位/反转。因此,为了您的控制器,以得到正确初始化,并且能够注入你拥有的任何依赖关系,你需要告诉MVC如何跟你的使用(你的情况温莎)的容器。

My understanding is the IDependencyResolver is what is used internally by MVC 3 to do Service Location/Inversion of Control. So in order for your Controllers to get instantiated properly, and be able to inject whatever dependencies you have, you need to tell MVC how to talk to the container your using (Windsor in your case).

您仍然希望当你需要得到的东西是不是通过构造函数/属性注射注射你容器的使用kernel.Resolve(SOMETYPE)。

You would still want to use kernel.Resolve(someType) when you need to get something out of the container that is not injected for you via constructor/property injection.

有趣的是,MSDN文档指向布拉德·威尔逊的博客帖子上的IDependencyResolver了解详情。

Interestingly, the MSDN documentation points to Brad Wilson's Blog Post on the IDependencyResolver for details.

这篇关于ASP.NET MVC3 - 使用DependencyResolver和温莎城堡:为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆