DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Using dependency injection in a controller class

 8 Replies
 3 Subscribed to this topic
 26 Subscribed to this forum
Sort:
Author
Messages
Veteran Member
Posts: 1182
Veteran Member
MVP
MVP
You're an MVP!

Hi all,

how can I use dependency injection in a controller class (of a Webforms module) in DNN 9.8.0?

What I am trying to do is using IEventLogger (instead of EventLogController) in a Scheduler client. Something like

<code>IEventLogger _eventLogger = DependencyProvider.GetRequiredService< IEventLogger >();</code>

in the constructor. Any help is appreciated.

Happy DNNing!
Michael

Michael Tobisch
DNN★MVP

DNN Connect
Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!
I'm not sure what you mean by a controller class (that could be a business logic class, a web API controller, an MVC controller, unfortunately a very overloaded term). If you were really taking about a scheduler client, you can create a constructor with takes IEventLogger as an argument and the framework will automatically fill it in from the DI container.
Veteran Member
Posts: 1182
Veteran Member
MVP
MVP
You're an MVP!
Brian,

thanks for the answer - it is a business controller. Sorry, I thought this is clear as I mentioned a Webforms module, and a scheduler client.

Is there some example code somewhere?

Happy DNNing!
Michael

Michael Tobisch
DNN★MVP

DNN Connect
Veteran Member
Posts: 1182
Veteran Member
MVP
MVP
You're an MVP!

And another question: How can it be used in the IUpgradeable interface?

<code>public string UpgradeModule(string version)</code>

Happy DNNing!
Michael

Michael Tobisch
DNN★MVP

DNN Connect
Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!

If you're talking about the module's business controller class, it's not currently supported to use dependency injection with a business controller class (e.g. when implementing <code>IUpgradeable</code>).

In a case where you have a controller, like <code>WidgetsController</code>, that your module and scheduler client use, you can register it with the dependency injection container and then retrieve it in any place where you have access. For places where the framework doesn't support DI yet, you'll need to manually supply an instance. For example:

<code>WidgetsController</code>

<code>public class WidgetsController
{
    private readonly IEventLogger eventLogger;
    public WidgetsController(IEventLogger eventLogger)
    {
        this.eventLogger = eventLogger;
    }

    public WidgetsController()
        : this(new EventLogController())
    {
    }
}</code>

<code>Startup</code>

<code>public class Startup : IDnnStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<widgetscontroller>();
    }
}</widgetscontroller></code>

<code>WidgetsSchedulerClient</code>

<code>public class WidgetsSchedulerClient : SchedulerClient
{
    private readonly WidgetsController widgetsController;
    public WidgetsSchedulerClient(SchedulerHistoryItem item, WidgetsController widgetsController)
    {
        this.ScheduleHistoryItem = item;
        this.widgetsController = widgetsController;
    }
}</code>

<code>ViewWidgets</code>

<code>public class ViewWidgets : PortalModuleBase
{
    private readonly WidgetsController widgetsController;
    public ViewWidgets(WidgetsController widgetsController)
    {
        this.widgetsController = widgetsController ?? this.DependencyProvider.GetRequiredService≪WidgetsController≫();
    }

    public ViewWidgets()
        : this(null)
    {
    }
}</widgetscontroller></code>

Does that address your questions? Once we're able to add support for constructor injection for WebForms and for the business controller class, you can remove those parameterless constructors.

Veteran Member
Posts: 1182
Veteran Member
MVP
MVP
You're an MVP!

Thanks Brian,

that answers my question. It was just because I got this warning

<code>'EventLogController.AddLog(string, string, EventLogController.EventLogType)' is obsolete: 'Deprecated in 9.8.0. Use Dependency Injection to resolve 'DotNetNuke.Abstractions.Logging.IEventLogger' instead. Scheduled for removal in v11.0.0.'</code>

in the BusinessController class - I thought there is a already a way to get rid of it.

Happy DNNing!
Michael

Michael Tobisch
DNN★MVP

DNN Connect
Senior Member
Posts: 1607
Senior Member
MVP
MVP
You're an MVP!
New Poster
New Poster
Congrats on posting!
How much of this has changed at this point? 🤔
Advanced Member
Posts: 159
Advanced Member
MVP
MVP
You're an MVP!
Nothing has changed at this point, other than that we know that DNN 10 includes support for WebForms constructor injection and business controller class constructor injection.
Senior Member
Posts: 1607
Senior Member
MVP
MVP
You're an MVP!
New Poster
New Poster
Congrats on posting!
Posted By Brian Dukes on 7/17/2023 5:36 AM
Nothing has changed at this point, other than that we know that DNN 10 includes support for WebForms constructor injection and business controller class constructor injection.

Awesome... That's great to know!  :)

 

These Forums are for the discussion of the open source CMS DNN platform and ecosystem.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
  2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
  3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  5. No Flaming or Trolling.
  6. No Profanity, Racism, or Prejudice.
  7. Site Moderators have the final word on approving / removing a thread or post or comment.
  8. English language posting only, please.

Would you like to help us?

Awesome! Simply post in the forums using the link below and we'll get you started.

Get Involved