Skip to main content
Crisp Byte

Building a Proof of Concept

The format certainly looks sound. Could there be any surprises when we try to implement it? There's one way to find out. I'll write a Proof of Concept. Finally, we get to the code!

Looking Sharp #

My career experience has mostly been with .NET and web development. My latest job had me coding in Ruby on Rails. After a two year absence, I'm anxious to get back into the world of .NET. I'm going to do the implementation in C# with .NET 8.0.

Start at the interface #

When I code, I always start at the interfaces. Whether it's the user interface (UI) or the application programming interfaces (APIs), starting at the interfaces sets up good boundaries that guide good design. I'll start by defining the contract for an API that encodes and decodes SDBD data.

namespace SDBD;

public interface ICodec {
  Document Decode(byte[] data);
  byte[] Encode(Document document);
}

public record Document(
  Dictionary<string, string> Metadata,
  byte[] Data
);

A basic demo #

The demo program will be a command line encoder/decoder. To encode, pass a file path on its own or with -e as the first parameter. The program will encode the file to SDBD with the original filename embedded and write a file with a .sdbd extension. To decode, pass -d as the first parameter and the path to an .sdbd file. It will write a file with the original data and the original filename.

SDBD.ICodec codec = new SDBD.Codec();

var (encode, filepath) = ParseArgs(args);
var inputData = File.ReadAllBytes(filepath);
var filename = Path.GetFileName(filepath);

if(encode) {
  SDBD.Document document = new (
    new() {
      { "content-name", filename }
    },
    inputData
  );

  var outputData = codec.Encode(document);

  File.WriteAllBytes($"{filename}.sdbd", outputData);
} else {
  var document = codec.Decode(inputData);

  File.WriteAllBytes(document.Metadata["content-name"], document.Data);
}

(bool encode, string filepath) ParseArgs(string[] args) {
  return args switch {
    [var filepath] => (true, filepath),
    ["-d", var filepath] => (false, filepath),
    ["-e", var filepath] => (true, filepath),
    _ => throw new Exception("I don't like those arguments")
  };
}

First run #

I'll mock up an implementation of the interface to make sure the program is working. All it will do is echo back the data it's given, and give the document the name text.txt.

namespace SDBD;

public class Codec : ICodec {
  public Document Decode(byte[] data) {
    return new(
      new () {
        { "content-name", "text.txt" }
      },
      data
    );
  }

  public byte[] Encode(Document document) {
    return document.Data;
  }
}

If we run the program on any file, it will output the same file with the .sdbd extension added. Run the program on a file with the -d parameter and it will output the same data with the file name text.txt. Looking good so far.