DirectClient<T>

Abstract client that calls a controller directly in the same memory space.

Inherits: IConfigurable, IReferenceable, IOpenable

Description

The DirectClientclass allows you to create clients that call a controller directly in the same memory space.

Important points

  • It is used when multiple microservices are deployed in a single container (monolyth) and communication between them can be done by direct calls rather than through the network.

Configuration parameters

  • dependencies:
    • controller: override controller descriptor

References

  • *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0 - (optional) ICounters components to pass collected measurements
  • *:traces:*:*:1.0 - (optional) ITracer components to record traces
  • *:discovery:*:*:1.0 - (optional) IDiscovery services to resolve a connection

Fields

_controller

Controller reference.

protected _controller: T

_opened

Open flag.

protected _opened: boolean = True

_logger

Logger.

protected _logger: CompositeLogger = CompositeLogger()

_counters

Performance counters

protected _counters: CompositeCounters = CompositeCounters()

_dependencyResolver

Dependency resolver used to get the controller’s reference.

protected _dependencyResolver: DependencyResolver = DependencyResolver()

Instance methods

CloseAsync

Closes a component and frees used resources.

public virtual Task CloseAsync(string correlationId)

  • correlationId: string - (optional) transaction id used to trace execution through the a chain.

Configure

Configures component by passing its configuration parameters.

public virtual void Configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

Instrument

Adds instrumentation to log calls and measures call time. It returns a Timing object that is used to end the time measurement.

protected CounterTiming Instrument(string correlationId, [CallerMemberName]string methodName = null)

  • correlationId: string - (optional) transaction id used to trace execution through a call chain.
  • methodName: [CallerMemberName]string - method name.
  • returns: CounterTiming - CounterTiming object used to end the time measurement.

InstrumentError

Adds instrumentation to error handling.

protected void InstrumentError(string correlationId, [CallerMemberName]string methodName = null, Exception ex = null, bool rethrow = false)

  • correlationId: string - (optional) transaction id used to trace execution through a call chain.
  • methodName: [CallerMemberName]string - method name.
  • ex: Exception - error that occured during the method call.
  • rethrow: bool - true to throw the exception.

IsOpen

Checks if the component is open.

public virtual bool IsOpen()

  • returns: bool - true if the component is open and false otherwise.

OpenAsync

Opens the component.

public virtual Task OpenAsync(string correlationId)

  • correlationId: string - (optional) transaction id used to trace execution through a call chain.

SetReferences

Sets references to dependent components.

public virtual void SetReferences(IReferences references)

  • references: IReferences - references to locate the component’s dependencies.

Examples

class MyDirectClient: DirectClient<IMyController>, IMyClient 
{
    public MyDirectClient()
    {   
        base();
        this._dependencyResolver.put("controller", new Descriptor("mygroup", "controller", "*", "*", "*"));
    }
    ...
    
    public MyData GetData(string correlationId, string id)
    {
        var timing = this.Instrument(correlationId, 'myclient.get_data');
        var result = this._controller.GetData(correlationId, id);
        timing.EndTiming();
        return result;
    }
    ...
}

var client = new MyDirectClient();
client.SetReferences(References.FromTuples(
    new Descriptor("mygroup","controller","default","default","1.0"), controller)
);
var data = client.GetData("123", "1");
...