Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've got a variety of answers, so I thought I'd add my $0.02 worth. </p> <p>Personally I alway hard code fixed lists like this (same with postcodes). That being said, when I'm at your position, I'll always optimize for readability. ie What will make sense in 6 months time, when you've forgotten about this project and have to do some maintenance?</p> <p>If I had to do it with a database:</p> <pre><code>public class Country { public string Name { get; set; } public Country[] BorderingCountries { get; set; } public Country(iDB db, string name) { BorderingCountries = db.BorderingCountriesGet(name); } } </code></pre> <p>Your unit test:</p> <pre><code>public UnitTest1() { iDB db = new DB(); Country c = new Country(db, "Spain"); Assert.AreEqual(2, c.BorderingCountries.Count()); Assert.AreEqual(1, c.BorderingCountries.Count(b =&gt; b.Name == "France")); Assert.AreEqual(1, c.BorderingCountries.Count(b =&gt; b.Name == "Portugal")); } </code></pre> <p>Oops! You probably want to populate the whole list (not one at a time!) DB:</p> <pre><code>static void Main(string[] args) { Countries countries = new Countries(new DB()); } public class Countries { public List&lt;Country&gt; Items { get; set; } public Countries(iDB db) { tblCountry[] countries = db.BorderingCountries(); Items = new List&lt;Country&gt;(); Country country = null; foreach (var c in countries) { if (country == null || country.Name != c.Name) { country = new Country(c.Name); Items.Add(country); } country.BorderingCountries.Add(new Country(c.BorderingCountry)); } } } public class Country { public string Name { get; set; } public List&lt;Country&gt; BorderingCountries { get; set; } public Country(string name) { this.Name = name; BorderingCountries = new List&lt;Country&gt;(); } } public interface iDB { tblCountry[] BorderingCountries(); } public class DB : iDB { public tblCountry[] BorderingCountries() { using (DataClassesDataContext dc = new DataClassesDataContext()) { return dc.tblCountries.ToArray(); } } } </code></pre> <p>If I was hardcoding it:</p> <pre><code>public class Countries { public List&lt;Country&gt; Items { get; set; } public Countries() { Items = new List&lt;Country&gt;(); Items.Add(new Country { Name = "Spain", BorderingCountries = new string[] { "France", "Portugal" }}); Items.Add(new Country { Name = "France", BorderingCountries = new string[] {"Spain","Belgium"}); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload