asp.net - 如何将 CaSTLe Windsor 与 ASP.Net Web 表单一起使用?

标签 asp.net inversion-of-control castle-windsor

我正在尝试将 Windsor 的依赖注入(inject)连接到标准的 asp.net 网络表单。我想我已经使用 HttpModule 和 CustomAttribute(代码如下所示)实现了这一点,尽管该解决方案似乎有点笨拙,并且想知道 Windsor 是否有更好的现成支持解决方案?

这里有几个文件一起显示

    // index.aspx.cs
    public partial class IndexPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Logger.Write("page loading");
        }

        [Inject]
        public ILogger Logger { get; set; }
    }

    // WindsorHttpModule.cs
    public class WindsorHttpModule : IHttpModule
    {
        private HttpApplication _application;
        private IoCProvider _iocProvider;

        public void Init(HttpApplication context)
        {
            _application = context;
            _iocProvider = context as IoCProvider;

            if(_iocProvider == null)
            {
                throw new InvalidOperationException("Application must implement IoCProvider");
            }

            _application.PreRequestHandlerExecute += InitiateWindsor;
        }

        private void InitiateWindsor(object sender, System.EventArgs e)
        {
            Page currentPage = _application.Context.CurrentHandler as Page;
            if(currentPage != null)
            {
                InjectPropertiesOn(currentPage);
                currentPage.InitComplete += delegate { InjectUserControls(currentPage); };
            }
        }

        private void InjectUserControls(Control parent)
        {
            if(parent.Controls != null)
            {
                foreach (Control control in parent.Controls)
                {
                    if(control is UserControl)
                    {
                        InjectPropertiesOn(control);
                    }
                    InjectUserControls(control);
                }
            }
        }

        private void InjectPropertiesOn(object currentPage)
        {
            PropertyInfo[] properties = currentPage.GetType().GetProperties();
            foreach(PropertyInfo property in properties)
            {
                object[] attributes = property.GetCustomAttributes(typeof (InjectAttribute), false);
                if(attributes != null && attributes.Length > 0)
                {
                    object valueToInject = _iocProvider.Container.Resolve(property.PropertyType);
                    property.SetValue(currentPage, valueToInject, null);
                }
            }
        }
    }

    // Global.asax.cs
    public class Global : System.Web.HttpApplication, IoCProvider
    {
        private IWindsorContainer _container;

        public override void Init()
        {
            base.Init();

            InitializeIoC();
        }

        private void InitializeIoC()
        {
            _container = new WindsorContainer();
            _container.AddComponent<ILogger, Logger>();
        }

        public IWindsorContainer Container
        {
            get { return _container; }
        }
    }

    public interface IoCProvider
    {
        IWindsorContainer Container { get; }
    }

最佳答案

我认为你基本上走在正确的轨道上 - 如果你还没有,我建议你看看 Rhino Igloo,一个 WebForms MVC 框架,Here's a good blog post on this来源是here - Ayende(Rhino Igloo 的作者)在这个项目/库中很好地解决了将 Windsor 与 Web 表单一起使用的问题。

如果您要注入(inject)整个嵌套的控件集,我会缓存反射信息,我怀疑这最终可能会导致性能下降。

最后,spring.net 以一种更加面向配置的方式来解决这个问题,但可能值得看看它们的实现——这里有一个很好的 reference blog post对此。

关于asp.net - 如何将 CaSTLe Windsor 与 ASP.Net Web 表单一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/293790/

相关文章:

c# - 如何使用 Linq-to-entities 检索分层数据?

c# - 使用 Windows 服务手动实现 IoC

.net - 我应该在 dotnet 中使用哪种依赖注入(inject)技术?

c# - 无法向 CaSTLe Windsor 注册 MvcMailer

c# - 使用 CaSTLe Fluent Interface 注册拦截器

c# - 从 session Cookie 实例化 ASP.NET session

c# - dotnet 工具 aspnet-codegenerator 在错误的路径中查找可执行文件

mvvm - MvvmLight 的标准 ViewModelLocator 是反模式吗?以及如何减轻这种情况?

generics - 如何让CasSTLe Windsor解析带有约束的泛型?

PHP curl - 发布 asp.net viewstate 值