Laura Ruiz

Laura Ruiz

  • 1.7k
  • 84
  • 951

Interfaces and Multiple Inheritance

Oct 3 2023 6:22 PM

I'm currently facing some uncertainties about the best approach to resolve a coding issue I've encountered. My project involves working with interfaces, and I'm unsure if this is the most optimal strategy.

Context:

I have a form consisting of 10 sections, each of which contains a variable number of subsections. For example:

  • Section 1
    • Subsection 1

    • Subsection 2

    • Subsection n

Each subsection has attributes that vary based on the state you're in. For instance, if you're in Florida, the subsections will share certain attributes with subsections in other states, but they will also have additional attributes specific to Florida.

My Attempt:

To tackle this complexity, I attempted to use abstract classes since I need to implement a method that returns a list of strings. However, I ran into the challenge of requiring multiple inheritance.

My question is: what would be the most effective approach in this case? Here's a snippet of my current code that employs interfaces and extension methods to handle this logic:

public interface ISubsection1Shared
{
    List<string>? Attribute1 { get; init; }
    List<string>? Attribute2 { get; init; }
    string? Attribute3 { get; init; }
    string? Attribute4 { get; init; }
    string? Attribute5 { get; init; }
    string? Attribute6 { get; init; }
    string? Attribute7 { get; init; }
    List<string>? Attribute8 { get; init; }
    List<string>? Attribute9 { get; init; }
}

public interface ISubsection1_FLorida : ISubsection1Shared
{
    List<string>? AdditionalAttributeFlorida { get; init; }
}

public interface ISubsection1_Texas : ISubsection1Shared
{
    List<string>? AdditionalAttributeTexas { get; init; }
}

public interface ISubsection1_RhodeIsland : ISubsection1_FLorida
{
    List<string>? AdditionalAttributeRhodeIsland { get; init; }
}

// Define the extension methods
public static class Statements
{
    public static List<string> Shared(this ISubsection1Shared subsection)
    {
        List<string> test = new List<string>();
        test.Add("shared stmt");
        return test;
    }

    public static List<string> Florida(this ISubsection1_FLorida subsection)
    {
        List<string> test = new List<string>();
        test.Add("Florida stmt");
        return test;
    }

    public static List<string> Texas(this ISubsection1_Texas subsection)
    {
        List<string> test = new List<string>();
        test.Add("Texas stmt");
        return test;
    }

    public static List<string> RhodeIsland(this ISubsection1_RhodeIsland subsection)
    {
        List<string> test = new List<string>();
        test.Add("RhodeIsland stmt");
        return test;
    }
}

public class Subsection1_RhodeIsland : ISubsection1_RhodeIsland
{
    public List<string>? Attribute1 { get; init; }
    public List<string>? Attribute2 { get; init; }
    public string? Attribute3 { get; init; }
    public string? Attribute4 { get; init; }
    public string? Attribute5 { get; init; }
    public string? Attribute6 { get; init; }
    public string? Attribute7 { get; init; }
    public List<string>? Attribute8 { get; init; }
    public List<string>? Attribute9 { get; init; }
    public List<string>? AdditionalAttributeFlorida { get; init; }
    public List<string>? AdditionalAttributeRhodeIsland { get; init; }

    public List<string> statements()
    {
        var sharedResult = this.Shared();
        var rhodeIslandResult = this.RhodeIsland();
        var floridaResult = this.Florida();

        List<string> statements = new List<string>();
        statements.AddRange(sharedResult);
        statements.AddRange(rhodeIslandResult);
        statements.AddRange(floridaResult);

        return statements;
    }
}

I'd greatly appreciate any guidance on how to enhance my approach or if there's a better way to tackle this problem. Thank you!

Note: The class names used in the code snippets (e.g., Subsection1_FLorida, Subsection1_Texas, etc.) are used here as placeholders for illustration purposes and do not represent actual locations or implementations in my code.


Answers (2)