如何使用构造函数实例化 class?

How do I instantiate a class with a constructor?

我希望能够 运行 我的 RunApp 方法与我的 StartUp class 在我的控制台应用程序中使用 static void Main方法。问题是我在 StartUp class 中使用依赖注入和构造函数来创建其他 class 方法的实例。但我不知道如何继续,所以我可以在 static void Main 中使用我的 RunApp 方法。

我试过用

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

但是好像不行,必须要有参数才能输入

启动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");
        }
    }
}

我的主要方法

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

主题 class 需要满足其所有依赖关系才能根据需要对其进行初始化。

所以要么通过 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();
    }
}

或者通过控制反转 (IoC) 容器,它会为您完成初始化和注入所有已注册依赖项的繁重工作。

使用默认 .Net Core 容器的简单示例

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();
    }
}

在过度简化的 Pure DI 示例中,如果这些依赖项中的任何一个具有显式依赖项,那么也需要满足这些依赖项,以便在初始化主题类型时所有要求都可用。

您可以根据程序的复杂程度和大小进行选择。