View Full Version : UserControl dependecy injection
Ted Husted
07-22-2005, 12:31 AM
Any good way to use dependency injection with UserControls?
I tried pretending they were pages, but, no joy, it didn't seem to work :cry:
-Ted.
pancham_singh
07-22-2005, 05:17 PM
I have a similar issue and here is the approach that I'm taking:
Since UserControls are created by asp.NET framework, i'm using IObjectFactory.configureObject method to inject the dpendencies. In the parent page's page load method, I find the control in which I need to inject the dependency and then call this method. Here is how my code snippet (Page_Load) looks like:
protected void Page_Load(object sender, EventArgs e)
{
IApplicationContext context = ConfigurationSettings.GetConfig("spring/context")
as IApplicationContext;
IList controls = GetUserControls();
for (int i = controls.Count -1; i >= 0 ; i--)
{
String controlName = controls[i] as String;
Control control = FindControl(controlName);
context.ConfigureObject(control, controlName);
}
}
private IList GetUserControls()
{
// Return ids of all user controls on this page
}
Let me know if you come up with a better way.
Aleks Seovic
08-13-2005, 04:17 PM
Actually, there is a better way built into the framework.
I just added documentation for the controls DI to http://opensource.atlassian.com/confluence/spring/display/NET/Dependency+Injection+for+ASP.Net+pages
Regards,
Aleks
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.