2016-09-13 10 views
4

私はgRPCを使って簡単なCRUDサービスを構築しようとしていますが、大きな重なりのメッセージを作成しています。何度も繰り返しなくgRPCサービスでCRUDを作成するには?

message Todo { 
    // id is only available for a persisted entity in database. 
    string id = 1; 
    string content = 2; 
    // this is only available for users with admin role. 
    string secret_content = 3; 
} 

service Todos { 
    rpc CreateTodo(CreateRequest) returns (CreateResponse) {} 
    rpc ReadTodo(ReadRequest) returns (ReadResponse) {} 
} 

message CreateRequest { 
    // this todo is not supposed to have id, 
    // should I create another version of Todo without an id field? 
    Todo todo 
} 

message CreateResponse { 
    // this todo will always have an id. 
    Todo todo = 1; 
} 

message ReadRequest { 
    string id = 1; 
} 

message ReadResponse { 
    // this todo should only have the secret_content field if the 
    // user is authenticated as an admin, if not, the field should not 
    // fallback to the zero value, the whole field must be missing. 
    Todo todo = 1; 
} 

これはgRPCとリソースのようなCRUDを構築するための良いアプローチです:

この

は、最良の例で記述されていますか?つまり、リソースを表す単一のメッセージ( Todo)を持ち、アクションごとに応答/要求タイプでこのメッセージをラップします。

Todoタイプのメッセージには、すべてのリクエスト/レスポンスのすべてのフィールドが含まれていて、それぞれのフィールドで使用されていないフィールドは設定されていませんか?

答えて

1

Todoタイプのメッセージにはすべてのリクエスト/レスポンスのすべてのフィールドが含まれていて、それぞれのフィールドで使用されていないフィールドは設定されていませんか?

はい、これは合理的な設計のようです。 protobuf v2では、理解しやすいようにフィールドoptionalをマークしていました。しかし、v3では、デフォルトではすべてのフィールドがオプションです。

関連する問題