Skip to content

Commit c2d35d5

Browse files
authored
Dev (#18)
* - Refactor SendAsync method signatures for IServiceChannel and their implementations: This commit simplifies the `SendAsync` method signatures across `DefaultServiceChannel`, `IServiceChannel`, and `ServiceChannelRegistry` by removing generic type parameters for requests and using a single `IServiceRequest<TResponse>` or `IServiceRequest` parameter. This change enhances code readability and reduces complexity in method calls. * - bump version
1 parent 3c48273 commit c2d35d5

File tree

7 files changed

+61
-50
lines changed

7 files changed

+61
-50
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1919
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
2020

21-
<Version>0.6.7</Version>
21+
<Version>0.6.8</Version>
2222
</PropertyGroup>
2323
</Project>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,6 @@ Then remote services are called with IServiceChannel instance resolved from DI..
479479
//resolve service channel from DI
480480
var channel = provider.GetRequiredService<IServiceChannel>();
481481
//send request over channel to remote service
482-
var listResult = await channel.SendAsync<ListStoresRequest, ListStoresResponse>(new ListStoresRequest(), ct);
482+
var listResult = await channel.SendAsync(new ListStoresRequest(), ct);
483483

484484
```

samples/ServiceEndpointClient/Program.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,26 @@ static async Task CallRemoteServicesAsync(IServiceProvider hostProvider)
4848
//resolve service channel from DI
4949
var channel = provider.GetRequiredService<IServiceChannel>();
5050
//send request over channel to remote ServiceResultEndpoint
51-
var listResult = await channel.SendAsync<ListStoresRequest, ListStoresResponse>(
51+
var listResult = await channel.SendAsync(
5252
new ListStoresRequest(),
5353
"v1/storesWithServiceEndpoint/",
5454
default);
5555

5656
if (listResult.IsOk)
5757
{
58+
Console.WriteLine("***********************");
5859
Console.WriteLine($"ListStores complete. Total count: {listResult.Value.Stores.Count}");
60+
Console.WriteLine("***********************");
5961
var id = listResult.Value.Stores.FirstOrDefault()?.Id;
6062
if (id is not null)
6163
{
6264
//send request over channel to remote ServiceResultEndpoint
63-
var getResult = await channel.SendAsync<GetStoreByIdRequest, GetStoreByIdResponse>(
65+
var getResult = await channel.SendAsync(
6466
new GetStoreByIdRequest(Id: id.Value),
6567
"v1/storesWithServiceEndpoint/",
6668
default);
69+
Console.WriteLine("***********************");
70+
Console.WriteLine("GetStoreById response BEFORE delete:");
6771
if (getResult.IsOk)
6872
{
6973
Console.WriteLine(getResult.Value);
@@ -72,6 +76,34 @@ static async Task CallRemoteServicesAsync(IServiceProvider hostProvider)
7276
{
7377
Console.WriteLine(getResult.DumpMessages());
7478
}
79+
Console.WriteLine("***********************");
80+
81+
//send request over channel to remote ServiceResultEndpoint
82+
var deleteResult = await channel.SendAsync(
83+
new DeleteStoreRequest(Id: id.Value),
84+
"v1/storesWithServiceEndpoint/",
85+
default);
86+
Console.WriteLine("***********************");
87+
Console.WriteLine("DeleteStore response:");
88+
Console.WriteLine(deleteResult.DumpMessages());
89+
Console.WriteLine("***********************");
90+
91+
//send request over channel to remote ServiceResultEndpoint
92+
getResult = await channel.SendAsync(
93+
new GetStoreByIdRequest(Id: id.Value),
94+
"v1/storesWithServiceEndpoint/",
95+
default);
96+
Console.WriteLine("***********************");
97+
Console.WriteLine("GetStoreById response AFTER delete:");
98+
if (getResult.IsOk)
99+
{
100+
Console.WriteLine(getResult.Value);
101+
}
102+
else
103+
{
104+
Console.WriteLine(getResult.DumpMessages());
105+
}
106+
Console.WriteLine("***********************");
75107
}
76108
}
77109
else

src/ModEndpoints.RemoteServices/DefaultServiceChannel.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ public class DefaultServiceChannel(
1111
{
1212
private const string NoChannelRegistrationFound = "No channel registration found for request type {0}.";
1313

14-
public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(TRequest req, CancellationToken ct)
15-
where TRequest : IServiceRequest<TResponse>
14+
public async Task<Result<TResponse>> SendAsync<TResponse>(IServiceRequest<TResponse> req, CancellationToken ct)
1615
where TResponse : notnull
1716
{
18-
return await SendAsync<TRequest, TResponse>(req, null, ct);
17+
return await SendAsync(req, null, ct);
1918
}
2019

21-
public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
22-
TRequest req,
20+
public async Task<Result<TResponse>> SendAsync<TResponse>(
21+
IServiceRequest<TResponse> req,
2322
string? endpointUriPrefix,
2423
CancellationToken ct,
2524
Func<IServiceProvider, HttpRequestMessage, CancellationToken, Task>? httpRequestInterceptor = null,
2625
Func<IServiceProvider, HttpResponseMessage, CancellationToken, Task>? httpResponseInterceptor = null,
2726
string? uriResolverName = null,
2827
string? serializerName = null)
29-
where TRequest : IServiceRequest<TResponse>
3028
where TResponse : notnull
3129
{
3230
try
@@ -42,9 +40,10 @@ public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
4240
{
4341
return Result<TResponse>.Fail(requestUriResult);
4442
}
45-
if (!ServiceChannelRegistry.Instance.IsRequestRegistered<TRequest>(out var clientName))
43+
var requestType = req.GetType();
44+
if (!ServiceChannelRegistry.Instance.IsRequestRegistered(requestType, out var clientName))
4645
{
47-
return Result<TResponse>.CriticalError(string.Format(NoChannelRegistrationFound, typeof(TRequest)));
46+
return Result<TResponse>.CriticalError(string.Format(NoChannelRegistrationFound, requestType));
4847
}
4948
using (HttpRequestMessage httpReq = new(
5049
HttpMethod.Post,
@@ -73,20 +72,19 @@ public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
7372
}
7473
}
7574

76-
public async Task<Result> SendAsync<TRequest>(TRequest req, CancellationToken ct) where TRequest : IServiceRequest
75+
public async Task<Result> SendAsync(IServiceRequest req, CancellationToken ct)
7776
{
78-
return await SendAsync<TRequest>(req, null, ct);
77+
return await SendAsync(req, null, ct);
7978
}
8079

81-
public async Task<Result> SendAsync<TRequest>(
82-
TRequest req,
80+
public async Task<Result> SendAsync(
81+
IServiceRequest req,
8382
string? endpointUriPrefix,
8483
CancellationToken ct,
8584
Func<IServiceProvider, HttpRequestMessage, CancellationToken, Task>? httpRequestInterceptor = null,
8685
Func<IServiceProvider, HttpResponseMessage, CancellationToken, Task>? httpResponseInterceptor = null,
8786
string? uriResolverName = null,
8887
string? serializerName = null)
89-
where TRequest : IServiceRequest
9088
{
9189
try
9290
{
@@ -101,9 +99,10 @@ public async Task<Result> SendAsync<TRequest>(
10199
{
102100
return Result.Fail(requestUriResult);
103101
}
104-
if (!ServiceChannelRegistry.Instance.IsRequestRegistered<TRequest>(out var clientName))
102+
var requestType = req.GetType();
103+
if (!ServiceChannelRegistry.Instance.IsRequestRegistered(requestType, out var clientName))
105104
{
106-
return Result.CriticalError(string.Format(NoChannelRegistrationFound, typeof(TRequest)));
105+
return Result.CriticalError(string.Format(NoChannelRegistrationFound, requestType));
107106
}
108107
using (HttpRequestMessage httpReq = new(
109108
HttpMethod.Post,

src/ModEndpoints.RemoteServices/DefaultServiceChannelSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ValueTask<HttpContent> CreateContentAsync<TRequest>(
2828
{
2929
return new ValueTask<HttpContent>(
3030
JsonContent.Create(
31-
request,
31+
(object)request,
3232
null,
3333
options.SerializationOptions));
3434
}

src/ModEndpoints.RemoteServices/IServiceChannel.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@ public interface IServiceChannel
1111
/// <summary>
1212
/// Sends request to remote service endpoint.
1313
/// </summary>
14-
/// <typeparam name="TRequest">Type of ServiceEndpoint request that will be sent.</typeparam>
1514
/// <typeparam name="TResponse">ServiceEndpoint response type.</typeparam>
1615
/// <param name="req">Request to be sent.</param>
1716
/// <param name="ct">The <see cref="CancellationToken"/> to cancel operation.</param>
1817
/// <returns>Response of remote service endpoint or failure result.</returns>
19-
Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
20-
TRequest req,
18+
Task<Result<TResponse>> SendAsync<TResponse>(
19+
IServiceRequest<TResponse> req,
2120
CancellationToken ct)
22-
where TRequest : IServiceRequest<TResponse>
2321
where TResponse : notnull;
2422

2523
/// <summary>
2624
/// Sends request to remote service endpoint.
2725
/// </summary>
28-
/// <typeparam name="TRequest">Type of ServiceEndpoint request that will be sent.</typeparam>
2926
/// <typeparam name="TResponse">ServiceEndpoint response type.</typeparam>
3027
/// <param name="req">Request to be sent.</param>
3128
/// <param name="endpointUriPrefix">Path to append as prefix to resolved enpoint uri. Usually used to add path segments to configured client's base address.</param>
@@ -35,33 +32,29 @@ Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
3532
/// <param name="uriResolverName"><see cref="IServiceEndpointUriResolver"/> name to be used to resolve ServiceEnpoint Uri.</param>
3633
/// <param name="serializerName"><see cref="IServiceChannelSerializer"/> name to be used to resolve ServiceEnpoint Uri.</param>
3734
/// <returns>Response of remote service endpoint or failure result.</returns>
38-
Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
39-
TRequest req,
35+
Task<Result<TResponse>> SendAsync<TResponse>(
36+
IServiceRequest<TResponse> req,
4037
string? endpointUriPrefix,
4138
CancellationToken ct,
4239
Func<IServiceProvider, HttpRequestMessage, CancellationToken, Task>? httpRequestInterceptor = null,
4340
Func<IServiceProvider, HttpResponseMessage, CancellationToken, Task>? httpResponseInterceptor = null,
4441
string? uriResolverName = null,
4542
string? serializerName = null)
46-
where TRequest : IServiceRequest<TResponse>
4743
where TResponse : notnull;
4844

4945
/// <summary>
5046
/// Sends request to remote service endpoint.
5147
/// </summary>
52-
/// <typeparam name="TRequest">Type of ServiceEndpoint request that will be sent.</typeparam>
5348
/// <param name="req">Request to be sent.</param>
5449
/// <param name="ct">The <see cref="CancellationToken"/> to cancel operation.</param>
5550
/// <returns>Response of remote service endpoint or failure result.</returns>
56-
Task<Result> SendAsync<TRequest>(
57-
TRequest req,
58-
CancellationToken ct)
59-
where TRequest : IServiceRequest;
51+
Task<Result> SendAsync(
52+
IServiceRequest req,
53+
CancellationToken ct);
6054

6155
/// <summary>
6256
/// Sends request to remote service endpoint.
6357
/// </summary>
64-
/// <typeparam name="TRequest">Type of ServiceEndpoint request that will be sent.</typeparam>
6558
/// <param name="req">Request to be sent.</param>
6659
/// <param name="endpointUriPrefix">Path to append as prefix to resolved enpoint uri. Usually used to add path segments to configured client's base address.</param>
6760
/// <param name="ct">The <see cref="CancellationToken"/> to cancel operation.</param>
@@ -70,13 +63,12 @@ Task<Result> SendAsync<TRequest>(
7063
/// <param name="uriResolverName"><see cref="IServiceEndpointUriResolver"/> name to be used to resolve ServiceEnpoint Uri.</param>
7164
/// <param name="serializerName"><see cref="IServiceChannelSerializer"/> name to be used to resolve ServiceEnpoint Uri.</param>
7265
/// <returns>Response of remote service endpoint or failure result.</returns>
73-
Task<Result> SendAsync<TRequest>(
74-
TRequest req,
66+
Task<Result> SendAsync(
67+
IServiceRequest req,
7568
string? endpointUriPrefix,
7669
CancellationToken ct,
7770
Func<IServiceProvider, HttpRequestMessage, CancellationToken, Task>? httpRequestInterceptor = null,
7871
Func<IServiceProvider, HttpResponseMessage, CancellationToken, Task>? httpResponseInterceptor = null,
7972
string? uriResolverName = null,
80-
string? serializerName = null)
81-
where TRequest : IServiceRequest;
73+
string? serializerName = null);
8274
}

src/ModEndpoints.RemoteServices/ServiceChannelRegistry.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ private ServiceChannelRegistry()
2323
_httpClientList = new();
2424
}
2525

26-
public bool IsRequestRegistered<TRequest>()
27-
where TRequest : IServiceRequestMarker
28-
{
29-
return IsRequestRegistered(typeof(TRequest));
30-
}
31-
3226
public bool IsRequestRegistered(Type requestType)
3327
{
3428
return _registry.ContainsKey(requestType);
@@ -49,12 +43,6 @@ internal bool RegisterRequest(Type requestType, string clientName)
4943
return _registry.TryAdd(requestType, clientName);
5044
}
5145

52-
public bool IsRequestRegistered<TRequest>([NotNullWhen(true)] out string? clientName)
53-
where TRequest : IServiceRequestMarker
54-
{
55-
return IsRequestRegistered(typeof(TRequest), out clientName);
56-
}
57-
5846
public bool IsRequestRegistered(Type requestType, [NotNullWhen(true)] out string? clientName)
5947
{
6048
return _registry.TryGetValue(requestType, out clientName);

0 commit comments

Comments
 (0)