使用 Unity、AutoFac 或其他 IOC 容器,您必须注册并解析 IInterface 才能获取实例。这是你在 app 类中所做的一切的根。

在完成注册/解析之后,我正在创建我的 MainController 并将它们传递给所有已解析的服务,例如:

protected void Application_Start(object sender, EventArgs e)
{
    var builder = new ContainerBuilder();

    builder.Register<IUserService1, UserService1>();
    builder.Register<IUserService2, UserService2>();
    builder.Register<IUserService3, UserService3>();
            builder.Register<IAnotherService, AnotherService>();
    // And many more Services...

    _container = builder.Build();

    var userService1 = _container.Resolve<IUserService1>();
    var userService2 = _container.Resolve<IUserService2>();
    var userService3 = _container.Resolve<IUserService3>();
var anotherService = _container.Resolve<IAnotherService>();

    var vm = new MainController(userService1,userService2,userService3,anotherService)
}

public class MainController
{
    private UserController1 _userVM1;
    private UserController2 _userVM2;
    private UserController3 _userVM3;

    public MainController(IUserService1 userService1,IUserService2 userService2,IUserService3 userService3,anotherService)
    {
        _userVM1 = new UserController1(userService1,anotherService);
        _userVM2 = new UserController2(userService2,...,...);
        _userVM3 = new UserController3(userService3,...,...,...);
    }
}

// Such a Controller class needs to be created 10 times... and what I do here is typical for all Controllers driving the GUI
public class UserController1
{
    private readonly IUserService1 _userService1;

    public UserController1(IUserService1 userService1,IAnotherService anotherService)
    {
        _userService1 = userService1;
        //Bind data to GUI
        UserData1Collection = ConvertModelIntoViewModelCollection(userService1,anotherService);
    }

    public ObservableCollection<UserData1> UserData1Collection { get; set; }

    private ObservableCollection<UserData1ViewModel> ConvertModelIntoViewModelCollection(IAnotherService anotherService)
    {
        var userData1ViewModelCollection = new ObservableCollection<UserData1ViewModel>();
        _userService1.GetUserData1().ForEach(user =>
        {
            userData1ViewModelCollection.Add(new UserData1ViewModel(user, anotherService,...));
        });
        return userData1ViewModelCollection;
    }
}

现在的问题是:

有很多通过/传递低谷服务,因为当例如 View 模型的属性通过 gui 控件上的 lost_focus 更改时,我必须调用服务。

我这样做可以吗?你看有什么缺点吗?或者你会怎么做?

更新

