Static / Permanent Business Objects

I don’t quite know how to ask this question, so I will use it instead of an example:

Imagine that you have an object in the application Country. There are two properties of this object: Nameand collection ' Bordering Countries. Additional properties can be added later, but this will be information that will change very rarely (for example, changes in the names of countries / borders)

Let's say that this application should know about all countries of the world. Where would you save this state of an object? How would you update them? It seems foolish to store all this state in the database, as it will not change very often.

One option may be to have an abstract base object "country" and have a class for each country that inherits from it, with the details of each country. But it seems to me not quite right.

What is the correct way to work with these objects?

UPDATE:

Someone asked about language: C #

In addition, I come to this from the point of view of a web application, so there would be few client installations where I would have to worry about updating hard-coded values.

Most people have suggested not hard-coding data, but using DB or XML files to store data. Can someone provide an example of how this type of object will be "updated" (for example, from an XML file)? Are you using some kind of helper or factory method to get an instance of a specific country?

+5
6

, , 0.02 .

( , ). , , . .. 6 , ?

:

public class Country
    {
        public string Name { get; set; }
        public Country[] BorderingCountries { get; set; }

        public Country(iDB db, string name)
        {
            BorderingCountries = db.BorderingCountriesGet(name);
        }
    }

unit test:

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 => b.Name == "France"));
        Assert.AreEqual(1, c.BorderingCountries.Count(b => b.Name == "Portugal"));
    }

Oops! , ( !) DB:

static void Main(string[] args)
    {
        Countries countries = new Countries(new DB());
    }

public class Countries
    {
        public List<Country> Items { get; set; }
        public Countries(iDB db)
        {
            tblCountry[] countries = db.BorderingCountries();
            Items = new List<Country>();
            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<Country> BorderingCountries { get; set; }
        public Country(string name)
        {
            this.Name = name;
            BorderingCountries = new List<Country>();
        }
    }

    public interface iDB
    {
        tblCountry[] BorderingCountries();
    }

    public class DB : iDB
    {
        public tblCountry[] BorderingCountries()
        {
            using (DataClassesDataContext dc = new DataClassesDataContext())
            {
                return dc.tblCountries.ToArray();
            }
        }

    }

:

public class Countries
{
    public List<Country> Items { get; set; }
    public Countries()
    {
        Items = new List<Country>();
        Items.Add(new Country { Name = "Spain", BorderingCountries = new string[] { "France", "Portugal" }});
        Items.Add(new Country { Name = "France", BorderingCountries = new string[] {"Spain","Belgium"});
    }
}
0

. ( ) . , , , , , .

+3

. , , java ( ), .

+1

, hierachy . Country, .

+1

, , , - . - , - , .

, - , . , , , , , .

You can simply attach the application to the list of countries at startup and leave it in your memory. It will automatically update when the application restarts. Another option would be for him to check updated countries every X-period and update the internal list if it is an application that has been running for a long time.

+1
source

As oedo said, enums should do the job, but if you need more than an index, you can use an xml file. it would be dynamic, no compilation needed

+1
source

All Articles