Esempio n. 1
0
        public void SingletonTotalPopulationTest()
        {
            var rf    = new SingletonRecordFinder();
            var names = new[] { "Seoul", "Mexico City" };
            int tp    = rf.GetTotalPopulation(names);

            Assert.AreEqual(17500000 + 17400000, tp);
        }
Esempio n. 2
0
        public void SingletonTotalPopulationTest()
        {
            var rf    = new SingletonRecordFinder();
            var names = new[] { "Seoul", "New York" };
            int tp    = rf.GetTotalPopulation(names);

            Assert.That(tp, Is.EqualTo(17800000 + 17500000));
        }
Esempio n. 3
0
        public void SingletonTotalPopulationTest()
        {
            // testing on a live database
            var rf    = new SingletonRecordFinder();
            var names = new[] { "Seoul", "Mexico City" };
            int tp    = rf.TotalPopulation(names);

            Assert.That(tp, Is.EqualTo(17500000 + 17400000));
        }
        public void SingletonTotalPopulationTest()
        {
            var recordFinder = new SingletonRecordFinder();
            var names        = new[] { "New Delhi", "Beijing" };

            var totalPopulations = recordFinder.TotalPopulation(names);

            Assert.That(totalPopulations, Is.EqualTo(21542000 + 14200004));
        }
Esempio n. 5
0
        public void ConfigurablePopulationTest()
        {
            SingletonRecordFinder singletonRecordFinder = new SingletonRecordFinder();
            int population = singletonRecordFinder.GetTotalPopulation(new List <string>()
            {
                "Seoul", "Tokyo"
            });

            Assert.AreEqual(33200000 + 17500000, population);
        }
Esempio n. 6
0
            public void SingletonTotalPopulationTest()
            {
                // Singleton database is used as live (production) database test
                // data of live database may changed by user
                // so we need to create another database for test environment
                var rf    = new SingletonRecordFinder();
                var names = new[] { "Seoul", "Mexico City" };
                var tp    = rf.GetTotalPopulation(names);

                Assert.That(tp, Is.EqualTo(17500000 + 17400000));
            }
Esempio n. 7
0
        public void SingletonTotalPopulationTest()
        {
            // Here we're testing on a 'live db'
            // One of the consequences is that we need to look at the DB for the values we want to test
            var rf = new SingletonRecordFinder();
            // If somebody removes from the db one of the entries, the test will break
            var names = new[] { "Seoul", "Mexico City", "Osaka" };
            int tp    = rf.GetTotalPopulation(names);

            Assert.That(tp, Is.EqualTo(17500000 + 17400000 + 16425000));
            // to be independent from a live db is very expensive
            // what you want to do is to fake the object instead the real db
            // In this particular scenario, to fake it isn't possible because RecordFinder is using a hardcoded reference
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            var db  = SingletonDatabase.Instance;
            var db2 = SingletonDatabase.Instance;

            Console.WriteLine(db.GetPopulation("Kitchener"));
            Console.WriteLine($"db.Equals(db2): {db.Equals(db2)}");
            Console.WriteLine($"SingletonDatabase.Count: {SingletonDatabase.Count}");
            var rf = new SingletonRecordFinder();

            Console.WriteLine($"Total population of Kitchener and Waterloo: {rf.GetTotalPopulation(new [] { "Kitchener", "Waterloo" })}");
            var crf = new ConfigurableRecordFinder(new DummyDatabase());

            Console.WriteLine($"Total population of Kitchener and Waterloo with ConfigurableRecordFinder and DummyDatabase injected: {crf.GetTotalPopulation(new[] { "Kitchener", "Waterloo" })}");
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Singleton through dependency injection");
            var collection = new ServiceCollection();

            collection.AddSingleton <IDatabase, OrdinaryDatabse>();
            var serviceProvider = collection.BuildServiceProvider();
            var service         = serviceProvider.GetService <IDatabase>();
            var tokyoPop        = service.GetPopulation("Tokyo");

            Console.WriteLine($"Tokyo population is {tokyoPop}");


            SingletonRecordFinder recordFinder = new SingletonRecordFinder();
            var result = recordFinder.GetToalPopulation(new[] { "Tokyo", "New York" });

            Console.WriteLine($"Total Population {result}");

            var configurableRecordFinder = new ConfigurableRecordFinder(new DummyDatabase());
            var total = configurableRecordFinder.GetToalPopulation(new[] { "New York", "Tokyo" });

            Console.WriteLine(total);


            Console.WriteLine("A variation of singleton, which is MonoState");
            var ceo1 = new MonoState()
            {
                Name     = "Payam",
                Position = "CEO",
                Age      = 100
            };

            var ceo2 = new MonoState()
            {
                Name     = "Jack",
                Position = "Developer",
                Age      = 90
            };

            Console.WriteLine(ceo1);
            Console.WriteLine(ceo2);
        }