Dependency Injection is a new feature coming to DNN in 9.4 that allows you to inject abstractions or other objects into your SPA Web API Module Controllers. This has been a common practice in both ASP.NET Web API 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 Request 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 (this blog)
- To Be Announced..
Getting Started
Getting started with Dependency Injection in a DNN SPA Web API 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 SPA Web API Module and follow along in the Dependency Injection Guide. The Sample code creates a simple HTML page and renders data from the Web API Controller using jQuery.
Go ahead and clone the repo or use your own.
1
git clone --branch spa_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.Spa_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 HomeController
. 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 "Hello 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 registration
}
}
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.
Add Constructor Injection
The IMessageService
has been correctly registered with the DNN Dependency Injeciton Provider. The SPA Web API Module is ready to start using Constructor Injection, 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
12
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Anonymous)]
public class HomeController : DnnApiController
{
protected IMessageService MessageService { get; }
public HomeController(IMessageService messageService)
{
MessageService = messageService;
}
// omitted code
}
**Yes, all you need to do is add the abstractions to the constructor parameters. **
Use the MessageService
Update the Get
action to use the injected IMessageService
1
2
3
4
5
6
[AllowAnonymous]
[HttpGet]
public string Get()
{
return MessageService.GetMessage();
}
Complete HomeController Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Anonymous)]
public class HomeController : DnnApiController
{
protected IMessageService MessageService { get; }
public HomeController(IMessageService messageService)
{
MessageService = messageService;
}
[AllowAnonymous]
[HttpGet]
public string Get()
{
return 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