• Login
  • Register

DNN Forums

A community discussion page. We're starting from scratch, so...let's get the party started!

Using dependency injection in a controller class

You are not authorized to post a reply.
Sort:


Veteran Member


Posts:484
Veteran Member

    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

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

    in the constructor. Any help is appreciated.

    Happy DNNing!
    Michael

    Michael Tobisch
    DNN★MVP

    dnnWerk Austria
    DNN Connect


    Basic Member


    Posts:87
    Basic Member

      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.
      DNN partner specializing in custom, enterprise DNN development https://engagesoftware.com/showcase


      Veteran Member


      Posts:484
      Veteran Member

        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

        dnnWerk Austria
        DNN Connect


        Veteran Member


        Posts:484
        Veteran Member

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

          public string UpgradeModule(string version)

          Happy DNNing!
          Michael

          Michael Tobisch
          DNN★MVP

          dnnWerk Austria
          DNN Connect


          Basic Member


          Posts:87
          Basic Member

            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 IUpgradeable).

            In a case where you have a controller, like WidgetsController, 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:

            WidgetsController

            public class WidgetsController
            {
                private readonly IEventLogger eventLogger;
                public WidgetsController(IEventLogger eventLogger)
                {
                    this.eventLogger = eventLogger;
                }
            
                public WidgetsController()
                    : this(new EventLogController())
                {
                }
            }

            Startup

            public class Startup : IDnnStartup
            {
                public void ConfigureServices(IServiceCollection services)
                {
                    services.AddTransient();
                }
            }

            WidgetsSchedulerClient

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

            ViewWidgets

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

            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.

            DNN partner specializing in custom, enterprise DNN development https://engagesoftware.com/showcase


            Veteran Member


            Posts:484
            Veteran Member

              Thanks Brian,

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

              '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.'

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

              Happy DNNing!
              Michael

              Michael Tobisch
              DNN★MVP

              dnnWerk Austria
              DNN Connect
              You are not authorized to post a reply.

              These Forums are dedicated to discussion of DNN Platform.

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

              1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
              2. 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.
              3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
              4. No Flaming or Trolling.
              5. No Profanity, Racism, or Prejudice.
              6. Site Moderators have the final word on approving / removing a thread or post or comment.
              7. 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