简体   繁体   中英

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.

I've created a blazor webassembly client/server project similar to this one: https://github.com/stevejgordon/gRPCBasicSample/

I have an Domain layer with lots of C# models that I'm using for the Application. 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)

Example: 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:

[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]

See: Protobuf-net serialization without annotation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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