How do I instantiate a class with a constructor?

Daniel

I want to be able to run my RunApp method with my StartUp class in my Console App with a Static Void Main Method. The problem is that I use dependecy injection in StartUp class with a constructor to make an instance with other classes with methods. But I do not know how to proceed so I can use my RunApp method inside static void Main.

I have tried to use

StartUp s = new StartUp();
s.RunApp();

But it does not seem to work, I have to have parameters to enter.

StartUp Class:

 public class StartUp : IStartUp
    {

        private readonly AddCustomer _addCustomer;
        private readonly Booking _booking;
        private readonly GetCustomer _getCustomer;
        private readonly Service _service;


        public StartUp(AddCustomer addCustomer, Booking booking, GetCustomer getCustomer, Service service)
        {
            _addCustomer = addCustomer;
            _booking = booking;
            _getCustomer = getCustomer;
            _service = service;
        }



        public void RunApp()
        {

            Console.WriteLine("Hi! Welcome to Kennel GoldenRetriver. What would you like to do?");
            Console.WriteLine("Press 1 to register a new customer and dog");
            Console.WriteLine("Press 2 to show all customers");
            Console.WriteLine("Press 3 to get all dogs");
            Console.WriteLine("Press 4 to show customers and thier related dogs");
            Console.WriteLine("Press 5 to leave dog on service");
            Console.WriteLine("Press 6 to show all dogs on service");
            Console.WriteLine("Press 7 to get your dog from service");

       

            bool isNumber = int.TryParse(Console.ReadLine(), out int start);

     
            if (isNumber)
            {
                switch (start)
                {
                    case 1:
                        _addCustomer.AddCustomers();
                        break;
                    case 2:
                        _getCustomer.GetCustomers();
                        break;
                    case 3:
                        _getCustomer.GetDogs();
                        break;
                    case 4:
                        _getCustomer.GetRelatedCustomerToDog();
                        break;
                    case 5:
                        _booking.AddBooking();
                        _service.AddService();
                        break;
                    case 6:
                        _service.AddService();
                        break;
                    case 7:
                        _service.DeleteFromService();
                        break;
                    default:
                        Console.WriteLine("Please enter valid number");
                        break;
                }
            }
            else
            {
                Console.WriteLine("Enter a number");
            }

        }
    }

My Main Method

    class Program 
    {
        


        static void Main(string[] args)
        {
            StartUp s = new StartUp();
            s.RunApp();
        }

        
    }
Nkosi

The subject class needs to have all its dependencies satisfied in order for it to be initialized as desired.

So either provide them via Pure DI

class Program {
    static void Main(string[] args) {
        //...assuming the dependencies don't have dependencies themselves.
        AddCustomer addCustomer = new();
        Booking booking = new();
        GetCustomer getCustomer = new();
        Service service = new();
        
        StartUp s = new StartUp(addCustomer, booking, getCustomer, service);
        s.RunApp();
    }
}

or via an Inversion of Control (IoC) container that will do the heavy lifting of initializing and injecting all the registered dependencies for you.

Simple example using default .Net Core container

class Program {
    static void Main(string[] args) {
        IServiceProvider services = new ServiceCollection()
            .AddTransient<AddCustomer>()
            .AddTransient<Booking>()
            .AddTransient<GetCustomer>()
            .AddTransient<Service>()
            .AddTransient<StartUp>()
            .BuildServiceProvider();
        
        StartUp s = services.GetService<StartUp>();
        s.RunApp();
    }
}

In the overly simplified Pure DI example, if any of those dependencies have explicit dependencies, then those too need to be satisfied so that all requirements are available when initializing the subject type.

The choice is yours depending on the complexity and size of the program.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I test this static member in a class with private constructor?

From Java

How do I programmatically compile and instantiate a Java class?

From Java

How to instantiate a Java abstract class with a constructor parameter with a groovy closure

From Dev

How to instantiate a .NET class in PHP which has a parameterized constructor?

From Dev

How can I make a PHP class instantiate another class in its constructor?

From Dev

how to instantiate a class with parameters outside constructor?

From Dev

Why can I not instantiate a class whose constructor is private in a friend class?

From Dev

c++ How do i call a constructor of an interior class with templates?

From Dev

How do I instantiate an instance of a class in Python?

From Dev

How do I instantiate a Scala case class using all the attributes of a smaller case class?

From Dev

How do I detect if anything but an integer is passed to my class constructor?

From Dev

How to instantiate a template class when constructor takes arguments

From Dev

How to instantiate trait which extends class with constructor

From Dev

How do I access an inner class constructor from a derived class?

From Dev

C++ - How to instantiate object with constructor private in another class

From Dev

How do I verify that a specific constructor of a given class is called?

From Dev

How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

From Dev

How do I implement a Decorator class of abstract class with no default constructor?

From Dev

How do I create an instance of a inner class in the outer class constructor

From Dev

How do I get the original constructor for a HTML DOM class?

From Dev

How to dynamically instantiate a class with parameterized constructor using std::vector?

From Dev

How do I call the constructor of a class' member?

From Dev

How can I instantiate C++ class A in the constructor of class B such that its object is available to the member functions

From Dev

How to instantiate a class with base in the constructor?

From Dev

How do I instantiate a covergroup within a class?

From Dev

How to instantiate a class with an internal constructor in Kotlin

From Dev

How to instantiate base class as derived class in constructor?

From Dev

How can I instantiate a class with custom names?

From Dev

How do I instantiate structs inside a nested class?

Related Related

  1. 1

    How do I test this static member in a class with private constructor?

  2. 2

    How do I programmatically compile and instantiate a Java class?

  3. 3

    How to instantiate a Java abstract class with a constructor parameter with a groovy closure

  4. 4

    How to instantiate a .NET class in PHP which has a parameterized constructor?

  5. 5

    How can I make a PHP class instantiate another class in its constructor?

  6. 6

    how to instantiate a class with parameters outside constructor?

  7. 7

    Why can I not instantiate a class whose constructor is private in a friend class?

  8. 8

    c++ How do i call a constructor of an interior class with templates?

  9. 9

    How do I instantiate an instance of a class in Python?

  10. 10

    How do I instantiate a Scala case class using all the attributes of a smaller case class?

  11. 11

    How do I detect if anything but an integer is passed to my class constructor?

  12. 12

    How to instantiate a template class when constructor takes arguments

  13. 13

    How to instantiate trait which extends class with constructor

  14. 14

    How do I access an inner class constructor from a derived class?

  15. 15

    C++ - How to instantiate object with constructor private in another class

  16. 16

    How do I verify that a specific constructor of a given class is called?

  17. 17

    How do I use Unity Container to Instantiate a DAL class From a BLL class in a Winforms App

  18. 18

    How do I implement a Decorator class of abstract class with no default constructor?

  19. 19

    How do I create an instance of a inner class in the outer class constructor

  20. 20

    How do I get the original constructor for a HTML DOM class?

  21. 21

    How to dynamically instantiate a class with parameterized constructor using std::vector?

  22. 22

    How do I call the constructor of a class' member?

  23. 23

    How can I instantiate C++ class A in the constructor of class B such that its object is available to the member functions

  24. 24

    How to instantiate a class with base in the constructor?

  25. 25

    How do I instantiate a covergroup within a class?

  26. 26

    How to instantiate a class with an internal constructor in Kotlin

  27. 27

    How to instantiate base class as derived class in constructor?

  28. 28

    How can I instantiate a class with custom names?

  29. 29

    How do I instantiate structs inside a nested class?

HotTag

Archive