Created
March 23, 2011 15:19
-
-
Save jgable/883267 to your computer and use it in GitHub Desktop.
TeamRepository and Models for a demo project.
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; | |
/// <summary> | |
/// Team information | |
/// </summary> | |
public class Team | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
public string Captain { get; set; } | |
public List<string> MemberNames { get; set; } | |
public Team() | |
{ | |
MemberNames = new List<string>(); | |
} | |
} | |
/// <summary> | |
/// A base repository class that has some very rudimentary thread safety. | |
/// (I know it's not a "real" repository, just relax bro) | |
/// </summary> | |
public class ListRepository<TItem> | |
where TItem : class | |
{ | |
object itemLock = new object(); | |
protected List<TItem> Items = new List<TItem>(); | |
/// <summary> | |
/// Returns the first item matching the filter. | |
/// </summary> | |
public TItem First(Predicate<TItem> filter) | |
{ | |
TItem result; | |
lock (itemLock) | |
{ | |
result = Items.FirstOrDefault(item => filter(item)); | |
} | |
return result; | |
} | |
/// <summary> | |
/// Returns all items matching the filter. | |
/// </summary> | |
public IEnumerable<TItem> Where(Predicate<TItem> filter) | |
{ | |
IEnumerable<TItem> result; | |
lock (itemLock) | |
{ | |
result = Items.Where(item => filter(item)).ToList(); | |
} | |
return result; | |
} | |
/// <summary> | |
/// Removes all items matching the specified filter. | |
/// </summary> | |
public void Remove(Predicate<TItem> filter) | |
{ | |
lock (itemLock) | |
{ | |
var toRemove = Items.Where(item => filter(item)).ToList(); | |
foreach (var item in toRemove) | |
{ | |
Items.Remove(item); | |
} | |
} | |
} | |
/// <summary> | |
/// Adds a new item. | |
/// </summary> | |
public void Add(TItem newItem) | |
{ | |
lock (itemLock) | |
{ | |
Items.Add(newItem); | |
} | |
} | |
} | |
/// <summary> | |
/// Teams Data | |
/// </summary> | |
public class TeamRepository : ListRepository<Team> | |
{ | |
public TeamRepository() | |
{ | |
Items = new List<Team> | |
{ | |
new Team | |
{ | |
Id = 1, | |
Name = "Jaya Win Jive", | |
Description = "Learning Windows Mobile", | |
Captain = "ravware", | |
MemberNames = new List<string>() { "ravware", "jayasingh" } | |
}, | |
new Team | |
{ | |
Id = 2, | |
Name = "Teem", | |
Description = "I hear they're giving out free Windows Phone 7s at this event! *chuckle*", | |
Captain = "DawsonToth", | |
MemberNames = new List<string>() { "DawsonToth", } | |
}, | |
new Team | |
{ | |
Id = 3, | |
Name = "FVCP", | |
Description = "Fueled by gin. Greg would like to call us the Freezer Weazles...too late.", | |
Captain = "Michalsen", | |
MemberNames = new List<string>() { "Michalsen" } | |
}, | |
new Team | |
{ | |
Id = 4, | |
Name = "devmke", | |
Description = "Milwaukee Developers", | |
Captain = "brennanMKE", | |
MemberNames = new List<string>() { "brennanMKE" } | |
}, | |
new Team | |
{ | |
Id = 5, | |
Name = "anotherTeam", | |
Description = "anotherTeam", | |
Captain = "gordana", | |
MemberNames = new List<string>() { "gordana" } | |
}, | |
new Team | |
{ | |
Id = 6, | |
Name = "Triton Phone Phreaks", | |
Description = "Think Better", | |
Captain = "Dave Arlin", | |
MemberNames = new List<string>() { "matthidinger", "sumeet", "Akshat", "Dave Arlin" } | |
}, | |
new Team | |
{ | |
Id = 7, | |
Name = "mPOP", | |
Description = "nom nom nom", | |
Captain = "nishanthsamala", | |
MemberNames = new List<string>() { "pricexperience", "nishanthsamala" } | |
}, | |
new Team | |
{ | |
Id = 8, | |
Name = "Packer Hackers", | |
Description = "A husband and wife team.", | |
Captain = "thejeanious", | |
MemberNames = new List<string>() { "thejeanious", "scott.seely" } | |
}, | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment