简体   繁体   English

模式在不同的命名空间中调用相同的对象

[英]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. 我们的想法是命名空间Client1Client2存储在数据库中,并可用于检索正确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.

相关问题 如何分配在2个不同名称空间中声明的相同类对象 - How to assign same class object declared in 2 different namespaces 使用相同的方法从不同的命名空间实例化相同的对象 - Using the same method to instantiate identical object from different namespaces 具有相同对象名称的两个名称空间 - Two namespaces with the same object name 引用同一程序集中的不同项目,不同的名称空间 - Referencing a different project in the same assembly, different namespaces 为什么在映射具有相同名称但不同名称空间的类的对象图时,XmlSerializer无法初始化? - Why is XmlSerializer failing to initialize when mapping the object graph of classes with the same name, but different namespaces? 调用存在于多个命名空间中的相同方法 - call same method that exists in multiple namespaces 如何在不同的命名空间中处理相同的类名? - How to handle same class name in different namespaces? 内部类具有相同的命名空间但在不同的程序集中? - Internal classes having the same namespaces but in different assemblies? 相同的类,不同的命名空间,一种简化的方法? - Same class, different namespaces, a way to simplify? 如何切换具有相同名称和不同名称空间的类? - How to switch classes with same name and different namespaces?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM