Sharing services added with dependencies via the basic view

fatih uyanık 80 Reputation points
2024-01-25T13:31:57.54+00:00

Hello With injection I currently have 3 services and I use them on view models. Instead of defining these models individually within each view model, I defined them in the base view model. Then I try to use it wherever I want. The problem is: I need to send them as parameters one by one in each view model's constructor method. I chose this method to avoid code repetition. But now the problem of sending it as a parameter has arisen. Can you help me with this? Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,635 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,982 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 52,826 Reputation points
    2024-01-25T16:17:16.54+00:00

    DI uses the constructor to pass the injected objects. The .net runtime does not support constructor inheritance, so you need to define an injection constructor for each view model. Your base class can have a common constructor that is called.

    public MyModel(MyDI1 di1) : base(di)


  2. Hui Liu-MSFT 26,276 Reputation points Microsoft Vendor
    2024-01-29T09:34:25.3933333+00:00

    Hi,@fatih uyanık.Welcome Microsoft Q&A.

    For the issue about avoiding code repetition by defining common services in a base view model and then using dependency injection to provide those services to various view models. However, you are facing challenges in passing these services as parameters to the constructor of each view model.

    Here are a few approaches you could consider:

    Dependency Injection Container: Instead of manually passing services as parameters to each view model constructor, consider using a dependency injection (DI) container, such as Microsoft's Dependency Injection container. Register your services in the DI container during application startup. Resolve and inject these services into your view models automatically.

    Example with Microsoft.Extensions.DependencyInjection:

    
    // Startup.cs
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<IMyService, MyService>();
        // Register other services
    }
    
    // YourViewModel.cs
    public YourViewModel(IMyService myService)
    {
        // Use myService in your view model
    }
    
    

    BaseViewModel Initialization: Instead of passing services individually to each view model constructor, consider having a centralized method in your BaseViewModel that initializes the required services. Each view model calls this method during its initialization. Example:

    
    public class BaseViewModel
    {
        protected IService1 Service1 { get; private set; }
        protected IService2 Service2 { get; private set; }
    
        protected void InitializeServices(IService1 service1, IService2 service2)
        {
            Service1 = service1;
            Service2 = service2;
        }
    }
    
    public class YourViewModel : BaseViewModel
    {
        public YourViewModel(IService1 service1, IService2 service2)
        {
            InitializeServices(service1, service2);
            // Use Service1 and Service2 in your view model
        }
    }
    
    

    Choose the approach that best fits your application structure and requirements. Using a DI container is generally considered a good practice for managing dependencies in a clean and modular way.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.