Map, which contains biomes, which contain landforms, which contain tiles
$begingroup$
I am trying to model this:
- a Map can have multiple child of type Biomes and no parent
- a Biome can have multiple child of type Landforms and a Map as its parent
- a Landform can have multiple child of type Tiles and a Biome as its parent
- a Tile has no child and a Landform as its parent
I want it to be generic so I can easily add new links to the chain (like adding a new kind of section between Biome and Landform for example). Here is the less ugly solution I have for now :
public class RootSection<T, TChild> : Section<T>
where T : Section<T>
where TChild : Section<TChild>
{
public List<TChild> ChildSection { get; } // duplicate
}
public class MiddleSection<T, TChild, TParent> : Section<T>
where T : Section<T>
where TChild : Section<TChild>
where TParent : Section<TParent>
{
public List<TChild> ChildSection { get; } // duplicate
public TParent Parent { get; } // duplicate
}
public class BottomSection<T, TParent> : Section<T>
where T : Section<T>
where TParent : Section<TParent>
{
public TParent Parent { get; } // duplicate
}
public class Section<T>
where T : Section<T>
{
List<T> AdjacentSections { get; }
}
public class Map : RootSection<Map, Biome> { } // (T, TChild)
public class Biome : MiddleSection<Biome, Landform, Map> { } // (T, TChild, TParent)
public class Landform : MiddleSection<Landform, Tile, Biome> { } // (T, TChild, TParent)
public class Tile : BottomSection<Tile, Landform> { } // (T, TParent)
As you can see, there is already duplicate code and I can't think of a solution to get rid of this issue. I feel like I am either missing something obvious or over-complexifying the problem. I also feel like this is close to a classic data structure which I ignore the name preventing me from searching for inspiration on the net.
How can I rewrite this code to look cleaner ? Am I right to think it's close to a well known data structure ?
c# object-oriented generics
New contributor
$endgroup$
add a comment |
$begingroup$
I am trying to model this:
- a Map can have multiple child of type Biomes and no parent
- a Biome can have multiple child of type Landforms and a Map as its parent
- a Landform can have multiple child of type Tiles and a Biome as its parent
- a Tile has no child and a Landform as its parent
I want it to be generic so I can easily add new links to the chain (like adding a new kind of section between Biome and Landform for example). Here is the less ugly solution I have for now :
public class RootSection<T, TChild> : Section<T>
where T : Section<T>
where TChild : Section<TChild>
{
public List<TChild> ChildSection { get; } // duplicate
}
public class MiddleSection<T, TChild, TParent> : Section<T>
where T : Section<T>
where TChild : Section<TChild>
where TParent : Section<TParent>
{
public List<TChild> ChildSection { get; } // duplicate
public TParent Parent { get; } // duplicate
}
public class BottomSection<T, TParent> : Section<T>
where T : Section<T>
where TParent : Section<TParent>
{
public TParent Parent { get; } // duplicate
}
public class Section<T>
where T : Section<T>
{
List<T> AdjacentSections { get; }
}
public class Map : RootSection<Map, Biome> { } // (T, TChild)
public class Biome : MiddleSection<Biome, Landform, Map> { } // (T, TChild, TParent)
public class Landform : MiddleSection<Landform, Tile, Biome> { } // (T, TChild, TParent)
public class Tile : BottomSection<Tile, Landform> { } // (T, TParent)
As you can see, there is already duplicate code and I can't think of a solution to get rid of this issue. I feel like I am either missing something obvious or over-complexifying the problem. I also feel like this is close to a classic data structure which I ignore the name preventing me from searching for inspiration on the net.
How can I rewrite this code to look cleaner ? Am I right to think it's close to a well known data structure ?
c# object-oriented generics
New contributor
$endgroup$
add a comment |
$begingroup$
I am trying to model this:
- a Map can have multiple child of type Biomes and no parent
- a Biome can have multiple child of type Landforms and a Map as its parent
- a Landform can have multiple child of type Tiles and a Biome as its parent
- a Tile has no child and a Landform as its parent
I want it to be generic so I can easily add new links to the chain (like adding a new kind of section between Biome and Landform for example). Here is the less ugly solution I have for now :
public class RootSection<T, TChild> : Section<T>
where T : Section<T>
where TChild : Section<TChild>
{
public List<TChild> ChildSection { get; } // duplicate
}
public class MiddleSection<T, TChild, TParent> : Section<T>
where T : Section<T>
where TChild : Section<TChild>
where TParent : Section<TParent>
{
public List<TChild> ChildSection { get; } // duplicate
public TParent Parent { get; } // duplicate
}
public class BottomSection<T, TParent> : Section<T>
where T : Section<T>
where TParent : Section<TParent>
{
public TParent Parent { get; } // duplicate
}
public class Section<T>
where T : Section<T>
{
List<T> AdjacentSections { get; }
}
public class Map : RootSection<Map, Biome> { } // (T, TChild)
public class Biome : MiddleSection<Biome, Landform, Map> { } // (T, TChild, TParent)
public class Landform : MiddleSection<Landform, Tile, Biome> { } // (T, TChild, TParent)
public class Tile : BottomSection<Tile, Landform> { } // (T, TParent)
As you can see, there is already duplicate code and I can't think of a solution to get rid of this issue. I feel like I am either missing something obvious or over-complexifying the problem. I also feel like this is close to a classic data structure which I ignore the name preventing me from searching for inspiration on the net.
How can I rewrite this code to look cleaner ? Am I right to think it's close to a well known data structure ?
c# object-oriented generics
New contributor
$endgroup$
I am trying to model this:
- a Map can have multiple child of type Biomes and no parent
- a Biome can have multiple child of type Landforms and a Map as its parent
- a Landform can have multiple child of type Tiles and a Biome as its parent
- a Tile has no child and a Landform as its parent
I want it to be generic so I can easily add new links to the chain (like adding a new kind of section between Biome and Landform for example). Here is the less ugly solution I have for now :
public class RootSection<T, TChild> : Section<T>
where T : Section<T>
where TChild : Section<TChild>
{
public List<TChild> ChildSection { get; } // duplicate
}
public class MiddleSection<T, TChild, TParent> : Section<T>
where T : Section<T>
where TChild : Section<TChild>
where TParent : Section<TParent>
{
public List<TChild> ChildSection { get; } // duplicate
public TParent Parent { get; } // duplicate
}
public class BottomSection<T, TParent> : Section<T>
where T : Section<T>
where TParent : Section<TParent>
{
public TParent Parent { get; } // duplicate
}
public class Section<T>
where T : Section<T>
{
List<T> AdjacentSections { get; }
}
public class Map : RootSection<Map, Biome> { } // (T, TChild)
public class Biome : MiddleSection<Biome, Landform, Map> { } // (T, TChild, TParent)
public class Landform : MiddleSection<Landform, Tile, Biome> { } // (T, TChild, TParent)
public class Tile : BottomSection<Tile, Landform> { } // (T, TParent)
As you can see, there is already duplicate code and I can't think of a solution to get rid of this issue. I feel like I am either missing something obvious or over-complexifying the problem. I also feel like this is close to a classic data structure which I ignore the name preventing me from searching for inspiration on the net.
How can I rewrite this code to look cleaner ? Am I right to think it's close to a well known data structure ?
c# object-oriented generics
c# object-oriented generics
New contributor
New contributor
edited 1 min ago
200_success
129k15153417
129k15153417
New contributor
asked 20 hours ago
BenHBenH
1111
1111
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
I want it to be generic so I can easily add new links to the chain
(like adding a new kind of section between Biome and Landform for
example)
Generics work fine for simple generic data structures (like a List). Your "generic" data structure is actually a very special one which will not be used outside of your model. It is more a try to extract the common parts of your model to a generic structure which may be useful, but based on your question I can not see the value in your case.
In my experience, data stuctures with multiple generic types, which has contraints to other generic types, are hard to understand and make the code more complicated.
In your case, I would just give generics up and write the data structure down as it is:
public class Map
{
public List<Biome> Biomes { get; } = new List<Biome>();
public List<Map> AdjacentMaps { get; } = new List<Map>();
}
public class Biome
{
public Map Map {get; }
public List<Landform> Landforms { get; } = new List<Landform>();
public List<Biome> AdjacentBiomes { get; } = new List<Biome>();
}
public class Landform
{
public Biome Biome {get; }
public List<Tile> Tiles { get; } = new List<Tile>();
public List<Landform> AdjacentLandforms { get; } = new List<Landform>();
}
public class Tile
{
public Landform Landform {get; }
public List<Tile> AdjacentTiles { get; } = new List<Tile>();
}
Much more readable! The properties has more descriptive names and it needs a minute to extend this hierachical data structure with other types.
$endgroup$
$begingroup$
Besides readable, it is by far more understandable. QUESTION:Parent
/Child
idea is transformed toAdjacent
. In as much as "ATile
has no child", for example, is this bad? Is the P/C relationship explicitly relevant for other code? If this is all about composition - A Biome is composed of LandTypes, then I think "Parent/Child" concept is actually misleading.
$endgroup$
– radarbob
5 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
BenH is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214102%2fmap-which-contains-biomes-which-contain-landforms-which-contain-tiles%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I want it to be generic so I can easily add new links to the chain
(like adding a new kind of section between Biome and Landform for
example)
Generics work fine for simple generic data structures (like a List). Your "generic" data structure is actually a very special one which will not be used outside of your model. It is more a try to extract the common parts of your model to a generic structure which may be useful, but based on your question I can not see the value in your case.
In my experience, data stuctures with multiple generic types, which has contraints to other generic types, are hard to understand and make the code more complicated.
In your case, I would just give generics up and write the data structure down as it is:
public class Map
{
public List<Biome> Biomes { get; } = new List<Biome>();
public List<Map> AdjacentMaps { get; } = new List<Map>();
}
public class Biome
{
public Map Map {get; }
public List<Landform> Landforms { get; } = new List<Landform>();
public List<Biome> AdjacentBiomes { get; } = new List<Biome>();
}
public class Landform
{
public Biome Biome {get; }
public List<Tile> Tiles { get; } = new List<Tile>();
public List<Landform> AdjacentLandforms { get; } = new List<Landform>();
}
public class Tile
{
public Landform Landform {get; }
public List<Tile> AdjacentTiles { get; } = new List<Tile>();
}
Much more readable! The properties has more descriptive names and it needs a minute to extend this hierachical data structure with other types.
$endgroup$
$begingroup$
Besides readable, it is by far more understandable. QUESTION:Parent
/Child
idea is transformed toAdjacent
. In as much as "ATile
has no child", for example, is this bad? Is the P/C relationship explicitly relevant for other code? If this is all about composition - A Biome is composed of LandTypes, then I think "Parent/Child" concept is actually misleading.
$endgroup$
– radarbob
5 hours ago
add a comment |
$begingroup$
I want it to be generic so I can easily add new links to the chain
(like adding a new kind of section between Biome and Landform for
example)
Generics work fine for simple generic data structures (like a List). Your "generic" data structure is actually a very special one which will not be used outside of your model. It is more a try to extract the common parts of your model to a generic structure which may be useful, but based on your question I can not see the value in your case.
In my experience, data stuctures with multiple generic types, which has contraints to other generic types, are hard to understand and make the code more complicated.
In your case, I would just give generics up and write the data structure down as it is:
public class Map
{
public List<Biome> Biomes { get; } = new List<Biome>();
public List<Map> AdjacentMaps { get; } = new List<Map>();
}
public class Biome
{
public Map Map {get; }
public List<Landform> Landforms { get; } = new List<Landform>();
public List<Biome> AdjacentBiomes { get; } = new List<Biome>();
}
public class Landform
{
public Biome Biome {get; }
public List<Tile> Tiles { get; } = new List<Tile>();
public List<Landform> AdjacentLandforms { get; } = new List<Landform>();
}
public class Tile
{
public Landform Landform {get; }
public List<Tile> AdjacentTiles { get; } = new List<Tile>();
}
Much more readable! The properties has more descriptive names and it needs a minute to extend this hierachical data structure with other types.
$endgroup$
$begingroup$
Besides readable, it is by far more understandable. QUESTION:Parent
/Child
idea is transformed toAdjacent
. In as much as "ATile
has no child", for example, is this bad? Is the P/C relationship explicitly relevant for other code? If this is all about composition - A Biome is composed of LandTypes, then I think "Parent/Child" concept is actually misleading.
$endgroup$
– radarbob
5 hours ago
add a comment |
$begingroup$
I want it to be generic so I can easily add new links to the chain
(like adding a new kind of section between Biome and Landform for
example)
Generics work fine for simple generic data structures (like a List). Your "generic" data structure is actually a very special one which will not be used outside of your model. It is more a try to extract the common parts of your model to a generic structure which may be useful, but based on your question I can not see the value in your case.
In my experience, data stuctures with multiple generic types, which has contraints to other generic types, are hard to understand and make the code more complicated.
In your case, I would just give generics up and write the data structure down as it is:
public class Map
{
public List<Biome> Biomes { get; } = new List<Biome>();
public List<Map> AdjacentMaps { get; } = new List<Map>();
}
public class Biome
{
public Map Map {get; }
public List<Landform> Landforms { get; } = new List<Landform>();
public List<Biome> AdjacentBiomes { get; } = new List<Biome>();
}
public class Landform
{
public Biome Biome {get; }
public List<Tile> Tiles { get; } = new List<Tile>();
public List<Landform> AdjacentLandforms { get; } = new List<Landform>();
}
public class Tile
{
public Landform Landform {get; }
public List<Tile> AdjacentTiles { get; } = new List<Tile>();
}
Much more readable! The properties has more descriptive names and it needs a minute to extend this hierachical data structure with other types.
$endgroup$
I want it to be generic so I can easily add new links to the chain
(like adding a new kind of section between Biome and Landform for
example)
Generics work fine for simple generic data structures (like a List). Your "generic" data structure is actually a very special one which will not be used outside of your model. It is more a try to extract the common parts of your model to a generic structure which may be useful, but based on your question I can not see the value in your case.
In my experience, data stuctures with multiple generic types, which has contraints to other generic types, are hard to understand and make the code more complicated.
In your case, I would just give generics up and write the data structure down as it is:
public class Map
{
public List<Biome> Biomes { get; } = new List<Biome>();
public List<Map> AdjacentMaps { get; } = new List<Map>();
}
public class Biome
{
public Map Map {get; }
public List<Landform> Landforms { get; } = new List<Landform>();
public List<Biome> AdjacentBiomes { get; } = new List<Biome>();
}
public class Landform
{
public Biome Biome {get; }
public List<Tile> Tiles { get; } = new List<Tile>();
public List<Landform> AdjacentLandforms { get; } = new List<Landform>();
}
public class Tile
{
public Landform Landform {get; }
public List<Tile> AdjacentTiles { get; } = new List<Tile>();
}
Much more readable! The properties has more descriptive names and it needs a minute to extend this hierachical data structure with other types.
answered 16 hours ago
JanDotNetJanDotNet
6,8681339
6,8681339
$begingroup$
Besides readable, it is by far more understandable. QUESTION:Parent
/Child
idea is transformed toAdjacent
. In as much as "ATile
has no child", for example, is this bad? Is the P/C relationship explicitly relevant for other code? If this is all about composition - A Biome is composed of LandTypes, then I think "Parent/Child" concept is actually misleading.
$endgroup$
– radarbob
5 hours ago
add a comment |
$begingroup$
Besides readable, it is by far more understandable. QUESTION:Parent
/Child
idea is transformed toAdjacent
. In as much as "ATile
has no child", for example, is this bad? Is the P/C relationship explicitly relevant for other code? If this is all about composition - A Biome is composed of LandTypes, then I think "Parent/Child" concept is actually misleading.
$endgroup$
– radarbob
5 hours ago
$begingroup$
Besides readable, it is by far more understandable. QUESTION:
Parent
/ Child
idea is transformed to Adjacent
. In as much as "A Tile
has no child", for example, is this bad? Is the P/C relationship explicitly relevant for other code? If this is all about composition - A Biome is composed of LandTypes, then I think "Parent/Child" concept is actually misleading.$endgroup$
– radarbob
5 hours ago
$begingroup$
Besides readable, it is by far more understandable. QUESTION:
Parent
/ Child
idea is transformed to Adjacent
. In as much as "A Tile
has no child", for example, is this bad? Is the P/C relationship explicitly relevant for other code? If this is all about composition - A Biome is composed of LandTypes, then I think "Parent/Child" concept is actually misleading.$endgroup$
– radarbob
5 hours ago
add a comment |
BenH is a new contributor. Be nice, and check out our Code of Conduct.
BenH is a new contributor. Be nice, and check out our Code of Conduct.
BenH is a new contributor. Be nice, and check out our Code of Conduct.
BenH is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214102%2fmap-which-contains-biomes-which-contain-landforms-which-contain-tiles%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown