简体   繁体   English

c# 模型到 protobuf 消息(gRPC 项目)

[英]c# models to protobuf messages (gRPC project)

I can find quite many resources on how to generate C# models from protobuf messages (it's even built into the Grpc.AspNetCore package), but not the other way around.我可以找到很多关于如何从 protobuf 消息生成 C# 模型的资源(它甚至内置在 Grpc.AspNetCore 包中),但反之则不行。

I've created a blazor webassembly client/server project similar to this one: https://github.com/stevejgordon/gRPCBasicSample/我创建了一个类似于此的 blazor webassembly 客户端/服务器项目: https://github.com/stevejgordon/gRPCBasicSample/

I have an Domain layer with lots of C# models that I'm using for the Application.我有一个域层,其中包含许多用于应用程序的 C# 模型。 I would like to convert these models into message reply "view models", instead of writing them all in hand (also I'm not even sure of the right conversion between c# and protobuf)我想将这些模型转换为消息回复“查看模型”,而不是全部写在手中(我什至不确定 c# 和 protobuf 之间的正确转换)

Example: C# Model:示例:C# Model:

    /// <summary>
    /// The area.
    /// </summary>
    public class Area
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Area"/> class.
        /// </summary>
        /// <param name="name">Name of the region.</param>
        /// <param name="areaType">What type of area is the region.</param>
        /// <param name="numberOfSupplies">number of supplies the region has.</param>
        /// <param name="numberOfMusterCrows">number of crowns the region has.</param>
        public Area(string name, AreaType areaType, int numberOfSupplies, int numberOfMusterCrows)
        {
            this.Name = name;
            this.AreaType = areaType;
            this.NumberOfSupplies = numberOfSupplies;
            this.NumberOfMusterCrowns = numberOfMusterCrows;
            this.CurrentArmy = new Collection<Unit>();
            this.AdjacentAreas = new Collection<Area>();
        }

        /// <summary>
        /// Gets the name of the region.
        /// </summary>
        public string Name { get; }

        /// <summary>
        /// Gets or sets which house the area is controlled by.
        /// </summary>
        public House ControlledBy { get; set; }

        /// <summary>
        /// Gets the type of area the region is.
        /// </summary>
        public AreaType AreaType { get; }

        /// <summary>
        /// Gets the adjacent regions the region has.
        /// </summary>
        public IEnumerable<Area> AdjacentAreas { get; internal set; }

        /// <summary>
        /// Gets the number of supplies (barrels) the region has.
        /// </summary>
        public int NumberOfSupplies { get; }

        /// <summary>
        /// Gets the number of crowns the region has.
        /// </summary>
        public int NumberOfMusterCrowns { get; }

        /// <summary>
        /// Gets or sets a value indicating whether the region is occupied by troops.
        /// </summary>
        public bool IsOccupied { get; set; }

        /// <summary>
        /// Gets a value indicating whether the region has a stronghold.
        /// </summary>
        public bool HasStronghold { get; }

        /// <summary>
        /// Gets a value indicating whether the region has a castle.
        /// </summary>
        public bool HasCastle { get; }

        /// <summary>
        /// Gets or sets the current armies occupying the region.
        /// </summary>
        public ICollection<Unit> CurrentArmy { get; set; }

        /// <summary>
        /// Gets or sets the current order token placed.
        /// </summary>
        public OrderToken? PlacedOrderToken { get; set; }
    }

Should turn into something like this (simplified):应该变成这样的东西(简化):

message AreaReply {
  string name = 1;
  enum AreaType {
    Land = 0;
    Water = 1;
  }
  AreaType areaType = 4;
  House controlledBy = 5;
}
message House {
    google.protobuf.StringValue name = 1;
}

Is there really no such generator out there?真的没有这样的发电机吗? :) :)

Thanks谢谢

Use this annotation on top of your class definition:在 class 定义之上使用此注释:

[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)] [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]

See: Protobuf-net serialization without annotation参见: Protobuf-net 无注释序列化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM