Spring.NET &即时CMS

发布于 2024-08-16 22:17:44 字数 90 浏览 5 评论 0原文

有没有什么方法可以使用 Spring.NET 将依赖项注入到 Immediacy CMS 控件中,理想情况下在初始化控件时无需使用 ContextRegistry ?

Is there any way to inject dependencies into an Immediacy CMS control using Spring.NET, ideally without having to use to ContextRegistry when initialising the control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不咬妳我踢妳 2024-08-23 22:17:44

作为一名前 Immediacy 员工,我经常看到此类问题。

将即时性视为位于框架之上的标准 .net Web 应用程序。
他们只是扩展了 Page 类等的基类,并添加了一些全局有用的对象。

Immediacy 确实有自己的控件基类等,但没有什么可以阻止您基于标准 .net 类创建自己的控件。

我见过的一个技巧是,您需要执行一些可能会破坏即时功能的特定操作,即创建一个子应用程序,然后在添加到主应用程序的自定义控件中从中调用页面。

这样,您的子应用程序就可以做它喜欢做的事情,而不会破坏任何内容,并且父即时应用程序只需从该应用程序读取流即可。

最简单的技巧是简单地创建一个只做一件事的控件...

WebClient wc = new WebClient();
this.Controls.Add(Page.ParseControl( wc.DownloadString("/childapp/yourcustomthing.aspx") ));

最干净的其他方法是将所需的子组件实现为我所看到的 ashx 处理程序。

可能出现的唯一问题是,您失去了子应用程序中对象的即时框架,但您可以轻松地通过查询字符串传递一些关键信息,然后构建自己的“依赖项缓存”,其中包含您需要的内容。

我知道这不是一个理想的选择,但是当您需要做任何超出向页面或模板添加列表/类似插件的事情时,即时性确实会让生活变得困难。

As an ex Immediacy employee i used to see this sort of problem all the time.

Think of immediacy as being a standard .net web app that sits on top of the framework.
They have simply extended the base classes for things like the Page class and added some globally useful objects.

Immediacy does have its own base classes for controls ect but there's nothing stopping you creating your own controls based on the standard .net classes.

One trick I have seen where you need to do something specific that may break an immediacy function is to create a child application then call up pages from it within a custom control that you add to the main app.

This way your child app can do what it likes without breaking anything and the parent immediacy app and simply reads a stream from that app.

in its simplest form the trick is to simply create a control that does one thing ...

WebClient wc = new WebClient();
this.Controls.Add(Page.ParseControl( wc.DownloadString("/childapp/yourcustomthing.aspx") ));

The cleanest other way to do this is to implement the required child component as an ashx handler from what i've seen.

The only issue that may bring up is that you lose the immediacy framework of objects in your child app, but you could easily pass in some key information via querystrings then build your own "depency cache" that contains the stuff you need.

It's not an ideal I know but immediacy does have a habit of making life difficult when you need to do anything above and beyond dropping in lists / similar plugins to pages or templates.

无言温柔 2024-08-23 22:17:44

我将用我已经使用了一年多的 Immediacy 6.x 解决方案来回答这个问题。理论上,经过一些修改后,这应该可以与 Alterian CMC 7.x(Immediacy 的后继者)一起使用。

这里的问题是 Immediacy 已经在 web.config 中定义了一个处理所有 aspx 页面的处理程序。然而,我发现这可以用 web.config 中 Spring.NET 的 PageHandlerFactory 条目替换,就像大多数 Webforms 应用程序一样!

然而,由于开箱即用,预览器仍然存在问题,因此仍然需要进行一些调整。让 Spring.NET 与即时控件完美配合的过程是:

  1. 将常用条目添加到 Spring.NET 的 web.config 如文档中所述。在本例中,我在 Spring.config 中获得了对象定义。

  2. 创建以下抽象基类来处理所有依赖注入工作:

    SpringImmediacyControl.cs

    公共抽象类 SpringImmediacyControl :ImmediacyControl、ISupportsWebDependencyInjection
    {
        // 您可以对其他控件类执行相同的操作,例如 LiteralControl
    
        #region ISupportsWebDependencyInjection 成员
    
        公共 IApplicationContext 默认应用程序上下文
        {
            得到;
            放;
        }
    
        #endregion
    
        受保护的重写 void OnInit(EventArgs e)
        {
            // 页面预览所需,使用 Immediacy 的页面预览处理程序执行
    
            if (DefaultApplicationContext == null)
            {
                DefaultApplicationContext = ContextRegistry.GetContext() 作为 WebApplicationContext;
                DefaultApplicationContext.ConfigureObject(this, this.GetType().FullName);
            }
    
            基.OnInit(e);
        }
    
        受保护的覆盖无效AddedControl(控制控制,int索引)
        {
            this.EnableViewState = false;
    
            // 自己为子进程处理 DI - 默认调用 InjectDependencyRecursive
    
            if (DefaultApplicationContext != null)
                WebDependencyInjectionUtils.InjectDependencyRecursive(DefaultApplicationContext,控制);
    
            base.AddedControl(控制, 索引);
        }
    }
    

    这类似于服务器代码Spring.NET 文档的侧面控制。然而,额外的 OnInit &需要 null 检查代码是因为预览器在 Immediacy 自己的预览器 HTTP 处理程序下呈现控件。这意味着需要手动调用 Spring.NET 的上下文注册表来注入依赖项。

  3. 对于您希望与 Spring.NET 的控制反转容器一起使用的任何控件,请将相关条目添加到 Spring.config,例如:

    SampleControl.cs

    公共类SampleControl:SpringImmediacyControl,INAmingContainer
    {
        公共字符串文本
        {
            得到;
            放;
        }
    
        受保护的字符串 InjectedText
        {
            得到;
            放;
        }
    
        公共样本控件()
            : 根据()
        {
            文本=“你好世界”;
        }
    
        protected override void RenderContents(HtmlTextWriter 输出)
        {
            输出.Write(string.Format("{0} {1}", Text, InjectedText));
        }
    }
    

    Spring.config

    <对象 xmlns="http://www.springframework.net">
        <对象类型=“MyProject.SampleControl,MyAssembly”抽象=“true”>
            <属性名称=“InjectedText”值=“来自Spring.NET”/>
        
    
    
  4. 如果一切都正确完成,您将拥有写出“Hello world from Spring.NET!”的控件。在页面中使用时。

我使用过的所有代码都可以下载&

顺便说一句,如果您希望创建 ButtonPluginConfig 的 Spring.NET 兼容子类,则过程类似,只不过这些子类始终在下面执行Immediacy 自己的 HTTP Handler 用于控制配置,&因此始终需要调用 Spring.NET 的上下文注册表。我还在上面的要点中包含了一个从 ButtonPluginConfig 派生的抽象类。

I'm going to answer this one with a solution I've used for over a year with Immediacy 6.x. In theory, this should work with Alterian CMC 7.x (Immediacy's successor) with some modification.

The issue here is that Immediacy already has a handler defined in web.config that deals with all aspx pages. However, I've found this can be replaced with an entry for Spring.NET's PageHandlerFactory in web.config, as per most webforms apps!

However, some tweaking is still needed since out of the box, there are still issues with the previewer. The process for getting Spring.NET to play nicely with an Immediacy Control is:

  1. Add the usual entries to web.config for Spring.NET as outlined in the documentation. In this case I've got my object definitions in Spring.config.

  2. Create the following abstract base class that will deal with all of the Dependency Injection work:

    SpringImmediacyControl.cs

    public abstract class SpringImmediacyControl : ImmediacyControl, ISupportsWebDependencyInjection
    {
        // You can do the same thing for other control classes, like LiteralControl
    
        #region ISupportsWebDependencyInjection Members
    
        public IApplicationContext DefaultApplicationContext
        {
            get;
            set;
        }
    
        #endregion
    
        protected override void OnInit(EventArgs e)
        {
            // Required for page preview, which is executed using Immediacy's page preview handler
    
            if (DefaultApplicationContext == null)
            {
                DefaultApplicationContext = ContextRegistry.GetContext() as WebApplicationContext;
                DefaultApplicationContext.ConfigureObject(this, this.GetType().FullName);
            }
    
            base.OnInit(e);
        }
    
        protected override void AddedControl(Control control, int index)
        {
            this.EnableViewState = false;
    
            // Handle DI for children ourselves - defaults to a call to InjectDependenciesRecursive
    
            if (DefaultApplicationContext != null)
                WebDependencyInjectionUtils.InjectDependenciesRecursive(DefaultApplicationContext, control);
    
            base.AddedControl(control, index);
        }
    }
    

    This is similar to the code for server side controls over on the Spring.NET documentation. However, the extra OnInit & null checking code is needed is because the previewer renders the control under Immediacy's own previewer HTTP Handler. This means a manual call to Spring.NET's context registry is needed to inject dependencies.

  3. For any control with which you wish to use with Spring.NET's Inversion of Control container, add the relelvant entry to Spring.config, for example:

    SampleControl.cs

    public class SampleControl : SpringImmediacyControl, INamingContainer
    {
        public string Text
        {
            get;
            set;
        }
    
        protected string InjectedText
        {
            get;
            set;
        }
    
        public SampleControl()
            : base()
        {
            Text = "Hello world";
        }
    
        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(string.Format("{0} {1}", Text, InjectedText));
        }
    }
    

    Spring.config

    <objects xmlns="http://www.springframework.net">
        <object type="MyProject.SampleControl, MyAssembly" abstract="true">
            <property name="InjectedText" value="from Spring.NET" />
        </object>
    </objects>
    
  4. Provided everything's done correctly, you will have the control writing out "Hello world from Spring.NET!" when used in a page.

All of the code I've used can be downloaded & forked from here.

As an aside, the process is similar if you wish to create Spring.NET compatible subclasses of ButtonPluginConfig, except these subclasses are always executed under Immediacy's own HTTP Handler for control configuration, & so calling Spring.NET's context registry is always required. I've included an abstract class derived from ButtonPluginConfig in the gist above as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文