在顶层使用DependencyInjection,了解如何将服务传递到体系结构中
本文关键字:服务 体系结构 DependencyInjection 了解 | 更新日期: 2023-09-27 18:00:13
使用Unity、AutoFac或其他IOC容器,您必须注册并解析IInterface才能获得实例。这是你在应用程序类中所做的一切的根源。
在做了注册/解决的事情后,我正在创建我的主控制器,并将所有解决的服务传递给他们,如:
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;
}
}
现在的问题是:
有很多失败/失败的服务,因为例如,当视图模型的属性通过gui控件上的lost_focus更改时,我必须调用服务。
我这样做可以吗?你觉得有什么缺点吗?或者你会怎么做?
更新
DI的东西是对我坏习惯的大规模攻击:p
你是那个意思吗?
Btw。为什么我要做控制器工厂?那为什么不建立一个ServiceFactory呢。。。然后我们回到ServiceLocator。。。
我现在如何在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>();
}
}
更新2:
现在,我更改了MainViewModel接受IControllerFactory作为参数的代码,并将以下两行代码添加到应用程序类:
builder.Register<IControllerFactory, ControllerFactory>();
builder.Register<IContainerBuilder, ContainerBuilder>();
这样,我就不需要传递MainViewModel Ct中的所有控制器,或者MainViewModel从工厂获取控制器实例。
我能在这里做点什么吗?或者这是一个可以接受的好解决方案?我根本没有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对象的生命周期,并确保所有依赖项(需要初始化的属性和构造函数参数)都被注入和填充。
将会发生的事情是,容器将理解,要创建MainController的实例,它需要IUserService1、IUserService2等等,然后通过查看在容器中注册的其他类型来查看是否可以创建这些实例的任何实例。这将以递归的方式完成,以建立依赖关系树,直到一个类的所有依赖关系都可以满足为止。你得到的MainController已经注入了所有的依赖项
理想情况下,您应该在尽可能少的地方调用Resolve(),以便以只有一个根的方式构建应用程序。为了深入了解依赖注入,我强烈推荐Mark Seeman的《.NET中的依赖注入》一书,这本书在我看来是对DI最好的介绍之一。
更新:
我之所以建议使用ControllerFactory,是因为您的MainController中有很多UserController类,并且将所有这些作为依赖项传递,您最终会得到10多个构造函数参数,更不用说在创建新控制器时必须添加更多。如果你的视图模型只依赖于一个控制器,那么以这种方式使用工厂是没有意义的,你可以直接依赖于所需的控制器。
至于ServiceFactory,它是不需要的,因为您的每个类不太可能需要所有可用的服务类,只需要其中的一些。在这种情况下,最好为构造函数中的每个服务显式指定它们。
您还应该在一个地方(或在小型安装程序类中)注册所有实例,而不是在不同类的构造函数中。
以下是一个更具体的MVVM问题,它应该让您了解如何构建类和依赖项:如何在WPF应用程序中组合MVVM和依赖项注入?