简体   繁体   中英

uwp with c# and mvvm how to do inversion of control (dependency injection)

so most of my progamming has been done in java where i used a lot of IoC and DPI by injecting all the necessary depencies to the constructors of the newly created objects from the main method. so i injected all the repositories into the services, then the services into the controllers. in this way i only created one instance of each dependency and injected them wherever they were needed.

in java i do it like this:

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)

    }

}

in uwp i'm completely lost on how to do the same as i did in java. how and where are the viewmodels even instanced and how can i inject the repositories into the viewmodels; if some of my repositories are needed by several of the viewmodels, can i inject the same instance of the repositories to the different viewmodels? i want to be able to test my viewmodels, so i want to have the possibility to be flexible with what i inject as well.

i'm sorry i don't have any code, this is because i can't even get started coding in uwp with c# and mvvm without finding out how to be able to do IoC and follow the SOLID principles

About MVVM in UWP, you could refer to the document . There are samples in the document you could view that where the viewmodles are instanced.

You could use Microsoft.Extensions.DependencyInjection NuGet package to do the dependency injection.

Please check the following steps: 1.Create the MVVM architecture in a C# UWP project. 2.Click Tools in menu, select the option NuGet Package Manager > Manage NuGet Package for Solution… > Browse , enter Microsoft.Extensions.DependencyInjection in search box, install the package for your project. 3.Create Container object which maintains all of the dependencies that can be resolved anywhere in the application.

//App.xaml.cs
public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;
    Container = ConfigureDependencyInjection();
}
public IServiceProvider Container { get; }

IServiceProvider ConfigureDependencyInjection()
{
    var serviceCollection = new ServiceCollection();

    serviceCollection.AddTransient<IMessageService, MessageService>();

    return serviceCollection.BuildServiceProvider();
}

4.Create the MessageService class and the IMessageService interface.

//Add a new class to create the interface and class
public interface IMessageService
{
    string GetMessage();
}
public class MessageService : IMessageService
{
    public string GetMessage()
    {
        return "Hello from Message Service & Dependency Injection";
    }
}

5.Register MessageService to Container (referring to the code in step 3 ). 6.Inject MessageService into your viewmodel class such as MainViewModel class.

public class MainViewModel
{
    protected IMessageService MessageService { get; }
    public string Message { get => MessageService.GetMessage(); }
    public MainViewModel(IMessageService messageService)
    {
        MessageService = messageService;
        ……
    }
    ……
}

7.Resolve the MainViewModel , and inject the dependencies by using ActivatorUtilities function to inject parameters into the constructor.

//MainPage.xaml.cs
public MainPage()
{
    this.InitializeComponent();
    var container = ((App)App.Current).Container;
    DataContext = ActivatorUtilities.GetServiceOrCreateInstance(container, typeof(MainViewModel));
}

8.Show the message in page.

//MainPage.xaml
<Grid>
    <TextBlock Text="{Binding Message}" Margin="10" FontSize="20"/>
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM