Should I use dependency injection when two instances must share a single third instance?

时间:2016-04-04 19:03:40

标签: c# wpf mvvm dependency-injection

I have an application that serves as both "Client" and "Server" to showcase single communication between these. Inside this application, I have a class called "ClientConnectionService" that holds all the methods for the client-side communication:

public class ClientConnectionService : ConnectionModelBase
{
    public string Connect()
    {
        //Some code here
    }

    public void OnDataReceived()
    {
        //Some code here
    }

    public virtual void WaitForData()
    {
        //Some code here
    }

    public void SendMessage(string msg)
    {
        //Some code here
    }

    public string Disconnect()
    {
        //Some code here
    }
}

Using MVVM, I made a View and a ViewModel for 2 UserControls: ConnectToServerView/ViewModel and ViewUserView/ViewModel.

public partial class ViewUserView : UserControl
{
    private ViewUserViewModel viewModel = new ViewUserViewModel();

    public ViewUserView()
    {
        DataContext = viewModel;
    }
}


public class ViewUserViewModel : ViewModelBase
{
        private ClientConnectionService clientConnectionService 
        = new ClientConnectionService();

        public ViewUserViewModel()
        {

        }

}

The other View/ViewModel is basically the same as the ones above, so I'll skip the code for that one (for simplicity purposes).

I KNOW this is wrong, as when trying to access the socket for the "Client" inside the ViewUserViewModel, it appears null (because I did not call the "Connect" method, where I actually set this "client socket"), because this instance of the "ClientConnectService" is not the same instance as the one I called in the "ConnectToServerViewModel".

TL;DR I have 2 viewModels with 2 different instances of a class, and I need to share that instance (of the class) between these (and possibly more) viewModels.

My question is: Is Dependency Injection the way to go? ('Cause I've been trying to implement it, but the interface is always null, so I think I'm missing something), or is there a way to make this "ClientConnectionService" class "sharable" between all my ViewModel instances (or the ones that need them, at least)

2 个答案:

答案 0 :(得分:1)

依赖注入需要您从容器中解析视图模型。这意味着您需要有一些方法来解析每个视图的视图模型,而不是实例化它们。使用一些现成的MVVM框架(如Caliburn Micro)时,最好做到这一点。

你真正想要的是Singleton pattern。尽管IoC很不错,但您可能不希望只在应用程序中使用一个单例。

如果您真的想在应用中使用IoC和DI,可以查看Caliburn MicroReactiveUI。两者都使用一些简单的服务定位器(检查herehere),用于解析VM和依赖关系。我还为Autofac做了一个sample adaptor与RxUI 6一起使用。

答案 1 :(得分:1)

跟随@Alexey Zimarev的回答,这是我的Singleton实现版本:

public class ClientConnectionService : ConnectionModelBase
{
    private static readonly ClientConnectionService instance 
    = new ClientConnectionService();

    private ClientConnectionService() { }

    public static ClientConnectionService getClientConnectionService()
    {
        return instance;
    }

    public string Connect(string ipAddress, int portNumber)
    {
        //Some code
    }

    //More code below, just omitted.
}

然后,只需在任何viewModel中调用它:

public class ConnectToServerViewModel : ViewModelBase
{
    #region Singletons

    private ClientConnectionService clientConnectionService =
    ClientConnectionService.getClientConnectionService();

    #endregion

    //Code omitted for simplicity purposes
}

这解决了我遇到的问题,但我真的想看看是否有另一种方法可以做到这一点(特别是DI,因为我真的对这个主题感兴趣)。

非常感谢所有贡献者。