Created
May 4, 2012 02:43
-
-
Save nelsonlaquet/2591549 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ConsoleApplication1 | |
{ | |
class Person | |
{ | |
public int Id { get; private set; } | |
public string Name { get; private set; } | |
public Person(int id, string name) | |
{ | |
Id = id; | |
Name = name; | |
} | |
} | |
class People | |
{ | |
public Dictionary<int, Person> People { get; private set; } | |
public People() | |
{ | |
People = new Dictionary<int, Person>(); | |
} | |
public void AddPerson(Person person) | |
{ | |
People.Add(person.Id, person); | |
} | |
public Person GetPersonById(int id) | |
{ | |
return People[id]; | |
} | |
} | |
interface IPeopleView | |
{ | |
void DisplayPeople(); | |
Person SelectPerson(); | |
void DisplayPerson(Person person); | |
} | |
class ConsolePeopleView : IPeopleView | |
{ | |
private People _people; | |
public ConsolePeopleView(People people) | |
{ | |
_people = people; | |
} | |
public void DisplayPeople() | |
{ | |
foreach (var person in _people.People) | |
{ | |
Console.WriteLine("{0} - {1}", person.Key, person.Value.Name); | |
} | |
} | |
public Person SelectPerson() | |
{ | |
DisplayPeople(); | |
Console.Write("Select person: "); | |
var selectedId = int.Parse(Console.ReadLine()); | |
return _people.GetPersonById(selectedId); | |
} | |
public void DisplayPerson(Person person) | |
{ | |
Console.WriteLine("Person: {0} is named {1}", person.Id, person.Name); | |
} | |
} | |
class Database | |
{ | |
public void Save(object obj) | |
{ | |
} | |
} | |
class ThinggyRepository | |
{ | |
private Database _database; | |
public ThinggyRepository(Database database) | |
{ | |
_database = database; | |
} | |
public void Add(object thinggy) | |
{ | |
// validate thinggy | |
_database.Save(thinggy); | |
// perhaps perform more logic here | |
} | |
} | |
class Program | |
{ | |
static void RunProgram(People model, IPeopleView view) | |
{ | |
model.AddPerson(new Person(423, "Nelson")); | |
model.AddPerson(new Person(41, "Whoa")); | |
model.AddPerson(new Person(312, "Blegh")); | |
model.AddPerson(new Person(4214, "Hey")); | |
model.AddPerson(new Person(566, "There")); | |
var selectedPerson = view.SelectPerson(); | |
view.DisplayPerson(selectedPerson); | |
} | |
static void Main(string[] args) | |
{ | |
var people = new People(); | |
var view = new ConsolePeopleView(people); | |
RunProgram(people, view); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment