Can I create IOC controls with .NET for hosting in web projects?

I currently have a UserControls library that I use in many of our web applications. I would like to update these controls so that I can use the IOC to help decouple the logic.

So, on the webpage itself, it would have something similar to the following:

<prefix:ControlName ID="myControl" runat="server" property="value1" />

      

And the control will have a constructor similar to:

public ControlName (IControlLogic logic)

      

Then ideally the IOC container will handle the injection. It can be done? If so, which libraries offer this? If you have any links that you know about this please discuss it, they will be very grateful.

Thank you in advance

0


source to share


3 answers


Unfortunately there is no answer to this question, many IOC containers contain some implementation of injection control properties, but there is no way to (actually) create a control without a parameterless constructor.



0


source


Unfortunatley ASP.NET doesn't seem to support a simple factory creation pattern for controls. However, 3.5 gives you very fine control over the actual code that the ASP.NET runtime generates for your .aspx file.

Just apply the [ControlBuilder (...)] attributes to all the controls you want to create in your container. Subclass ControlBuilder and override ProcessGeneratedCode to replace the constructor with a call to your container.



Here's a simple example:

public class ServiceProviderBuilder : ControlBuilder
{
    public override void ProcessGeneratedCode(System.CodeDom.CodeCompileUnit codeCompileUnit, System.CodeDom.CodeTypeDeclaration baseType, System.CodeDom.CodeTypeDeclaration derivedType, System.CodeDom.CodeMemberMethod buildMethod, System.CodeDom.CodeMemberMethod dataBindingMethod)
    {
        // search for the constructor
        foreach (CodeStatement s in buildMethod.Statements)
        {
            var assign = s as CodeAssignStatement;
            if (null != assign)
            {
                var constructor = assign.Right as CodeObjectCreateExpression;
                if (null != constructor)
                {
                    // replace with custom object creation logic
                    assign.Right = new CodeSnippetExpression("("+ ControlType.FullName + ")MyContainer.Resolve<" + ControlType.BaseType.FullName + ">()");
                    break;
                }
            }
        }
        base.ProcessGeneratedCode(codeCompileUnit, baseType, derivedType, buildMethod, dataBindingMethod);
    }
}    

[ControlBuilder(typeof(ServiceProviderBuilder))]
public partial class WebUserControl1 : System.Web.UI.UserControl
{
    public WebUserControl1()
    {

    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

      

+2


source


Yes, I can suggest you look at spring.net

http://www.springframework.net/doc-latest/reference/html/springair.html

I'm not sure if you want to keep the usercontrol idea you are proposing.

Personally, I use spring.net and nHibernate together (although I avoid the currently existing Hibernate patterns), which work fantastic with .net MVC beta versions.

Using spring.net to handle nHibernate reduces configuration entries a bit, and MVC doesn't use the xmls context you need (Springair example is a good example).

Good luck.

+1


source







All Articles