Home > Mobile >  Is there a good, readable way to define simple nested classes in C#
Is there a good, readable way to define simple nested classes in C#

Time:05-19

I'm an experienced C# software engineer. I've read the docs and dug through blogs. I suspect the answer is "No, C# doesn't support that", but it's worth asking here:

Suppose I have a JSON data description like this:

{
  "data": {
    "name": {
      "first_name": "Fred",
      "last_name": Smith,
    },
    "address": {
      "street": "123 Main Street",
      "city": "Anytown",
      "state": "CA"
    }
}

JSON makes it super easy to create deeply nested data structures. But in order to represent this same data structure in C#, I apparently need to define a separate class for EVERY nested structure, like this:

private class Data {
  public Name name { get; set; }
  public Address address { get; set; }
}

private class name {
  public string first_name { get; set; }
  public string last_name { get; set; }
}

private class Address {
  public string street { get; set; }
  public string city { get; set; }
  public string state { get; set; }
}

Is there any way to simplify this code to approach the simplicity of the JSON representation?

CodePudding user response:

You're confounding two different concepts. The JSON you included is defining the object. The C# equivalent of this might look like this, using anonymous types:

new
{
    data = new
    {
        name = new
        {
            first_name = "Fred",
            last_name = "Smith"
        },
        address = new
        {
            street = "123 Main Street",
            city = "Anytown",
            state = "CA"
        }
    }
}

The C# code you posted defined the types. The equivalent of this would have to be done in a language like TypeScript, since JavaScript doesn't have types yet. The TypeScript would look like this:

type response = {
  data: {
    name: {
      first_name: string,
      last_name: string
    },
    address: {
      street: string,
      city: string,
      state: string
    }
  }
}

There isn't a simple syntax for declaring a nested C# type like this. You can declare C# classes nested inside of other classes, but that doesn't let you declare the types inline with the property name like TypeScript does.

If you are trying to consume JSON in C#, and don't care to create strong types like the C# code above, you can avoid creating types entirely: Just deserialize the JSON to a dynamic type, or use a type like JToken and access its pieces using strings. (Details on how to do this will depend on your serialization framework.)

CodePudding user response:

If the classes are very dependent on each other, for example, name and addresss don't make sense outside data, you can define them inside data, as nested classes. You do not save code, but it is more encapsulated.

In that case, you do this:

private class Data
{
    public Name name { get; set; }
    public Address address { get; set; }

    #region Nested classes

    public class Name
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
    }

    public class Address
    {
        public string street { get; set; }
        public string city { get; set; }
        public string state { get; set; }
    }

    #endregion
}

With the region you can collapse that code and get your Data class less overloaded.

CodePudding user response:

If you are looking to build new types that are easier to read just use something like notepadd and make a json object first then use a conversion tool to auto have it made into c# objects.

Here's another way : What you could do is look into Generating an Avro Schema from C#

If you are looking to kind of automate the process you can do the following https://www.thecodebuzz.com/generate-avro-schema-from-json/

  • Related