Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 共享依赖项引用_C#_Interface_Dependency Injection_Abstraction - Fatal编程技术网

C# 共享依赖项引用

C# 共享依赖项引用,c#,interface,dependency-injection,abstraction,C#,Interface,Dependency Injection,Abstraction,我的域层中有一个IClientConnection接口,负责连接到IRC服务器。我想在服务层后面隐藏与服务器的通信 服务接口 我创建了两个服务接口IIrcConnectionService提供两种方法,ConnectToServer和Disconnect。它处理与IClientConnection实现的对话。第二个服务接口是IChannelService,提供了两种方法,JoinChannel和PartChannel public class IrcConnectionService : IIr

我的域层中有一个
IClientConnection
接口,负责连接到IRC服务器。我想在服务层后面隐藏与服务器的通信

服务接口 我创建了两个服务接口
IIrcConnectionService
提供两种方法,
ConnectToServer
Disconnect
。它处理与
IClientConnection
实现的对话。第二个服务接口是
IChannelService
,提供了两种方法,
JoinChannel
PartChannel

public class IrcConnectionService : IIrcConnectionService
{
    /// <summary>
    /// The client connection
    /// </summary>
    private IClientConnection clientConnection;

    /// <summary>
    /// The connected completed callback
    /// </summary>
    private Action connectedCompletedCallback;

    /// <summary>
    /// Initializes a new instance of the <see cref="IrcConnectionService"/> class.
    /// </summary>
    /// <param name="clientConnection">The client connection.</param>
    public IrcConnectionService(IClientConnection clientConnection)
    {
        this.clientConnection = clientConnection;
    }

    /// <summary>
    /// Connects to server.
    /// </summary>
    /// <param name="serverInfo">The server information.</param>
    /// <param name="userInfo">The user information.</param>
    /// <returns>Returns an awaitable Task</returns>
    public Task ConnectToServer(ServerConnectionInfo serverInfo, UserInformation userInfo, Action connectedCompletedCallback = null)
    {
        if (connectedCompletedCallback != null)
        {
            this.connectedCompletedCallback = connectedCompletedCallback;
            this.clientConnection.Connected += this.OnClientConnected;
        }

        IrcConnectionService.CacheConnection(this.clientConnection);
        return this.clientConnection.Connect(serverInfo, userInfo);
    }

    private void OnClientConnected(object sender, EventArgs e)
    {
        this.connectedCompletedCallback();
    }


    /// <summary>
    /// Disconnects from server.
    /// </summary>
    /// <param name="serverInfo">The server information.</param>
    public void DisconnectFromServer(ServerConnectionInfo serverInfo)
    {
        this.clientConnection.Connected -= this.OnClientConnected;
        this.clientConnection.Disconnect();
    }
}

public class ChannelService : IChannelService
{
    private IIrcConnectionService connectionService;

    private IClientConnection connection;

    public ChannelService(IIrcConnectionService connectionService)
    {
        this.connectionService = connectionService;
    }

    public Task JoinChannel(string server, string channelName, string password = "")
    {
        if (!this.connectionService.IsConnected)
        {
            throw new InvalidOperationException("You must connect to a server prior to joining a channel.");
        }

        if (!channelName.StartsWith("#"))
        {
            channelName = channelName.Insert(0, "#");
        }

        return this.connectionService.SendMessage($"JOIN {channelName}");
    }

    public Task PartChannel(string serverNaemstring channelName)
    {
        throw new NotImplementedException();
    }
}
我考虑的事情之一是构建一个内部
ConnectionCache
对象。
IIrcConnectionService
实现将按服务器名称将其
IClientConnection
引用推送到它。
IChannelService
将从
ConnectionCache
对象按服务器名称请求
IClientConnection

这听起来像是一条有效的路径吗?有没有我可以用不同的方式来解决这个问题的模式?如能就此提供任何指导,将不胜感激

public class ShellViewModel
{
    /// <summary>
    /// The irc connection service
    /// </summary>
    private IIrcConnectionService ircConnectionService;

    /// <summary>
    /// The channel service
    /// </summary>
    private IChannelService channelService;

    /// <summary>
    /// Initializes a new instance of the <see cref="ShellViewModel"/> class.
    /// </summary>
    /// <param name="connectionService">The connection service.</param>
    public ShellViewModel(IIrcConnectionService connectionService, IChannelService channelService)
    {
        this.ircConnectionService = connectionService;
        this.channelService = channelService;

        this.InitializeCommand = DelegateCommand.FromAsyncHandler(this.Initialize);
    }

    /// <summary>
    /// Gets the initialize command.
    /// </summary>
    public DelegateCommand InitializeCommand { get; private set; }

    /// <summary>
    /// Initializes this instance.
    /// </summary>
    /// <returns>Returns an awaitable Task</returns>
    private async Task Initialize()
    {
        var serverInfo = new ServerConnectionInfo { Url = "chat.freenode.net" };
        var userInfo = new UserInformation { PrimaryNickname = "DevTestUser" };

        // Connect to the server
        await this.ircConnectionService.ConnectToServer(
            serverInfo,
            userInfo,
            async () => await this.channelService.JoinChannel("#ModernIrc"));
    }
}