You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nirum compiler currently generates client classes of services by inheriting a service and Client at a time. Although the current design helps to generate less duplicated code and works well in ordinary cases, it's not good at adding or changing transport.
Suppose we have 2 services: customer-service and message-service. The current compiler generates 4 classes (2 interfaces and 2 client implementations) from the schema: CustomerService, CustomerServiceClient, MessageService, and MessageServiceClient. These two client classes assume we use the default HTTP transport. What if we need to use our own transport layer (say it's our own authentication protocol based on HTTPS) with these service clients?
One possible way is to subclass these client classes and override hooks like make_request(). Add a class named CustomerServiceClient2 and make it to inherit CustomerServiceClient with overridden make_request(). Also add MessageServiceClient2 and do the same thing. What if we need to add a more service? Add AnotherServiveClient2 and do the same thing. Repeat this when you add a new service.
It's basically because we use inheritance to extend transport. It can be more flexible if we refactor it to use composition instead of inheritance.
The following is how the currently generated code looks like:
In the above Transport is an interface type and we need to provide the built-in transport implementation like HttpTransport. When we use clients the code would look like the below:
Nirum compiler currently generates client classes of services by inheriting a service and
Client
at a time. Although the current design helps to generate less duplicated code and works well in ordinary cases, it's not good at adding or changing transport.Suppose we have 2 services:
customer-service
andmessage-service
. The current compiler generates 4 classes (2 interfaces and 2 client implementations) from the schema:CustomerService
,CustomerServiceClient
,MessageService
, andMessageServiceClient
. These two client classes assume we use the default HTTP transport. What if we need to use our own transport layer (say it's our own authentication protocol based on HTTPS) with these service clients?One possible way is to subclass these client classes and override hooks like
make_request()
. Add a class namedCustomerServiceClient2
and make it to inheritCustomerServiceClient
with overriddenmake_request()
. Also addMessageServiceClient2
and do the same thing. What if we need to add a more service? AddAnotherServiveClient2
and do the same thing. Repeat this when you add a new service.It's basically because we use inheritance to extend transport. It can be more flexible if we refactor it to use composition instead of inheritance.
The following is how the currently generated code looks like:
It should be changed to the following way:
In the above
Transport
is an interface type and we need to provide the built-in transport implementation likeHttpTransport
. When we use clients the code would look like the below:@admire93 Any opinions?
The text was updated successfully, but these errors were encountered: