DNN Forums

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

Using dependency injection in a controller class

Sort:
You are not authorized to post a reply.





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





    Advanced 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





        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





          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





          Advanced 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





              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





              Veteran Member





                How much of this has changed at this point? 🤔





                Advanced Member





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





                  Veteran Member





                    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!  :)

                     

                    You are not authorized to post a reply.

                    These Forums are dedicated to the 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. 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