Multiple API Calls: Design Patterns

I need to call multiple services having multiple APIs. Few of them are mostly read (they return some data to me), and few of them change the state of several objects (they basically update the state of several objects).

I am looking for a design pattern that can be applied to the scenario described above.

Code example

Let's look at a small example of service A

 AccountInfo A.getAccountInfo() void A.setAccountData(AccountInfo) AccountStatus A.getStatusForAccount 

...

I was thinking about having a universal interface

 interface CallAPI<Input , Output> { public Output execute(Input) } 

Each API call will implement this interface, and I can use the Factory pattern to get instances of the API.

I would like to know if there is a better template for the same or whether it can be reorganized differently. APIs and services will only grow, and it will be easier for them to configure new APIs, and clients should not have additional overhead for writing adapters for new APIs.

+6
source share
1 answer

The best approach would be to start with agnostics from a solid design of databases, infrastructure details, etc. The path to use with FactoryMethod and if you want to create a family of objects, use an abstract factory so that you can have SqlFamily objects, JsonFamily objects, etc. Then you can create business rules in which you can use several services to make it more interesting. The Agood project involves the use of several desing templates, so the option will also complement the factories using template methods for code reuse, as well as a strategy template, etc. Here is the code where you can define the various descriptor templates discussed above:

 public interface IRead<T> { T Fetch(); } public abstract class ServiceA { public abstract void OperationA(); public abstract void OperationB(); } public abstract class ServiceB { public abstract void OperationD(); public abstract void OperationE(); } public abstract class JsonServiceAReaderTemplate : IRead<ServiceA> { public abstract ServiceA Fetch(); } public sealed class JsonServiceAFetcher : JsonServiceAReaderTemplate { /// <summary> /// Get parameters needded to load from json /// </summary> public JsonServiceAFetcher() { } public override ServiceA Fetch() { //Load from Json store throw new NotImplementedException(); } } public abstract class JsonServiceBReaderTemplate : IRead<ServiceB> { public abstract ServiceB Fetch(); } public sealed class JsonServiceBFetcher : JsonServiceBReaderTemplate { /// <summary> /// Get parameters needded to load from json /// </summary> public JsonServiceBFetcher() { } public override ServiceB Fetch() { //Load from Json store throw new NotImplementedException(); } } public sealed class ServicesGateway { public ServiceA ServiceA { get; } public ServiceB ServiceB { get; } public ServicesGateway(IRead<ServiceA> serviceA, IRead<ServiceB> serviceB) { ServiceA = serviceA.Fetch(); ServiceB = serviceB.Fetch(); } } public interface IBusinessRule { void Execute(); } public sealed class BusinessRuleServiceAServiceB : IBusinessRule { private readonly ServicesGateway servicesGateway; public BusinessRuleServiceAServiceB(ServicesGateway servicesGateway) { this.servicesGateway = servicesGateway; } public void Execute() { var serviceA = servicesGateway.ServiceA; var serviceB = servicesGateway.ServiceB; serviceA.OperationA(); serviceA.OperationB(); serviceB.OperationD(); serviceB.OperationE(); } } public sealed class ClientCode { public void Run() { //Inject from IoC ServicesGateway gateway = new ServicesGateway(new JsonServiceAFetcher(), new JsonServiceBFetcher()); var businessRule = new BusinessRuleServiceAServiceB(gateway); businessRule.Execute(); } } 

Hope this helps!

0
source

All Articles