Skip to content

业务包桥接通信

业务功能包之间需要进行互相调用,需要使用桥接方式进行通信。通信方式如下:

  • 在公共包中定义通信接口。
  • 在业务实现包中,定义一个接口实现。
  • 在需要调用的业务包中,调用该接口即可。 例子:接口定义在Bridges/Interfaces文件中

定义接口

c#
namespace JingJian.Package.Common.Tests
{
    // 注意:此处SimpleA为实现该接口的服务名称
    [ImplementService("SimpleA")]
    public interface ISimpleACommonService : IBridgeService
    {
        TestResponse GetUser(TestRequest request);
    }

    // 此为调用代理,该类名必须以Proxy结尾
    public class SimpleACommonServiceProxy : ISimpleACommonService
    {
        public TestResponse GetUser(TestRequest request)
        {
            return ProxyInvoker.Invoke<TestRequest, TestResponse>(this, request);
        }
    }

    // 此为请求参数
    public class TestRequest
    {
        public string Name { get; set; } = string.Empty;

        public int Age { get; set; } = 0;
    }

  	// 此为返回参数
    public class TestResponse
    {
        public string Name { get; set; } = string.Empty;

        public int Age { get; set; } = 0;

        public bool Sexy { get; set; }

        public bool Sexy2 { get; set; }

        public bool Sexy3 { get; set; }

        public bool Sexy4 { get; set; }

        public bool Sexy5 { get; set; }
    }
}

接口业务实现

c#
namespace JingJian.Package.SimpleAService.Common
{
    public class SimpleACommonService : ISimpleACommonService, IDependency
    {
        public TestResponse GetUser(TestRequest request)
        {
            return new TestResponse() { Name = "回调数据", Age = 20 };
        }
    }
}

接口业务调用

c#
[ApiController]
[Route("api/[controller]")]
[AllowAnonymous]
public class TestController : ApiControllerBase
{
    private readonly ISimpleACommonService _simpleACommonService;

    public TestController(IRef<ISimpleACommonService> simpleACommonService)
    {
        _simpleACommonService = simpleACommonService.ValueInPackage(Package.Instance);
    }
    
    [HttpGet("get")]
    public object Get()
    {
        if (_simpleACommonService == null) return "未找到实现的服务";

        return _simpleACommonService.GetUser(new JingJian.Package.Common.Tests.TestRequest());
    }
}
c#
namespace JingJian.Package.SimpleBService
{
  	// 调用方需要继承接口IBridgeProxy,此为临时写法,
    public class Package : PackageBase, IBridgeProxiy
    {
        public static Package Instance => new();

        public override string PackageName => "SimpleB测试包";

        public override string ServiceName => "SimpleB";

        public override int PackageVersion => 1010001;

        public override string PackageVersionName => "1.0.1";

        public override void OnConfigureServices(IServiceCollection services)
        {
            base.OnConfigureServices(services);

            ProxyInvoker.Load();
        }
    }
}

广州宝点数字化科技