[英]Pattern to call same object in different namespaces
I'm creating a web forms project in c#. 我正在用c#创建一个Web表单项目。
One of the key requirements is to post out to an API, however there's not one but several API's available. 其中一个关键要求是发布到API,但是没有一个API可用。
At present the HttpClient
object is uniquely defined for each API in a different namespace, like below: 目前,
HttpClient
对象是为不同命名空间中的每个API唯一定义的,如下所示:
MyCo.Myapp.PostApi.Client1.ClientObject
MyCo.Myapp.PostApi.Client2.ClientObject
Aside from the HttpClient
, the endpoint url and mediatypes are stored as well as a few other configuration objects. 除了
HttpClient
之外,还存储了端点url和mediatypes以及一些其他配置对象。
The idea being that the namespace Client1
or Client2
is stored in a database, and can be used to retrieve the location of the correct API, to create the object. 我们的想法是命名空间
Client1
或Client2
存储在数据库中,并可用于检索正确API的位置,以创建对象。
None of the above is set in stone - it's in concept form - so I wondered if there are any other alternatives to this solution which I can consider. 以上都不是一成不变的 - 它是概念形式的 - 所以我想知道这个解决方案是否有其他替代方案我可以考虑。
Any advice appreciated. 任何建议表示赞赏
This seems like a potential candidate for an Abstract Factory approach. 这似乎是抽象工厂方法的潜在候选者。 What you're describing sounds familiar to how one would connect to various database types using
System.Data.Common
您所描述的内容听起来很熟悉如何使用
System.Data.Common
连接到各种数据库类型
var settings = GetInfoFromDatabase();
var factory = ClientObjectFactory.Create(settings);
var clientObject = factory.CreateClientObject();
clientObject.Post(""); // This could be any of the client objects.
However since you don't have control over client APIs you could have the underlying factory perform the post: 但是,由于您无法控制客户端API,因此您可以让基础工厂执行帖子:
factory.Post(clientObject, data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.