私はこのapiクライアントICommunicationClient(url, tenant)
を私のIoCコンテナに登録しています。今、私は1からnのapiクライアントを持つことができるシナリオに直面しています。私はそれらのすべてを登録する必要があり、どのようにそれを処理するか分からない。私はSIの中にこのRegisterCollection
があるのを見た。同じオブジェクトを異なる設定で複数回登録する
ICommunicationClientProvider
を実際のクライアントのラッパーとして使用することを検討しています。それは登録されたすべてのクライアントとそれらを取得するためのメソッドを含むリストを含んでいます。私はこれが最善のアプローチではないと感じています。もちろん、私はアプリの他の部分に触れるように強制します。
public class CommunicationClientProvider : ICommunicationClientProvider
{
public CommunicationClientCollection CommunicationClientsCollection { get; set; }
public string Tenant { get; set; }
public ICommunicationClient GetClients()
{
return CommunicationClientsCollection[Tenant];
}
public void SetClients(CommunicationClientCollection clients)
{
CommunicationClientsCollection = clients;
}
}
public interface ICommunicationClientProvider
{
ICommunicationClient GetClients();
void SetClients(CommunicationClientCollection clients);
}
コレクション
public class CommunicationClientCollection : Dictionary<string, ICommunicationClient>
{
}
をホストするために、このここで私は、あなたがこれを行うためのより良い方法を知っていますかSI
var clients = new CommunicationClientProvider();
foreach (var supportedTenant in supportedTenants)
{
clients.CommunicationClientsCollection
.Add(supportedTenant, new CommunicationClient(
new Uri(configuration.AppSettings["communication_api." + supportedTenant]),
new TenantClientConfiguration(supportedTenant)));
}
container.RegisterSingleton<ICommunicationClientProvider>(clients);
に対してコレクションを登録しますか?これは、複数のデータベースがある場合などの通常のシナリオです。
UPDATE:からITenantContext部分 - これは基本的にどのように私のテナントコンテキストインターフェイスは、次のようになります。
public interface ITenantContext
{
string Tenant { get; set; }
}
と私は通信APIへの私のコールを作ってるんだところ、これは次のとおりです。
public class MoveRequestedHandler : IHandlerAsync<MoveRequested>
{
private readonly IJctConfigurationService _communicationClient;
private readonly ITenantContext _tenantContext;
public MoveRequestedHandler(IJctConfigurationService communicationClient, ITenantContext tenantContext)
{
_communicationClient = communicationClient;
_tenantContext = tenantContext;
}
public async Task<bool> Handle(MoveRequested message)
{
_tenantContext.Tenant = message.Tenant;
_communicationClient.ChangeApn(message.Imei, true);
return await Task.FromResult(true);
}
}
ここでは、ITenantContextを登録します。
container.RegisterSingleton<ITenantContext, TenantContext>();
テナントはMoveRequested
オブジェクト内に定義されています(message.Tenant
)。 CommunicationClientにそのテナントを認識させるにはどうすればよいですか?
あなたの質問は現在、あまりにも曖昧です。あなたの質問にいくつかのコードを示してください。 – Steven
私は潜在的な解決策を更新しました。詳細が必要な場合はお知らせください。 – Rober