【问题标题】:Is accepting domain objects as request payload in grpc an anti pattern?在 grpc 中接受域对象作为请求有效负载是一种反模式吗?
【发布时间】:2019-11-02 05:37:12
【问题描述】:

我有一个简单的服务,其中包含像

这样的消息定义
message BrandAttribute {
 option (gorm.opts) = {
    ormable: true
 };
 atlas.rpc.Identifier id = 1 [(gorm.field).tag = {type: "serial" primary_key: true}];
 string attribute_key = 2;
 string attribute_value = 3;
 atlas.rpc.Identifier brand_id = 4 [(gorm.field).tag = {not_null: true}];
 Status status = 5;
}

message Brand {
 option (gorm.opts) = {
    ormable: true
 };
 atlas.rpc.Identifier id = 1 [(gorm.field).tag = {type: "serial" primary_key: true}];
 atlas.rpc.Identifier external_id = 2 [(gorm.field).tag = {type: "text" unique: true}];
 string name = 3;
 string description = 4;
 string keywords = 5;
 string meta_keywords = 6;
 string meta_description = 7;
 repeated BrandAttribute brand_attributes = 8 [(gorm.field).has_many.foreignkey = "brand_id", (gorm.field).has_many.association_foreignkey = "id"];
 Status status = 9;
}

我已经定义了在上述定义的实体之上实现 CRUD 操作的服务,但是这些服务接受品牌本身作为创建/更新操作的请求负载(参数),这种方法是否正确?如果这不是实现这一目标的最佳方法。此外,如果我想使用 JDBC 来实现持久性,我将不得不自己装饰 protoc 在 protobuf 定义文件中不允许的实体,我该如何实现呢?

【问题讨论】:

    标签: protocol-buffers microservices grpc grpc-java


    【解决方案1】:

    Brand 用作请求的一个潜在问题是,如果您需要向请求中添加其他信息,但又不想将其添加到Brand,您将陷入困境。更好的做法是针对每种服务方法,始终为其定义专用的请求和响应类型。对于您的情况,您可以制作 CreateRequestUpdateRequest 消息等,其中包含 Brand

    【讨论】:

    • 如何通过将Brand 封装在CreateRequestUpdateRequest 中来避免向Brand 添加更多属性?
    • 因为您可以将新属性添加到CreateRequest 而不是Brand
    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    相关资源
    最近更新 更多