2017-10-12 40 views
0

我正在寻找关于如何为我想创建的网站实现某些内容的建议。C#,按顺序一次性使用多个外部API /服务

我想要使用一些内部托管的SOAP服务,但我想知道这样做的最佳方式是,在下一个请求中需要响应中的某些元素。我可以(之前已经完成)管理这个,但它不是很干净或清晰。

我已经和一些同事谈过了,我已经提到了责任链(CoR)设计模式,但我不是100%确定这是否正是我所需要的,如果是这样,我不能似乎想象这将如何在我脑海中发挥作用,因为我目前对其有限的理解。

流程将会是这样的:

用户填写表单哪些职位数据项姓氏DOB邮编

Service1 is then called with the above details in the request body 

Service1与一些其它的数据来响应,EG

然后
<CustomerId>99999</CustomerId> 

服务2需要与它本身需要在客户节点和其他人的主体构成。

我的肺心病的理解是,它会把直到能找到的东西来处理它,我想了解,如果这是,如果我需要用别的东西来使用的正确模式的要求?

我还假设最佳做法是将我在服务调用中收到的项目映射到对象中?

回答

0

是的,我认为CoR会为你工作。

这可能是一个实现:

public class Request 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Postcode { get; set; } 
} 

public class Response : Request 
{ 
} 

public class EntryPointService 
{ 
    private readonly IService1 _service1; 

    public EntryPointService(IService1 service1, IService2 service2, IService3 service3) 
    { 
     _service1 = service1; 

     // Create your chain 
     service1.RegisterNext(service2); 
     service2.RegisterNext(service3); 
     service3.RegisterNext(EndServiceHandler.Instance); 
    } 

    public Response Post(Request request) 
    { 
     // start your workflow 
     var response = _service1.TryToProcess(request); 

     return response; 
    } 
} 

public interface IChainProcessor 
{ 
    void RegisterNext(IChainProcessor next); 
    Response TryToProcess(Request request); 
} 

public interface IService1: IChainProcessor 
{ 
} 

public interface IService2: IChainProcessor 
{ 
} 

public interface IService3 : IChainProcessor 
{ 
} 


public class Service1: IService1 
{ 
    private IChainProcessor _next = EndServiceHandler.Instance; 

    public void RegisterNext(IChainProcessor next) 
    { 
     _next = next; 
    } 

    public Response TryToProcess(Request request) 
    { 
     var canProcess = true; // Add your validation logic here. 

     if (!canProcess) return _next.TryToProcess(request); 

     try 
     { 
      // Do your service1 workflow. 

      // DoSomething1(request); 
      // DoSomething2(request);   
     } 
     catch (ChainProcessException e) 
     { 
      // If something is wrong, Log and stop your workflow. 
      return new Response(); // <= add your return info here. 
     } 

     return _next.TryToProcess(request); 
    } 
} 

// Null object pattern: End chain element 
public class EndServiceHandler : IChainProcessor 
{ 
    public static EndServiceHandler Instance { get; } = new EndServiceHandler(); 

    public void RegisterNext(IChainProcessor next) 
    { 
     throw new InvalidOperationException(
      "Could not assign the next chain processor to the End of Workflow"); 
    } 

    public Response TryToProcess(Request loanRequest) 
    { 
     // add here you invalid state. 
     return new Response(); 
    } 
} 

注:

  1. 所有服务实现类似服务1
  2. 你管理你的请求的状态改变成请求对象,做在每个步骤中对其进行所有必要的更改,最后将其映射到响应对象。
  3. 如果你想停止流动,就可以做到这一点有两种方式:
    • 返回响应对象之前调用_next.TryToProcess(请求)
    • 投掷ChainProcessException到的try-catch
  4. 关于停止流程的想法是,当您需要返回验证消息,显示意外错误或返回用户的某些额外信息时。
  5. 你永远不应该到达EndServiceHandler,流程应该在此之前中断。
+0

谢谢,我正在尝试这个过程,将upvote一旦我有机会试图实现这一点 – Chris

相关问题