在顶层使用DependencyInjection,如何在架构中传递服务?

msf*_*boy 3 .net c# dependency-injection mvvm viewmodel

使用像Unity,AutoFac或其他类似的IOC容器,您必须注册并解析IInterface以获取实例.这是你在app类中做的所有根.

在完成Register/Resolve之后,我正在创建我的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; 
    }
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是:

有很多通过/传递槽服务,因为我必须调用服务,例如viewmodels的属性通过gui控件上的lost_focus更改.

我做的不错吗?你觉得有什么不利吗?或者你会怎么做?

更新

DI的东西是对我恶性习惯的大规模攻击:P

  1. 你是这么想的吗?

  2. 顺便说一句.我为什么要做那个控制器工厂?那为什么不是ServiceFactory呢......那么我们回到ServiceLocator ......

  3. 如何在MainViewModel中获取控制器实例?通过扩展我的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>();
    }
}
Run Code Online (Sandbox Code Playgroud)

Update2:

现在我改变了我的代码,MainViewModel接受IControllerFactory作为参数,并将这两行代码添加到App类:

builder.Register<IControllerFactory, ControllerFactory>();
builder.Register<IContainerBuilder, ContainerBuilder>(); 
Run Code Online (Sandbox Code Playgroud)

这样我就不需要传递MainViewModel Ctor中的所有控制器,而是MainViewModel从Factory获取控制器实例.

我能在这做什么更好的事情吗?或者这是一个可接受的好解决方案?DI根本没有经验,所以我问:)

UPDATE3

好的我做了一些代码重构并为其他人做了评论,所以他们了解最终的解决方案:

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>();
    }
}
Run Code Online (Sandbox Code Playgroud)

非常感谢你的时间,@ Can.我学到了很多东西!

Can*_*cer 9

在我看来,你误解了如何使用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>();
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

更新:

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

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

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

这是一个更具体的MVVM问题,它可以帮助您了解如何构建类和依赖关系: 如何在WPF应用程序中组合MVVM和依赖注入?