Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:06:04+00:00 2026-06-05T01:06:04+00:00

I have a tricky modelling problem, on which I’d like to have another point

  • 0

I have a tricky modelling problem, on which I’d like to have another point of view.

I’m dealing with a model that is treated as a whole by some engines. It looks like that (names are not what we are using):

public class Container{
  // Some other specific properties and methods, ctor etc

  public IList<Group> Groups {get; private set;}

  // For convenience, we need that
  public IList<Element> Elements {get; private set;}
}

public class Group{
  // Specific stuff here      

  public IList<Element> Elements{get; private set;}

  public Container Container{get; set;}
}

public class Element{
  public Group Group {get; set;}
}

All along the model, we have those double references, because the model is treated as a whole by engines.

Now, the tricky part: I need to create 2 specific models, sharing this organization.

Currently, I have a model with three classes: Container – Group – Element.

I need to create specific models sharing the same structure, but with specific classes all along the model.

  • BaseContainer, BaseGroup, BaseElement (parent)
  • ContainerFoo – GroupFoo – ElementFoo (first child)
  • ContainerBar – GroupBar – ElementBar (second child)

And, to be complete, the most important thing is that I need strongly typed collections. For example, I need a list of GroupFoo in ContainerFoo, not a list of the parent type.

I explored 2 ways of achieving this : generics and creating new properties.

For example:

public class BaseContainer<TGroup, TElement>{
  public IList<TGroup> Groups {get; private set;}

  public IList<TElement> Elements{get; private set;}  
}

public class BaseGroup<TContainer, TElement>{
  public TContainer Container {get; set;}

  public IList<TElement> Elements {get; private set;}
}

public class BaseElement<TGroup>{

  public TGroup Group{get; set;}

}

public class ContainerFoo: BaseContainer<GroupFoo, ElementFoo>{
  // Specific stuff here
}

public class GroupFoo: BaseGroup<ContainerFoo, ElementFoo>{
}

public class ElementFoo: BaseElement<ContainerFoo>{
}

This solution works in this case but:

  • List of types in container can be very long, because the container is actually the entry point of the whole model (simplified here)
  • We can’t actually use it with protobuf-net, used for serialization and deserialization.

Second solution:

 public abstract class BaseContainer{

  public abstract IList<BaseGroup> Groups {get;}

  public abstract IList<BaseElement> Elements{get;}  
}

public abstract class BaseGroup{
  public abstract BaseContainer Container {get; set;}
}

public abstract class BaseElement{

  public abstract BaseGroup Group{get; set;}
}

public ContainerFoo : BaseContainer{

  public override IList<BaseGroup> Groups {
    get{
      // We are using .Net 4, and we can do that.
      return (IList<BaseGroup>)this.GroupsFoo;
    }
  }

 public IList<GroupFoo> GroupsFoo{ get; private set;}

 // Same thing for elements
}

// You see the point, I don't want to create other classes here.

I think you can obviously see what I don’t like about this second solution!

Any more ideas ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:06:05+00:00Added an answer on June 5, 2026 at 1:06 am

    Instead of using abstract/override you can use the new modifier on the properties of the derived classes:

    class Container {
      public IList<Group> Groups { get; private set; }
    }
    
    class ContainerFoo : Container {
      public new IList<GroupFoo> Groups { get { return (IList<GroupFoo>)base.Groups; } }
    }
    

    Alternatively, is there any real benefit in using inheritence here? Would you be better off defining a generic IContainer<T> interface and doing away with the base class?

    interface IContainer<TGroup> where T : Group {
      IList<TGroup> Groups { get; }
    }
    
    class ContainerFoo : IContainer<GroupFoo> {
      public IList<GroupFoo> Groups { get; private set; }
    }
    
    class ContainerBar : IContainer<GroupBar> {
      public IList<GroupBar> Groups { get; private set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tricky problem that has had me scratching my head for a
I have the following tricky problem: I have implemented a (rather complicated) class which
I have a tricky problem and I'm not sure where in the view rendering
I have a tricky Django problem which didn't occur to me when I was
I have a tricky problem. I have a very complex view in MS SQL
Good day code knights, I have a tricky problem that I cannot see a
I have this problem with codemirror which is quite tricky and I have no
I have a tricky problem that I've been messing about with for a few
I have tricky problem which I can't completely understand. It's jQuery tabs with cookie
I have a tricky question. As everyone knows, Apple clearly says that ONE UIViewController

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.