コード例 #1
0
 static void Main(string[] args)
 {
     //JsonExamples.PrintJsonExamples();
     ClassBasedExamples.PrintClassExamples();
 }
コード例 #2
0
        public static void PrintClassExamples()
        {
            string personOutputString = "id: {0};  LastName: {1}, FirstName: {2}";

            // WRITE ALL PEOPLE TO CONSOLE
            Console.WriteLine("Retreive All The People using classes:");
            IEnumerable <Person> people = ClassBasedExamples.getAllPeople();

            foreach (var person in people)
            {
                Console.WriteLine(personOutputString, person.Id, person.LastName, person.FirstName);
            }

            // WRITE A SPECIFIC PERSON TO CONSOLE:
            Console.WriteLine(Environment.NewLine
                              + "Retreive a Person object by ID:");

            Person singlePerson = ClassBasedExamples.getPerson(2);

            Console.WriteLine(personOutputString, singlePerson.Id,
                              singlePerson.LastName, singlePerson.FirstName);

            // ADD NEW PERSON, THEN WRITE TO CONSOLE:
            Console.WriteLine(Environment.NewLine
                              + "Add a new Person object and return the new object:");

            Person newPerson = new Person {
                LastName = "Atten", FirstName = "John"
            };

            newPerson = AddPerson(newPerson);
            Console.WriteLine(personOutputString, newPerson.Id,
                              newPerson.LastName, newPerson.FirstName);

            // UPDATE AN EXISTING PERSON, THEN WRITE TO CONSOLE:
            Console.WriteLine(Environment.NewLine
                              + "Update an existing Person object:");

            // Pretend we already had a person's data:
            Person personToUpdate = getPerson(2);
            string newLastName    = "Richards";

            Console.WriteLine("Updating Last Name of "
                              + personToUpdate.LastName + " to " + newLastName);
            personToUpdate.LastName = newLastName;

            if (ClassBasedExamples.UpdatePerson(personToUpdate))
            {
                Console.WriteLine(Environment.NewLine + "Updated person object:");
                Person updatedPerson = getPerson(2);
                Console.WriteLine(personOutputString, updatedPerson.Id,
                                  updatedPerson.LastName, updatedPerson.FirstName);
            }


            // DELETE AN EXISTING PERSON BY ID:
            Console.WriteLine(Environment.NewLine + "Delete person object:");

            ClassBasedExamples.DeletePerson(5);

            // WRITE THE UPDATED LIST TO THE CONSOLE:
            {
                Console.WriteLine("Retreive All The People using classes:");
                people = ClassBasedExamples.getAllPeople();
                foreach (var person in people)
                {
                    Console.WriteLine(personOutputString, person.Id,
                                      person.LastName, person.FirstName);
                }
            }

            Console.Read();
        }