C# - Flatter structure vs Nested structure

jsgg123

Gaegul

Posted on June 1, 2024

C# - Flatter structure vs Nested structure

Define the structure

  • what is the format I want to use? More nested? flatter object?
  • How do I make it more extensible?

Nested objects

"university": {
    "name": "sampleUni",
    "offeredCourses": {
        "SMPL101": {
            "courseName": "Intro to Sample Course 101", 
            "enrolledStudents": {
                "1": { 
                    "name": "John Doe", 
                    "age": 20 
                },
                "2": { 
                    "name": "Apple Smith", 
                    "age": 21
                }

            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

To achieve this nested structure, this is the code I wrote:

public class Program 
{
    public static void Main() 
    { 
        Student JohnDoe = new Student()
        {
            StudentName = "John Doe",
            Age = 20
        }; 

        Student AppleSmith = new Student()
        {
            StudentName = "Apple Smith",
            Age = 21
        }; 

        Dictionary<int, Student> enrolledStudents = new() 
        { 
            [1] = JohnDoe,
            [2] = AppleSmith
        }; 

        Course course1 = new Course() 
        { 
            CourseName = "Intro to Sample Course 101",
            EnrolledStudents = enrolledStudents
        };

        Dictionary<string, Course> offeredCourses = new() 
        { 
            ["SMPL101"] = course1
        }; 

        University u1 = new University() 
        { 
            Name = "sampleUni", 
            OfferedCourses = offeredCourses
        };

        // to achieve the indentation and the camelCase for json property. 
        // alternatively, we can also define the camelCase for individual property by using the [JsonPropertyName] attribute.
        JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions()
        {
            WriteIndented = true,
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        }; 

        string serializationResult = JsonSerializer.Serialize(u1, jsonSerializerOptions);
        Console.WriteLine(serializationResult);
    }
} 

Enter fullscreen mode Exit fullscreen mode

And, some examples of the DTO, Student and Course class:

public class Student 
{ 
    public string StudentName { get; set; }
    public int Age { get; set; }
    // Additional properties if we need to add more fields
}

public class Course 
{
    public string CourseName { get; set; }
    public Dictionary<int, Student> EnrolledStudents { get; set; } = []; 
    // Additional properties to add more fields, i.e. professor, time, maxStudents... 
}
Enter fullscreen mode Exit fullscreen mode

Advantages of nested json objects

  • intuitive and may be easier to understand
  • better encapsulation

Disadvantages of nested json objects

  • Deep nested objects are complex and data parsing/modification could get more difficult

Advantages of flatter json objects

  • simpler structure => good for simple data
  • faster data access/retrieval => less nested levels to traverse.

Disadvantages of flatter json objects

  • could result in more ambiguity
  • the relationship might not be as clear as the nested representation

Ultimately need to decide in conjunction with what I want to do with the data, think more about future changes, use cases with focus on OOP.

💖 💪 🙅 🚩
jsgg123
Gaegul

Posted on June 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related