DI 的东西是对我的恶习的大规模攻击:P
  • 你是那个意思吗?
  • 顺便说一句。我为什么要做那个 Controller 工厂?为什么不也是一个 ServiceFactory...然后我们回到 ServiceLocator...
  • 现在如何在 MainViewModel 中获取 Controller 实例?通过使用许多附加参数扩展我的 MVM 的构造函数?最终得到 30 个参数? ...

  • protected override void OnStartup(StartupEventArgs e)
    {
        IContainerBuilder builder = new ContainerBuilder();
    
        // Firstly Register ALL existing Services
        builder.Register<IAdminService, AdminService>();
        builder.Register<IDocumentService, DocumentService>();
        builder.Register<ILessonPlannerService, LessonPlannerService>();
        builder.Register<IMediator, Mediator>();
        builder.Register<IMainRepository, MainRepository>();
        builder.Register<MainViewModel>();
    
        IContainer _container = builder.Build();
    
        // THEN Register ALL Controllers needing the previously registered Services
        IControllerFactory factory = new ControllerFactory(builder);
        IDailyPlanner controller1 = factory.Create<IDailyPlanner>();
        IWeeklyPlanner controller2 = factory.Create<IWeeklyPlanner>();
        SchoolclassAdministrationViewModel controller3 = factory.Create<SchoolclassAdministrationViewModel>();
    
        // THEN Register the mainViewModel(MainController) which should take ALL Services and ALL Controller... WOW thats a massive Ctor param count... is that pure? Did you mean it that way???
        MainViewModel mainViewModel = _container.Resolve<MainViewModel>();
    
        //MainWindow mainWindow = _container.Resolve<MainWindow>();
        //mainWindow.DataContext = mainViewModel;
        //mainWindow.ShowDialog();
    }
    
    public class ControllerFactory : IControllerFactory
    {
        private readonly IContainerBuilder _builder;
        private readonly IContainer _container;
    
        /// <summary>
        /// Takes the IOC container to register all Controllers
        /// </summary>
        public ControllerFactory(IContainerBuilder builder)
        {
            _builder = builder;
    
            _builder.Register<SchoolclassAdministrationViewModel>();
            _builder.Register<IDailyPlanner, LessonPlannerDailyViewModel>();
            _builder.Register<IWeeklyPlanner, LessonPlannerWeeklyViewModel>();
            _container = _builder.Build();
        }
    
        /// <summary>
        /// Returns an Instance of a given Type
        /// </summary>
        public T Create<T>()
        {
            return _container.Resolve<T>();
        }
    }
    

    更新 2 :

    现在我更改了 MainViewModel 接受 IControllerFactory 作为参数的代码,并将这两行代码添加到 App 类中:
    builder.Register<IControllerFactory, ControllerFactory>();
    builder.Register<IContainerBuilder, ContainerBuilder>();
    

    这样我就不需要在 MainViewModel Ctor 中传递所有 Controller ,而是 MainViewModel 从工厂获取 Controller 实例。

    我在这里有什么更好的办法吗?或者这是一个可以接受的好的解决方案?我对 DI 完全没有经验,所以我问:)

    更新3

    好的,我进行了一些代码重构并为其他人发表了评论,以便他们了解最终解决方案是什么:
    protected override void OnStartup(StartupEventArgs e)
    {
        IContainerBuilder builder = new ContainerBuilder();
    
        // Firstly Register ALL existing Services
        builder.Register<IAdminService, AdminService>();
        builder.Register<IDocumentService, DocumentService>();
        builder.Register<ILessonPlannerService, LessonPlannerService>();
        builder.Register<IMediator, Mediator>();
        builder.Register<IMainRepository, MainRepository>();
        builder.Register<IControllerFactory, ControllerFactory>();
        builder.Register<IDailyPlanner, LessonPlannerDailyViewModel>();
        builder.Register<IWeeklyPlanner, LessonPlannerWeeklyViewModel>();
    
        // Just for visual separation THEN register the MainController driving all other Controllers created via the IControllerFactory
        builder.Register<MainViewModel>();
    
        // Build the container
        IContainer container = builder.Build();
    
        // THEN Register the MainController which should take ALL IServices and the IFactory
        MainViewModel mainViewModel = container.Resolve<MainViewModel>();
    
        // LATER in the mainViewModel`s Ctor you can create all 10 Controller instances with the IControllerFactory like this
        // _dailyPlannerController = controllerFactory.Create<IDailyPlanner>();
    
        MainWindow mainWindow = new MainWindow();
        mainWindow.DataContext = mainViewModel;
        mainWindow.ShowDialog();
    }
    
    public class ControllerFactory : IControllerFactory
    {
        private readonly IContainer _container;
    
        /// <summary>
        /// Takes the IOC container to resolve all Controllers
        /// </summary>
        public ControllerFactory(IContainer container)
        {
            _container = container;
        }
    
        /// <summary>
        /// Returns an Instance of a given Type
        /// </summary>
        public T Create<T>()
        {
            return _container.Resolve<T>();
        }
    }
    

    非常感谢您的时间,@Can。我学到了很多!

    最佳答案

    在我看来,您误解了如何使用 IoC 容器。您不需要创建服务的实例并将它们作为参数传递,而是需要让容器为您解析它们。

    例如,您可以按如下方式重构代码以正确使用 IoC:

    protected void Application_Start(object sender, EventArgs e)
    {
        var builder = new ContainerBuilder();
    
        builder.Register<IUserService1, UserService1>();
        builder.Register<IUserService2, UserService2>();
        builder.Register<IUserService3, UserService3>();
        builder.Register<IAnotherService, AnotherService>();
    
        builder.Register<MainController, MainController>();
        // And many more Services...
    
        _container = builder.Build();
    
        //let the container inject all the required dependencies into MainController..
        var vm = _container.Resolve<MainController>();
    }
    

    在这种情况下,容器应该控制 MainController 对象的生命周期,并确保注入(inject)和填充所有依赖项(需要初始化的属性和构造函数参数)。

    将会发生的情况是,容器将了解要创建 MainController 的实例,它需要 IUserService1、IUserService2 等,然后通过查看向容器注册的其他类型来查看它是否可以创建这些实例的任何实例.这将以递归方式完成以构建依赖树,直到可以满足类的所有依赖关系。您得到的结果 MainController 已经将所有依赖项注入(inject)其中。

    理想情况下,您应该在尽可能少的地方调用 Resolve(),以便以只有一个根的方式构建您的应用程序。要深入了解依赖注入(inject),我强烈推荐 Mark Seeman 的书 Dependency Injection in .NET,在我看来,这是对 DI 最好的介绍之一。

    更新:

    我建议使用 ControllerFactory 的原因是因为你的 MainController 中有很多 UserController 类,并将所有这些作为依赖项传递,你最终会得到 10 多个构造函数参数,更不用说在创建时必须添加更多新 Controller 。如果您的 View 模型只依赖于一个 Controller ,那么以这种方式使用工厂是没有意义的,您可以直接依赖于所需的 Controller 。

    至于 ServiceFactory,它不是必需的,因为您的每个类不太可能需要所有可用的服务类,只需要其中的一些。在这种情况下,最好在构造函数中为每个服务显式指定它们。

    您还应该在一个地方(或在小型安装程序类中)而不是在不同类的构造函数中注册所有实例。

    这是一个更针对 MVVM 的问题,它应该让您了解如何构建您的类和依赖项:
    How can I combine MVVM and Dependency Injection in a WPF app?

    关于c# - 在顶层使用 DependencyInjection,如何将服务向下传递到架构?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6473075/

    10-12 04:19