Using webforms? Having a terrible time? Really want DI? This is for you.
DI containers are excellent productivity tools, but for those of us trapped maintaining (hopefully end of life) "Classic ASPX WebForms" sites, using a DI container look like a horrible message of static service location.
RetrIoc is a HTTP module shim that you can wrap around "the IOC container of your choice" which runs on every request and performs DI on your WebForms pages.
This package is deliberatley feature lite - all lifecycle and DI concerns are the responsibility of your container - this simply provides a HTTP Module to glue everything together.
Using the Inject
attribute you can mark:
- Public and Private Properties
- Public and Private Fields
As "Injected" on ASPX pages and controls. Per-request, your container will be invoked to populate the values of these properties, providing you a single hook for using Constructor injection across all the properties that you inject.
You'll need to do four things:
- Add the RetrIoc Http module to your web.config
- Implement our
IResolveTypes
interface - Register your implementation
- Markup your WebForms pages with attributes
Given the following WebForms page:
public class TestPage : Page
{
public Dep SomeDependency { get; set; }
private Dep2 _someOtherDependency;
}
- Add a nuget package reference to RetrIoc.
- Open your web.config
- Add to your http modules
This is really down to you, but you need to wrap the configured instance of your container. Consider this example that uses Activator:
public class ActivatorContainer : IResolveTypes
{
public object Resolve(Type type)
{
return Activator.CreateInstance(type);
}
public object ResolveAll(Type type)
{
return Activator.CreateInstance(type);
}
public void Release(object instance)
{
//noop;
}
}
Provide an implementation for the fully configured container of your choosing.
Open up your global.asax
file and add the following block of code:
protected void Application_Start(object sender, EventArgs e)
{
Autowire.ConfigureWith(new YourTypeResolverHere());
}
Obviously, don't replace your existing application start if you have one.
To enable DI simply add the Inject
attribute to your code - using our previous example:
public class TestPage : Page
{
[Inject]
public Dep SomeDependency { get; set; }
[Inject]
private Dep2 _someOtherDependency;
}