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();
}
}
CodePudding user response:
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.