Dependency Injection is a new feature coming to DNN in 9.4 that allows you to inject abstractions or other objects into your Web Forms Module. This has been a common practice in both ASP.NET and ASP.NET Core application development. Removing the tight coupling between your controller code and business layer of your module.
Heading into the weekend following Microsoft Build 2019 (5 days after .NET 5 was announced) I submited a noteworthy Pull Requesthttps://github.com/dnnsoftware/Dnn.Platform/pull/2774 to DNN that adds Dependency Injection into the platform, a feature accessible in any 3rd-party library or module. If you are not familliar with Dependency Injection and want to learn what this means for the platform you should read my first blog in the series DNN Dependency Injection: .NET Core.
Blog Series
- DNN Dependency Injection: .NET Core
- DNN Dependency Injection: MVC
- DNN Dependency Injection: SPA & WEB API
- DNN Dependency Injection: Razor3
- DNN Dependency Injection: WebForms (this blog)
- To Be Announced.. (yes, there is more)
Getting Started
Getting started with Dependency Injection in a DNN WebForms Module requires minimal changes from your existing code. We will cover a step-by-step guide on how to add Dependency Injection with sample code so you can immediately start taking advantage of the new features in your project.
Clone the Sample Code
I created a sample project on GitHub that let’s you start with a clean WebForms Module and follow along in the Dependency Injection Guide.
Go ahead and clone the repo or use your own.
1
git clone --branch webforms_clean https://github.com/SkyeHoefling/DNN.DependencyInjection.Samples
Open the Solution file: DNN.DependencyInjection.Samples.sln
Building and Packaging
The sample project simplifies the building and packaging. To create the installer build the solution in Release Mode.
You will find the installer file in
1
.\Module_Installers\Dnn.DependencyInjection.Samples.WebForms_00.00.01_Install.zip
Try to build your module and install it. You will get a screen that looks like this
Add NuGets
Add the Dependency Injection NuGet which currently lives in your local NuGet feed
NuGetFeed\DotNetNuke.DependencyInjection.9.4.0.156-alpha.nupkg
Once DNN 9.4 is released it will be available on the public NuGet Feed.
Create Service to Inject
Create an interface and implementation that will be injected into our View.ascx.cs
code behind. We will be building a simple MessageService
that implements IMessageService
.
IMessageService
1
2
3
4
public interface IMessageService
{
string GetMessage();
}
MessageService
1
2
3
4
5
6
7
public class MessageService : IMessageService
{
public string GetMessage()
{
return "Hell from Dependency Injection World!!";
}
}
Add Startup Code
Before we can use our new IMessageService
and implementation we need to register the service. Create a Startup.cs
in the root of the project. The Startup
class will inherit the IDnnStartup
which provides the configuration method where services can be registered into the Dependency Injection container.
Implement Interface
1
2
3
4
5
6
7
public class Startup : IDnnStartup
{
public void ConfigureServices(IServiceCollection services)
{
// add service registgrations
}
}
The interface contains a method named ConfigureServices
which includes a parameter of IServiceCollection
which is the object that allows the code to register services. The IServiceCollection
is the same object used in ASP.NET Core applications which will help your module be ready for a .NET Core version of DNN.
Register IMessageService
1
2
3
4
5
6
7
public class Startup : IDnnStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IMessageService, MessageService>();
}
}
There is nothing else you need to do with Startup
code, DNN will automatically invoke the code and register your services.
Use the Service Locator
The IMessageService
has been correctly registered with the DNN Dependency Injeciton Provider. The WebForms Module is ready to start using the DependencyProvider
to resolve the registrations, this is where the services are included in the constructor with minimal work from the module developer.
1
2
3
4
5
6
7
8
9
10
11
public partial class View : PortalModuleBase
{
protected IMessageService MessageService { get; }
public View()
{
MessageService = DependencyProvider.GetService<IMessageService>();
}
// omitted code
}
**Yes, all you need to do is add the abstractions to the constructor parameters. **
Use the MessageService
Update the ContentLiteral.Text
property to use the injected IMessageService
.
1
2
3
4
5
6
7
protected void Page_load(object sender, System.EventArgs e)
{
if (IsPostBack)
return;
ContentLiteral.Text = MessageService.GetMessage();
}
Complete Code Behind
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public partial class View : PortalModuleBase
{
protected IMessageService MessageService { get; }
public View()
{
MessageService = DependencyProvider.GetService<IMessageService>();
}
protected void Page_load(object sender, System.EventArgs e)
{
if (IsPostBack)
return;
ContentLiteral.Text = MessageService.GetMessage();
}
}
Final Result
Now that our module is complete you can install it into your DNN 9.4 or greater website and see Dependency Injection working in practice. Your module will look something like this.
Sample Code
If you had trouble getting this working take a look at the completed sampe code
Next Steps
You should now have a successfully building installer file that you can deploy to your DNN 9.4 or greater website that uses Dependency Injection. This new feature provides power and simplicity to module development.
If you are interested in learning more about other DNN Module patterns, look for the other blogs in the series, which will be announced as they are released.
-Happy Coding