-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] Generate DTOs from SBE IR for non-perf-sensitive usecases.
In some applications performance is not cricital. Some users would like to use SBE across their whole "estate", but don't want the "sharp edges" associated with using flyweight codecs, e.g., accidental escape. In this commit, I've added a first cut of DTO generation for C# and a simple test based on the Car Example. The DTOs support encoding and decoding via the generated codecs using `EncodeInto(CodecT codec)` and `DecodeFrom(CodecT codec)` methods. Currently there is no support for equality/comparison or read-only views over the data; although, these have been requested. Here are some points that we may or may not wish to change in the future: 1. Non-present (due to the encoded version) string/array data and repeating groups are represented as `null` rather than empty. 2. Non-present primitive values are represented as their associated null value rather than using nullable types. 3. Non-present bitsets are represented as `0`. 4. DTOs are generated via a separate `CodeGenerator` rather than a flag to the existing C# `CodeGenerator`.
- Loading branch information
Showing
7 changed files
with
1,353 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using Extension; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Org.SbeTool.Sbe.Dll; | ||
|
||
namespace Org.SbeTool.Sbe.Tests | ||
{ | ||
[TestClass] | ||
public class DtoTests | ||
{ | ||
[TestMethod] | ||
public void ShouldRoundTripCar() | ||
{ | ||
var inputByteArray = new byte[1024]; | ||
var inputBuffer = new DirectBuffer(inputByteArray); | ||
EncodeCar(inputBuffer); | ||
var decoder = new Car(); | ||
decoder.WrapForDecode(inputBuffer, 0, Car.BlockLength, Car.SchemaVersion); | ||
var decoderString = decoder.ToString(); | ||
var dto = new CarDto(); | ||
dto.DecodeFrom(decoder); | ||
var outputByteArray = new byte[1024]; | ||
var outputBuffer = new DirectBuffer(outputByteArray); | ||
var encoder = new Car(); | ||
encoder.WrapForEncode(outputBuffer, 0); | ||
dto.EncodeInto(encoder); | ||
var dtoString = dto.ToString(); | ||
CollectionAssert.AreEqual(inputByteArray, outputByteArray); | ||
Assert.AreEqual(decoderString, dtoString); | ||
} | ||
|
||
private static void EncodeCar(DirectBuffer buffer) | ||
{ | ||
var car = new Car(); | ||
car.WrapForEncode(buffer, 0); | ||
car.SerialNumber = 1234; | ||
car.ModelYear = 2013; | ||
car.Available = BooleanType.T; | ||
car.Code = Model.A; | ||
car.SetVehicleCode("ABCDEF"); | ||
|
||
for (int i = 0, size = Car.SomeNumbersLength; i < size; i++) | ||
{ | ||
car.SetSomeNumbers(i, (uint)i); | ||
} | ||
|
||
car.Extras = OptionalExtras.CruiseControl | OptionalExtras.SportsPack; | ||
|
||
car.CupHolderCount = 119; | ||
|
||
car.Engine.Capacity = 2000; | ||
car.Engine.NumCylinders = 4; | ||
car.Engine.SetManufacturerCode("ABC"); | ||
car.Engine.Efficiency = 35; | ||
car.Engine.BoosterEnabled = BooleanType.T; | ||
car.Engine.Booster.BoostType = BoostType.NITROUS; | ||
car.Engine.Booster.HorsePower = 200; | ||
|
||
var fuelFigures = car.FuelFiguresCount(3); | ||
fuelFigures.Next(); | ||
fuelFigures.Speed = 30; | ||
fuelFigures.Mpg = 35.9f; | ||
fuelFigures.SetUsageDescription("this is a description"); | ||
|
||
fuelFigures.Next(); | ||
fuelFigures.Speed = 55; | ||
fuelFigures.Mpg = 49.0f; | ||
fuelFigures.SetUsageDescription("this is a description"); | ||
|
||
fuelFigures.Next(); | ||
fuelFigures.Speed = 75; | ||
fuelFigures.Mpg = 40.0f; | ||
fuelFigures.SetUsageDescription("this is a description"); | ||
|
||
Car.PerformanceFiguresGroup perfFigures = car.PerformanceFiguresCount(2); | ||
perfFigures.Next(); | ||
perfFigures.OctaneRating = 95; | ||
|
||
Car.PerformanceFiguresGroup.AccelerationGroup acceleration = perfFigures.AccelerationCount(3).Next(); | ||
acceleration.Mph = 30; | ||
acceleration.Seconds = 4.0f; | ||
|
||
acceleration.Next(); | ||
acceleration.Mph = 60; | ||
acceleration.Seconds = 7.5f; | ||
|
||
acceleration.Next(); | ||
acceleration.Mph = 100; | ||
acceleration.Seconds = 12.2f; | ||
|
||
perfFigures.Next(); | ||
perfFigures.OctaneRating = 99; | ||
acceleration = perfFigures.AccelerationCount(3).Next(); | ||
|
||
acceleration.Mph = 30; | ||
acceleration.Seconds = 3.8f; | ||
|
||
acceleration.Next(); | ||
acceleration.Mph = 60; | ||
acceleration.Seconds = 7.1f; | ||
|
||
acceleration.Next(); | ||
acceleration.Mph = 100; | ||
acceleration.Seconds = 11.8f; | ||
|
||
car.SetManufacturer("Ford"); | ||
car.SetModel("Fiesta"); | ||
car.SetActivationCode("1234"); | ||
} | ||
} | ||
} |
Oops, something went wrong.