【发布时间】:2021-04-22 22:40:29
【问题描述】:
所以我的大部分编程都是在 java 中完成的,我使用了大量的 IoC 和 DPI,通过从 main 方法向新创建对象的构造函数注入所有必要的依赖项。 所以我将所有存储库注入服务,然后将服务注入控制器。这样,我只创建了每个依赖项的一个实例,并将它们注入到需要它们的地方。
在java中我这样做:
public class Main {
public static void main(String[] args) {
InterfaceRepository<Booking> bookingInterface = new BookingRepository();
InterfaceRepository<User> userInterface = new UserRepository();
InterfaceBookingService interfaceBookingService = new BookingService(bookingInterface, userInterface);
InterfaceUserService interfaceUserService = new UserService(userInterface);
BookingController bookingController = new BookingController(interfaceBookingService, interfaceUserService);
UserController userController = new UserController(interfaceUserService)
}
}
在 uwp 中,我完全不知道如何像在 java 中那样做。视图模型的实例化方式和位置以及如何将存储库注入视图模型;如果几个视图模型需要我的一些存储库,我可以将存储库的相同实例注入不同的视图模型吗?我希望能够测试我的视图模型,所以我也希望能够灵活地处理我注入的内容。
对不起,我没有任何代码,这是因为我什至无法在不了解如何进行 IoC 并遵循 S.O.L.I.D. 的情况下开始使用 c# 和 mvvm 在 uwp 中进行编码。原则
【问题讨论】:
标签: c# .net-core mvvm uwp inversion-of-control