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"));
}
}